Search Unity

Best practices for UI heavy dynamic localization?

Discussion in 'Localization Tools' started by nexprime, Feb 6, 2021.

  1. nexprime

    nexprime

    Joined:
    Nov 15, 2016
    Posts:
    5
    Trying to figure out the best way to add localization to a UI-heavy scene essentially containing a dynamic spreadsheet – see attachment.

    Localizing the buttons and column headings is trivial, however the data rows is my concern.

    Scroll view contains rows of prefabs with dynamically assigned data; and could potentially have a few hundred entries, each with a string requiring localization.

    At present, each “list item” has a LocalizeStringEvent component co-located with the Text component; and the “spreadsheet” control script is using TableEntryReference to set applicable localization table keys to each list item. The table reference is set in the editor and does not change at runtime. Specifically, something along the lines of:

    foreach (LocalizeStringEvent t in listItemLocolizedText)
    {
    t.StringReference.TableEntryReference = $"{tableKeyPrefix }_{tableKeySuffix }";
    }

    tableKeyPrefix and tableKeySuffix being a pair of strings getting concatenated to form a Localization Table Key … something to the effect of “EXPORT_REGION15”.

    The LocalizeStringEvent components are cached by the controller, so no concerns about mass GetComponent() calls. Additionally, the localized part of the data is only updated when the list needs to be fully rebuilt – so no localization in the Update() loop.

    While functionally this seems to work fine, the idea of having a few hundred LocalizeStringEvent components in a single scene, each registering its own listener and such, seems like a naïve implementation.

    Is there perhaps a better way to handle this?
     

    Attached Files:

  2. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    8,291
    The LocalizeStringEvent is there so you don't have to write code and can get started quickly. If you have a lot in a scene that needs to be localized then we have several different ways to do this through scripts.
    Take a look at the samples in the package manager.
    If all your entries are in the same table then a LocalizedStringTable could be used to get the table and you can then get items directly from it or you can use the LocalizedString in your own class to do something similar to the Event class. You could have a list of them. You can also call into the String database to load values.

    So you could perhaps cache the tables and then extract the values directly from them.
     
    nexprime likes this.
  3. nexprime

    nexprime

    Joined:
    Nov 15, 2016
    Posts:
    5
    I think using LocalizedStringTable to get the entry strings in bulk is the way to go!
    Thanks for pointing me in the right direction Karl!

    Having implemented the above, do I need to listen to any other events other than LocalizationSettings.SelectedLocaleChanged? (assuming my own code doesn't make changes to table entries, of course)
     
    Last edited: Feb 6, 2021
    karl_jones likes this.
  4. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    8,291
    LocalizationSettings.SelectedLocaleChanged is all you need. Also this gets called after tables have been preloaded, so if you are preloading your StringTables then everything should be immediately available.

    If you are using LocalizedStringTable then you dont need to use SelectedLocaleChanged, it has its own event you can use just for the String Table https://docs.unity3d.com/Packages/c...ne_Localization_LocalizedTable_2_TableChanged

    Alternatively if you are just using SelectedLocaleChanged then you can call in directly and get the table without using a LocalizedStringTable https://docs.unity3d.com/Packages/c...bleReference_UnityEngine_Localization_Locale_

    If you just have 1 table then LocalizedstringTable is the simplest approach
     
    Last edited: Feb 6, 2021
  5. nexprime

    nexprime

    Joined:
    Nov 15, 2016
    Posts:
    5
    That's perfect, thanks for another great explanation Karl!
     
    karl_jones likes this.
  6. JulianMC

    JulianMC

    Joined:
    Dec 30, 2019
    Posts:
    26

    Hi, what do you mean by:
    The cache part.