Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question Smart String with Arguments?

Discussion in 'Localization Tools' started by Cuku_, Feb 20, 2023.

  1. Cuku_

    Cuku_

    Joined:
    Sep 2, 2012
    Posts:
    10
    I'm not sure if this is possible or a if there's a workaroun, so here goes nothing.

    I'm using Localization Package with UIElements and I got the following code wich works fine for "normal" smart strings:

    Code (CSharp):
    1.         #region Smart Strings
    2.  
    3.         private class SmartStrings
    4.         {
    5.             public string selected_group;
    6.             public string selected_item;
    7.         }
    8.  
    9.         private static SmartStrings[] GetSmartStrings()
    10.             => new[] { new SmartStrings()
    11.             {
    12.                // GroupExtensions.SelectedGroup() is a static method that returns the name of the selected group
    13.                 selected_group = GroupExtensions.SelectedGroup(),
    14.                 selected_item = ItemExtensions.SelectedItem()
    15.             }};
    16.  
    17.         #endregion
    18.  
    19. // Get localized string taking in consideration also Smart Strings defined above
    20. var localizedString = stringTable[name].GetLocalizedString(args: smartStrings);
    The localized string value then is something like:
    "You have added this {selected_item} Item in {selected group} Group!"

    All this works fine.

    The next thing I want to do is if I can use something similar with static methods but which accept arguments, maybe something like this:

    "You have added this {selected_item(0)} Item in {selected group(3)} Group!"
    "You have added this {selected_item(1)} Item in {selected group(6)} Group!"
    "You have added this {selected_item(2)} Item in {selected group(9)} Group!"

    And the static methods would be something like this:

    selected_group = GroupExtensions.SelectedGroup(args),
    selected_item = ItemExtensions.SelectedItem(args)

    I'm not sure if this makes sense.
     
  2. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    8,226
    You could either pass it into the arguments like so:

    Code (csharp):
    1. var otherArgs = new object []{ 1, 2, 3, 4, 5, 6};
    2. var args = new object[]{ smartStrings, otherArgs};
    3. var localizedString = stringTable[name].GetLocalizedString(args);
    You would need to change the argument to reference the 2nd index, e.g
    You have added this {selected_item}({1.0}) Item in {selected_group}({1.3}) Group!

    Alternatively, you can also make it part of SmartStrings:

    Code (csharp):
    1.  
    2. class SmartStrings
    3. {
    4.     public string selected_group;
    5.     public string selected_item;
    6.     public IList myList;
    7. }
    You have added this {selected_item}({myList.0}) Item in {selected_group}({myList.3}) Group!

    In both these examples we are using the list formatter.
     
    Cuku_ likes this.