Search Unity

Question Changing locales

Discussion in 'Scripting' started by Robin-Jubber, Jan 5, 2023.

  1. Robin-Jubber

    Robin-Jubber

    Joined:
    Sep 7, 2012
    Posts:
    28
    Hi - I'm having trouble getting localized text to change. I have a table with just "hello" and "goodbye" in it as keys, for testing. Both words are translated into a number of languages, e.g. french. I've set the locale to 3 (french) and it reports as french with a debug.log statement. I can also set french in the Localization Scene Controls for the localization table (there is only one in the project)

    private IEnumerator Start()
    {
    // Wait for the table to load asynchronously
    var table_loaded = localized_string_table.GetTable();
    yield return table_loaded;
    current_string_table = table_loaded;

    // This can be used to force locale, although it doesn't change the string return. Not sure why.
    LocalizationSettings.SelectedLocale = LocalizationSettings.AvailableLocales.Locales[3];
    }

    Here I change the contents of a text mesh pro UI item - but Debug.Log also shows the same content. It is always "HELLO" - the English translation. It never shows "BONJOUR", despite this being in the table, as the third language. It only shows English no matter which locale I choose.

    void Display()
    {
    // put text on screen - tmp ui element.
    var str = current_string_table["hello"].LocalizedValue; // or .GetLocalizedString();
    dialogue_text.text = str;
    }

    I'm sure I'm making a basic mistake here. Unity version is 2022.1.23f1
     
  2. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    8,286
    Do you update current_string_table when the language changes? It's possible to have multiple languages loaded, a table won't change itself when you change the language. You need to call GetTable again when the language changes. The simplest thing to do is to use the Table changed event which will be called automatically when the language changes.
     
  3. Robin-Jubber

    Robin-Jubber

    Joined:
    Sep 7, 2012
    Posts:
    28
    Hi Karl - thanks for the reply!

    In the end I used some code I found in another thread you replied to, applying your advice about checking for the locale changing, after I didn't have any luck with RegisterChangeHandler (it seems to not be part of the LocalizedStringTable)

    For anybody else who needs the code -
    In Start, after getting the table as above, add the following line -
    LocalizationSettings.SelectedLocaleChanged += Changed_Locale_Function;

    then the following two functions

    // Async functions to reload the table if locale changes - e.g. from the settings screen.
    private void Changed_Locale_Function( Locale locale)
    {
    StartCoroutine( On_Changed_Locale( locale ) );
    }

    private IEnumerator On_Changed_Locale( Locale locale )
    {
    var loadingOperation = LocalizationSettings.StringDatabase.GetTableAsync( localized_string_table.TableReference );
    yield return loadingOperation;

    if( loadingOperation.Status == AsyncOperationStatus.Succeeded )
    current_string_table = loadingOperation.Result;
    else
    Debug.Log( "Could not load String Table\n" + loadingOperation.OperationException.ToString() );
    }
     
    karl_jones likes this.