Search Unity

LocalizationSettings.SelectedLocale is null in build

Discussion in 'Localization Tools' started by Oneiros90, Jun 1, 2022.

  1. Oneiros90

    Oneiros90

    Joined:
    Apr 29, 2014
    Posts:
    78
    Hello, I'm having a problem with Unity Localization 1.0.5 (Unity 2021.2.19f1).
    I have two locales ("en-US" and "it-T" renamed to just "en" and "it") and only one strings table.
    The project runs with no problems in the editor but it doesn't when I build it. Particularly,
    LocalizationSettings.SelectedLocale
    returns null.

    These are the localization settings:
    upload_2022-6-1_10-35-15.png

    In my script, I call first of all
    Code (CSharp):
    1. await LocalizationSettings.InitializationOperation.Task;
    I also tried to build addressable assets (Window/Asset Management/Addressable Assets/Groups -> Build/New Build/Default Build Script) but nothing changed.

    In my logs from the build I see
    Code (CSharp):
    1. The Locale 'it-IT' is not available, however the parent locale 'it' is available.
    But I guess it is not a problem...

    Any help?
     
  2. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    8,281
    Can you try updating to 1.3.1?
    You may need to edit the manifest.json file in the packages folder if it's not visible in the package manager.
     
  3. Oneiros90

    Oneiros90

    Joined:
    Apr 29, 2014
    Posts:
    78
    Oh, I was not aware that I was using an old version! The 1.3.1 was not visible in the package manager.
    Now I updated but that didn't solve my issue, unfortunately same as before
     
  4. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    8,281
  5. Oneiros90

    Oneiros90

    Joined:
    Apr 29, 2014
    Posts:
    78
    I'm sorry, I just realized that I misinterpreted my problem. SelectedLocale was not null, it had the expected value.
    My problem was that I was comparing it with other locales (saved as serialized references in a scriptable object) using the == operator. For some reason, this kind of comparison is not valid in builds (I guess locales have new instances in this case?).

    So I solved just by comparing the identifier of the locales, not the instances :)
    Thanks for the help @karl_jones

    TLDR: DON'T compare locales like this:
    Code (CSharp):
    1. locale1 == locale2
     
  6. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    8,281
    Yes, they are built into asset bundles. Don't hold references to the locale assets in your scripts or it will create duplicates. Use the LocaleIdentifier struct if you want a field in the inspector.