Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

How to get localized strings from other locales?

Discussion in 'Localization Tools' started by pryankster, Oct 28, 2020.

  1. pryankster

    pryankster

    Joined:
    Aug 6, 2015
    Posts:
    24
    This is probably simple, but I'm making a language dropdown, and I want each language in the dropdown to be represented in that language, i.e.: "English", "Español", "(elided japanese characters)" (Following the documented example, it puts locale.name in the dropdown, which seems to always be English?)

    My initial thought was to make a key "LANGUAGE", and then each translation would put the word/phrase for its own language. But how do I get access to a translated string from a different locale?

    (pseudo code)
    Code (CSharp):
    1. for ( var i = 0; i < LocalizationSettings.AvailableLocales.Locales.Count; i ++) {
    2.     var locale = LocalizationSettings.AvailableLocales.Locales[i];
    3.     // the GetLocalizedString() call below doesn't exist ...
    4.     options.Add( new OptionData( locale.GetLocalizedString( "LANGUAGE" ) );
    5. }
    Maybe something like this, instead?
    Code (CSharp):
    1. [SerializeField] LocalizedString languageString;
    2. ...
    3. for ( var i = 0; i < LocalizationSettings.AvailableLocales.Locales.Count; i ++) {
    4.     var locale = LocalizationSettings.AvailableLocales.Locales[i];
    5.     // the GetLocalizedStringFromLocale() call below doesn't exist ...
    6.     options.Add( new OptionData( languageString.GetLocalizedStringFromLocale( locale ));
    7. }

    -- pryankster
     
  2. pryankster

    pryankster

    Joined:
    Aug 6, 2015
    Posts:
    24
    (Admins: I could not post my original message because I included japanese characters in the text ... when I deleted the characters, I was able to post ... seems silly for a Localization message board )
     
    Mashimaro7 likes this.
  3. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    8,395
    Hi,
    By default, Each locale will have access to the English name and the native name through the CultureInfo.

    locale.Identifier.CultureInfo.EnglishName
    locale.Identifier.CultureInfo.NativeName

    If you want a name that is localized to the currently selected locale then you would need to provide these translations in a string table collection. You could then get to them like so:

    Code (csharp):
    1. for (var i = 0; i < LocalizationSettings.AvailableLocales.Locales.Count; i++)
    2. {
    3.    var locale = LocalizationSettings.AvailableLocales.Locales[i];
    4.    var translatedNamed = LocalizationSettings.StringDatabase.GetLocalizedStringAsync("Language Names", locale.Identifier.CultureInfo.EnglishName);
    5.    // wait for the translated text operation to complete
    6. }
    If you want to access a value that is specfic to a locale you can actually pass in the locale into the various methods such as GetLocalizedStringAsync. This lets you get a translated value for that locale instead of using the currently selected locale.
    I think this is some of the anti-spam meseasures that the forum system uses for newer accounts. Sadly it does not recognize the context of the forum section.
     
  4. pryankster

    pryankster

    Joined:
    Aug 6, 2015
    Posts:
    24
    Thanks for the quick response! I was coming back to post that I figured it out, only to see that you had answered the question! (I did have a bit of trouble because I hadn't set a default string table, so LocalizationSettings.StringDatabase returned an empty ref.)

    -- pryankster
     
    karl_jones likes this.
  5. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    8,395
    Oh yes we have a bug report to improve the default table stuff.
    Glad it's working