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

Simple Get String from table via script?

Discussion in 'Localization Tools' started by Bogdenz, Jul 25, 2020.

  1. Bogdenz

    Bogdenz

    Joined:
    Oct 17, 2019
    Posts:
    4
    Apologies if this is super obvious, but I couldn't figure out how to do it in the documentation.

    If I have a string table named "UI Text" like in the quick start example, what's the easiest way to just do something like

    Code (CSharp):
    1. public SomeSortOfSharedStringTable myTable;
    2.  
    3. myTable.GetLocalizedString(stringKeyName);
    Where I can just pull up a value by key name and automatically get the string for the current locale?

    Is there some global way to do something else like
    Code (CSharp):
    1. Localization.GetLocalizedStringTableEntry("UI Text", "START_GAME");
     
  2. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    8,230
    Yes it's LocalizationSettings.StringDatabase.GetLocalizedString
     
    Bogdenz likes this.
  3. Bogdenz

    Bogdenz

    Joined:
    Oct 17, 2019
    Posts:
    4
    Ahhh, thanks! I had seen that, but when I saw that it took `TableEntryReference`s I didn't realize that those were just structs that could be implicitly generated from strings. Thought they were specific existing references or something. Not sure if it's worth changing, but just seeing the autocomplete for that function pop up with a string input as an explicit overload would have helped in the learning through discovery process for me, even though it's obviously not necessary mechanically speaking given the implicit string to TableEntryReference that exists.

    Also totally aside, this feels like an extremely common use case to me, but it feels like it's quite deeply nested in terms of namespacing and layering, which made it harder to discover. All in all, you have to dig quite a bit (though obviously this is cut down with the right using directive):
    Code (CSharp):
    1. UnityEngine.Localization.Settings.LocalizationSettings.StringDatabase.GetLocalizedStringAsync("My Key");
     
    Last edited: Jul 28, 2020
  4. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    8,230
    Yes it is deep if you use the full namespace.
    The TableEntry and TableEntryReference exist so we don't need to have multiple versions of each function. 1 table string, 1 table guid, 1 key name, 1 key I'd, then arguments and locale etc. It creates a lot of overloads. So those structs help to reduce it although I can see how they may be confusing at first.