Search Unity

  1. We are migrating the Unity Forums to Unity Discussions. On July 12, the Unity Forums will become read-only. On July 15, Unity Discussions will become read-only until July 18, when the new design and the migrated forum contents will go live. Read our full announcement for more information and let us know if you have any questions.

Question How to access the localized string with a key?

Discussion in 'Localization Tools' started by SeleASD, Dec 22, 2022.

  1. SeleASD

    SeleASD

    Joined:
    Nov 13, 2021
    Posts:
    2
    I need some help understanding, how to access the localized string in a LocalizedStringTable. I have a color picker game that I'd like to localize. There's the name of the color on top of a color grid that I'd like to be able to change according the active locale.

    upload_2022-12-22_15-25-32.png

    As the player chooses the correct color, the grid refreshes and sets new color to be chosen. How can I fetch the localized string with the color string that is given in the grid refresh? I do have a string table in the localization tables, but can't figure out how to access the key.

    Code (CSharp):
    1. public class GridSpawner : MonoBehaviour
    2. {
    3.     private Canvas canvas;
    4.  
    5.     public Text wantedText;
    6.     public string localizedString;
    7.     public string wantedItem;
    8.  
    9.     public string localizedStringTableName;
    10.     public LocalizedStringTable stringTable;
    11.  
    12.     public Button[] buttonList;
    13.     public Button[] activeButtonList;
    14.     public int rows;
    15.     public int columns;
    16.     public int imageWidth;
    17.     public float spacing;
    18.     public float verticalOffset;
    19.  
    20.     private void Awake()
    21.     {
    22.         stringTable = new LocalizedStringTable { TableReference = localizedStringTableName };
    23.     }
    24.  
    25.     // Start is called before the first frame update
    26.     void Start()
    27.     {
    28.         canvas = gameObject.GetComponent<Canvas>();
    29.         ResetGrid();
    30.     }
    31.  
    32.     public void ResetGrid()
    33.     {
    34.         // Destroying the previous grid
    35.        
    36.         // Picking the array randomly
    37.        
    38.         // Setting the item name to look for
    39.         int random = Random.Range(0, activeButtonList.Length);
    40.         wantedItem = activeButtonList[random].name;
    41.  
    42.         // Fetching the localized string
    43.         localizedString = GetLocalizedString(stringTable, wantedItem);
    44.         wantedText.text = localizedString;
    45.  
    46.         // Initializing the grid
    47.     }
    48.  
    49. ...
    50.  
    51.     static string GetLocalizedString(StringTable table, string entryName)
    52.     {
    53.         var entry = table.GetEntry(entryName);
    54.         return entry.GetLocalizedString();
    55.     }
    56. }
    Now the stringTable variable in "GetLocalizedString(stringTable, wantedItem)" cannot convert from LocalizedStringTable to StringTable. I'm guessing I'm missing the OnEnable, OnDisable and LoadStrings methods shown in the examples, but I could use a little help on how to use them in this case.
     
  2. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    8,570
    There are a few ways. I would either call directly into
    Code (csharp):
    1. var translatedValue = LocalizationSettings.StringDatabase.GetLocalizedString(localizedStringTableName, entryName)
    Or use a LocalizedString like so:
    Code (csharp):
    1. public class GridSpawner : MonoBehaviour
    2. {
    3.     private Canvas canvas;
    4.  
    5.     public Text wantedText;
    6.     public string localizedString;
    7.     public string wantedItem;
    8.  
    9.     public LocalizedString localizedString;
    10.  
    11.     public Button[] buttonList;
    12.     public Button[] activeButtonList;
    13.     public int rows;
    14.     public int columns;
    15.     public int imageWidth;
    16.     public float spacing;
    17.     public float verticalOffset;
    18.  
    19.     private void Awake()
    20.     {
    21.         localizedString = new LocalizedString { TableReference = localizedStringTableName };
    22.         localizedString.StringChanged += StringValueChanged;
    23.     }
    24.  
    25.     void StringValueChanged(string value)
    26.     {
    27.         wantedText.text = value;
    28.     }
    29.  
    30.     // Start is called before the first frame update
    31.     void Start()
    32.     {
    33.         canvas = gameObject.GetComponent<Canvas>();
    34.         ResetGrid();
    35.     }
    36.  
    37.     public void ResetGrid()
    38.     {
    39.         // Destroying the previous grid
    40.        
    41.         // Picking the array randomly
    42.        
    43.         // Setting the item name to look for
    44.         int random = Random.Range(0, activeButtonList.Length);
    45.         wantedItem = activeButtonList[random].name;
    46.  
    47.         // This will trigger an update to the string
    48.         localizedString.TableEntryReference = wantedItem;
    49.  
    50.         // Initializing the grid
    51.     }
    52. }
     
  3. SeleASD

    SeleASD

    Joined:
    Nov 13, 2021
    Posts:
    2
    Awesome, LocalizedString worked like a charm in this case! Thanks!
     
    karl_jones likes this.