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

Saving SelectedLocale with PlayerPrefs

Discussion in 'Localization Tools' started by irinemlc, May 18, 2021.

  1. irinemlc

    irinemlc

    Joined:
    Feb 5, 2020
    Posts:
    2
    Hello!

    I'm new to programming, and I'm stuck with saving the chosen language.

    I'm using the Localization Package. And I'd like to save the preferable language using PlayerPrefs.

    I've found a script for changing the language within the game via dropdown, and I started adding PlayerPrefs. But when I'm trying to retrieve the value in another script, I have an error "CS0029: Cannot implicitly convert type "int" to "UnityEngine.Localization.Locale"."

    How can I save the language preference properly?

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.Localization.Settings;
    5. using TMPro;
    6.  
    7. public class LocaleDropdown : MonoBehaviour
    8. {
    9.     public TMP_Dropdown dropdown;
    10.  
    11.  
    12.     IEnumerator Start()
    13.     {
    14.         // Wait for the localization system to initialize, loading Locales, preloading etc.
    15.         yield return LocalizationSettings.InitializationOperation;
    16.  
    17.         // Generate list of available Locales
    18.         var options = new List<TMP_Dropdown.OptionData>();
    19.         int selected = 0;
    20.         for (int i = 0; i < LocalizationSettings.AvailableLocales.Locales.Count; ++i)
    21.         {
    22.             var locale = LocalizationSettings.AvailableLocales.Locales[i];
    23.             if (LocalizationSettings.SelectedLocale == locale)
    24.                 selected = i;
    25.             options.Add(new TMP_Dropdown.OptionData(locale.name));
    26.            
    27.            
    28.         }
    29.         dropdown.options = options;
    30.      
    31.  
    32.         dropdown.value = selected;
    33.         dropdown.onValueChanged.AddListener(LocaleSelected);
    34.     }
    35.  
    36.     public static void LocaleSelected(int index)
    37.     {
    38.         LocalizationSettings.SelectedLocale = LocalizationSettings.AvailableLocales.Locales[index];
    39.         PlayerPrefs.SetInt("language", index);
    40.         PlayerPrefs.Save();
    41.         Debug.Log(PlayerPrefs.GetInt("language") + "Language Saved!");
    42.     }
    43. }
    and the second script

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.Localization.Settings;
    3.  
    4. public class LocalizationManager : MonoBehaviour
    5. {
    6.     void Awake()
    7.     {
    8.         if (PlayerPrefs.HasKey("language"))
    9.         {
    10.             LocalizationSettings.SelectedLocale = PlayerPrefs.GetInt("language");
    11.         }
    12.         else if (Application.systemLanguage == SystemLanguage.Russian)
    13.         {
    14.             PlayerPrefs.SetInt("language", 1);
    15.         }
    16.         else
    17.         {
    18.             PlayerPrefs.SetInt("language", 0);
    19.         }
    20.     }
    21. }
     
  2. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    7,845
    Hey.
    We already have a system to do this. In the locale selectors you can add a Player Pref selector which will save the last selected value.
     
  3. irinemlc

    irinemlc

    Joined:
    Feb 5, 2020
    Posts:
    2
    Thank you!
    Could you please point out how to add it? I have a 2021.1 Unity version and there's no playerprefs, although I saw screenshots with a Player Preference Key

    My version
    upload_2021-5-19_19-59-33.png

    Found screenshot
     
  4. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    7,845
    Press the + button at the bottom right of the screen shot and you should get a list of different types that can be added.
     
    PokerDawg likes this.
  5. ejjht

    ejjht

    Joined:
    Jan 22, 2020
    Posts:
    10
    How do you save that player pref?

    Like this?

    PlayerPrefs.SetString("selected-locale", LocalizationSettings.SelectedLocale.LocaleName);

    My other question is, how do you load it? or will it be automatically loaded?

    I'm new using this package, thank you in advance :)
     
  6. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    7,845
    You don't need to save it or load it, it will all be handled by the system.
    When you set the LocalizationSettings.SelectedLocale it will save the value and when the localization system initializes and picks the startup language it will read the value.
     
    PokerDawg likes this.
  7. ejjht

    ejjht

    Joined:
    Jan 22, 2020
    Posts:
    10
    Yes, I'm using that in the fuction "OnSelectionChanged", but once I run the game again, it doesn't change to my last selected local

    Code (CSharp):
    1. using System.Collections.Generic;
    2. using UnityEngine.Localization.Settings;
    3. using UnityEngine.ResourceManagement.AsyncOperations;
    4. using TMPro;
    5. namespace UnityEngine.Localization.Samples
    6. {
    7.     public class LanguageSelectionMenuUGUIDropdown : MonoBehaviour
    8.     {
    9.        
    10.         public TMP_Dropdown m_Dropdown;
    11.         AsyncOperationHandle m_InitializeOperation;
    12.  
    13.         void Start()
    14.         {
    15.             // First we setup the dropdown component.
    16.             m_Dropdown = GetComponent<TMP_Dropdown>();
    17.             m_Dropdown.onValueChanged.AddListener(OnSelectionChanged);
    18.  
    19.             // Clear the options an add a loading message while we wait for the localization system to initialize.
    20.             m_Dropdown.ClearOptions();
    21.             m_Dropdown.options.Add(new TMP_Dropdown.OptionData("Loading..."));
    22.             m_Dropdown.interactable = false;
    23.  
    24.             // SelectedLocaleAsync will ensure that the locales have been initialized and a locale has been selected.
    25.             m_InitializeOperation = LocalizationSettings.SelectedLocaleAsync;
    26.  
    27.             if (m_InitializeOperation.IsDone)
    28.             {
    29.                 InitializeCompleted(m_InitializeOperation);
    30.             }
    31.             else
    32.             {
    33.                 m_InitializeOperation.Completed += InitializeCompleted;
    34.             }
    35.         }
    36.  
    37.         void InitializeCompleted(AsyncOperationHandle obj)
    38.         {
    39.             // Create an option in the dropdown for each Locale
    40.             var options = new List<string>();
    41.  
    42.             int selectedOption = 0;
    43.  
    44.             var locales = LocalizationSettings.AvailableLocales.Locales;
    45.  
    46.             for (int i = 0; i < locales.Count; ++i)
    47.             {
    48.                 var locale = locales[i];
    49.  
    50.                 if (LocalizationSettings.SelectedLocale == locale)
    51.                     selectedOption = i;
    52.  
    53.                 var displayName = locales[i].Identifier.CultureInfo != null ? locales[i].Identifier.CultureInfo.NativeName : locales[i].ToString();
    54.              
    55.                 options.Add(displayName);
    56.             }
    57.  
    58.             // If we have no Locales then something may have gone wrong.
    59.             if (options.Count == 0)
    60.             {
    61.                 options.Add("No Locales Available");
    62.  
    63.                 m_Dropdown.interactable = false;
    64.             }
    65.             else
    66.             {
    67.                 m_Dropdown.interactable = true;
    68.             }
    69.  
    70.             m_Dropdown.ClearOptions();
    71.  
    72.             m_Dropdown.AddOptions(options);
    73.  
    74.             m_Dropdown.SetValueWithoutNotify(selectedOption);
    75.  
    76.             LocalizationSettings.SelectedLocaleChanged += LocalizationSettings_SelectedLocaleChanged;    
    77.         }
    78.  
    79.         void OnSelectionChanged(int index)
    80.         {
    81.             // Unsubscribe from SelectedLocaleChanged so we don't get an unnecessary callback from the change we are about to make.
    82.             LocalizationSettings.SelectedLocaleChanged -= LocalizationSettings_SelectedLocaleChanged;
    83.  
    84.             var locale = LocalizationSettings.AvailableLocales.Locales[index];
    85.  
    86.             Debug.Log("current Selected local " + LocalizationSettings.SelectedLocale.LocaleName);
    87.  
    88.             //Here I'm using it
    89.             LocalizationSettings.SelectedLocale = locale;
    90.  
    91.             Debug.Log("new Selected local "+ LocalizationSettings.SelectedLocale.LocaleName);
    92.  
    93.             // Resubscribe to SelectedLocaleChanged so that we can stay in sync with changes that may be made by other scripts.
    94.             LocalizationSettings.SelectedLocaleChanged += LocalizationSettings_SelectedLocaleChanged;
    95.         }
    96.  
    97.         void LocalizationSettings_SelectedLocaleChanged(Locale locale)
    98.         {
    99.             // We need to update the dropdown selection to match.
    100.             var selectedIndex = LocalizationSettings.AvailableLocales.Locales.IndexOf(locale);
    101.  
    102.             m_Dropdown.SetValueWithoutNotify(selectedIndex);
    103.         }
    104.     }
    105. }
    106.  
     
  8. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    7,845
    Is the player pref selector at the top of the list in the settings? It needs to be first or another selector will be used before it.
     
  9. ejjht

    ejjht

    Joined:
    Jan 22, 2020
    Posts:
    10
    Is this how it's supposed to be?
     
  10. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    7,845
    Yes that's correct. Is it not working? Are you using 1.3.2?
     
  11. ejjht

    ejjht

    Joined:
    Jan 22, 2020
    Posts:
    10
    Yes, there was another script causing a conflict, now it works, thanks!
     
    karl_jones likes this.
  12. santiagokim00

    santiagokim00

    Joined:
    Aug 29, 2023
    Posts:
    8
    Excuse me, how can I save the state on localization when changing scene?
     
  13. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    7,845
    Hi,
    Which state are you trying to save? The selected locale state should persist when changing a scene.
    What happens when you change the scene?
     
  14. santiagokim00

    santiagokim00

    Joined:
    Aug 29, 2023
    Posts:
    8
    I'm trying to save the state of the setting of the user where the user selected a preferred language, but I don't know how to save it. When I'm changing the scene the selected language is resetting to the default language.
     
  15. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    7,845
    Try adding the player preferences locale selector to the locale selectors in the localization settings. This will save the state for you. Move it to the top of the list.
     
  16. santiagokim00

    santiagokim00

    Joined:
    Aug 29, 2023
    Posts:
    8
    Can I ask how to set the playerpref for localizationSettings.SelectedLocale?
     
  17. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    7,845
    If you use the player preferences locale selector then it will set it for you automatically whenever you change the selected locale.
     
  18. santiagokim00

    santiagokim00

    Joined:
    Aug 29, 2023
    Posts:
    8
    So it will no more needed for the c# script? it just automatically save the selected language? There's no need for code when the user click language? Sorry for having too many questions? I'm new in using Unity hehe
     
  19. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    7,845
    Yes if you want it to remember the last language chosen. No code needed ;)
     
  20. santiagokim00

    santiagokim00

    Joined:
    Aug 29, 2023
    Posts:
    8
    How I'm going to set this if I want this to apply to other scenes? It keeps on going back to default
     
  21. santiagokim00

    santiagokim00

    Joined:
    Aug 29, 2023
    Posts:
    8
    Hey, man. Thanks, it works. It is changing the language when the user selected a language, it save the the selected language. But there is still a little thing to fix when changing the scene. It doesn't changing the text.
     
  22. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    7,845
  23. santiagokim00

    santiagokim00

    Joined:
    Aug 29, 2023
    Posts:
    8
  24. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    7,845
    The guide just gives you some bits to get started. You don't have to do everything.
     
  25. santiagokim00

    santiagokim00

    Joined:
    Aug 29, 2023
    Posts:
    8
    I folow the instructions but I have encountered an error. How can I fix this? upload_2023-10-1_21-21-46.png
     
  26. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    7,845