Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Get Localized String from Key in 1 line

Discussion in 'Localization Tools' started by dimitris_baud, Oct 22, 2020.

  1. dimitris_baud

    dimitris_baud

    Joined:
    Jan 15, 2018
    Posts:
    29
    Is there a way using this package to get a localised string using 1 line of code, using all the default databases, string tables, etc., synchronously and without having to keep explicit references to assets?

    Something like: `button.text = LocallisedString.ForKey("MY_KEY");`

    Thank you
     
    Indrit_Vaka and Noxalus like this.
  2. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    7,787
    If you mark the string table as preload then it is possible providing the preloading operation has completed.

    LocalizationSettings.StringDatabase.GetStringAsync("My Key").Result.

    Because we use Addressables and tables can be local or even hosted remotely it's hard to provide a pure synchronous API when our dependencies dont have one themselves.
     
    kyteh9925 likes this.
  3. dimitris_baud

    dimitris_baud

    Joined:
    Jan 15, 2018
    Posts:
    29
    Understood. Is there a way to do it in a single line Async?

    Something like: `LocalizedString.ForKey("MY_KEY", (string) => { button.text = string; });`

    Basically what is the most common and recommended way of working with Localised Strings *in code*, the Package examples are more Editor-focused not so much code. At least no that I could find.

    Thank you.
     
  4. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    7,787
    Did you looks at the LoadingStrings samples? They show various ways.

    You can do it in 1 line like so

    Code (csharp):
    1. LocalizationSettings.StringDatabase.GetLocalizedStringAsync("My Key").Completed += result => button.text = result;
    The Completed event will always be called, even if the event has previously completed, so its safe to continue using it.
     
    Indrit_Vaka and dimitris_baud like this.
  5. dimitris_baud

    dimitris_baud

    Joined:
    Jan 15, 2018
    Posts:
    29
    I will have another look at that example, though your sample code, along with some helper/wrapper should be sufficient. Thank you!
     
    karl_jones likes this.
  6. zdolezal

    zdolezal

    Joined:
    Sep 27, 2017
    Posts:
    75
    Btw. where's this option for string tables? I have seen it on some screenshots on the string table definition panel but it's not there anymore, at least I cannot find it.
     
  7. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    7,787
  8. zdolezal

    zdolezal

    Joined:
    Sep 27, 2017
    Posts:
    75
    Yeah, I was missing this is the button: upload_2020-11-17_11-51-52.png
     
    karl_jones likes this.