Search Unity

Question StringTable.GetEntry doesn't work without an active locale

Discussion in 'Localization Tools' started by AlaaKhallouf, May 14, 2023.

  1. AlaaKhallouf

    AlaaKhallouf

    Joined:
    Dec 14, 2022
    Posts:
    10
    so I followed a question previously posted here about how to find if a table has an entry by its key, and its working fine, it returns null if if the key is invalid.
    Code (CSharp):
    1.  
    2.         var text = textMesh.text;
    3.         var sd = LocalizationSettings.StringDatabase;
    4.         var ta = sd.GetTable(tableReference); //problem: needs active locale
    5.         var entry = ta.GetEntry(text);
    6.  
    The problem is that sd.GetTable(tableReference) returns null if there is no ActiveLocale (I'm running this script in the editor), and the method fails, although it shouldn't depend on having an active locale since I'm searching for the Key not the Value.

    is there a better way to find if the Key exists without having to set an ActiveLocale in the Scene Controls panel? its very annoying since it defaults to None and I have to open that panel to set it up
     
  2. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    8,297
    If this is Editor code then using LocalizationEditorSettings will work better.
    You can call GetStringTableCollection to return all the string tables.

    E.G

    Code (csharp):
    1. var collection = LocalizationEditorSettings.GetStringTableCollection("My Strings");
    2. var englishTable = collection.GetTable("en");
    3. Debug.Log($"Preload: {LocalizationEditorSettings.GetPreloadTableFlag(englishTable)}");