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

Resolved How to set starting locale?

Discussion in 'Localization Tools' started by kouresearch, Nov 30, 2020.

  1. kouresearch

    kouresearch

    Joined:
    Dec 18, 2018
    Posts:
    8
    In my app, the user can choose the desired locale and save it as PlayerPrefs.
    When starting the app, app tries to change current locale to the saved value. But contrary to expectations, locale is always set as device default, not the saved value.

    Is there some timing-related problem or something? What is the best approach to make app start with the desired locale?
     
  2. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    7,846
    Use the Locale selectors. We already have a player preference one https://docs.unity3d.com/Packages/com.unity.localization@0.9/manual/LocalizationSettings.html
    Click the + icon to add one.
    You can also create a custom one by inheriting from IStartupLocaleSelector
    This is the PlayerPrefOne:

    Code (csharp):
    1.  
    2. using System;
    3.  
    4. namespace UnityEngine.Localization.Settings
    5. {
    6.     /// <summary>
    7.     /// Uses the Player Prefs to keep track of the last used locale.
    8.     /// Whenever the locale is changed, the new Locale is recorded in the Player prefs.
    9.     /// </summary>
    10.     [Serializable]
    11.     public class PlayerPrefLocaleSelector : IStartupLocaleSelector, IInitialize
    12.     {
    13.         [SerializeField]
    14.         string m_PlayerPreferenceKey = "selected-locale";
    15.  
    16.         /// <summary>
    17.         /// The Player Pref key to use.
    18.         /// </summary>
    19.         public string PlayerPreferenceKey
    20.         {
    21.             get => m_PlayerPreferenceKey;
    22.             set => m_PlayerPreferenceKey = value;
    23.         }
    24.  
    25.         /// <summary>
    26.         /// Registers a callback to <see cref="LocalizationSettings.SelectedLocaleChanged"/> in order to save changes made to the Locale.
    27.         /// </summary>
    28.         /// <param name="settings"></param>
    29.         public void PostInitialization(LocalizationSettings settings)
    30.         {
    31.             if (Application.isPlaying)
    32.             {
    33.                 // Record the new selected locale so it can persist between runs
    34.                 var selectedLocale = settings.GetSelectedLocale();
    35.                 if (selectedLocale != null)
    36.                     PlayerPrefs.SetString(PlayerPreferenceKey, selectedLocale.Identifier.Code);
    37.             }
    38.         }
    39.  
    40.         /// <summary>
    41.         /// Returns the last locale set or null if no value has been recorded yet.
    42.         /// </summary>
    43.         /// <param name="availableLocales"></param>
    44.         /// <returns></returns>
    45.         public Locale GetStartupLocale(ILocalesProvider availableLocales)
    46.         {
    47.             if (PlayerPrefs.HasKey(PlayerPreferenceKey))
    48.             {
    49.                 var code = PlayerPrefs.GetString(PlayerPreferenceKey);
    50.                 if (!string.IsNullOrEmpty(code))
    51.                 {
    52.                     return availableLocales.GetLocale(code);
    53.                 }
    54.             }
    55.  
    56.             // No locale could be found.
    57.             return null;
    58.         }
    59.     }
    60. }
    It will update the preference whenever the locale is changed.
     
  3. kouresearch

    kouresearch

    Joined:
    Dec 18, 2018
    Posts:
    8
    Ok, I created custom LocaleSelector class and set it as the first locale selector at the project setting.
    Now it works fine. :) Thanks!
     
    karl_jones likes this.
  4. fuadshahmuradov

    fuadshahmuradov

    Joined:
    Nov 20, 2019
    Posts:
    10
    Also if anyone wants to access it elsewhere, this is the script:
    Code (CSharp):
    1. PlayerPrefs.GetString("selected-locale", "en")