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?
using System;
namespace UnityEngine.Localization.Settings
{
/// <summary>
/// Uses the Player Prefs to keep track of the last used locale.
/// Whenever the locale is changed, the new Locale is recorded in the Player prefs.
/// </summary>
[Serializable]
public class PlayerPrefLocaleSelector : IStartupLocaleSelector, IInitialize
{
[SerializeField]
string m_PlayerPreferenceKey = "selected-locale";
/// <summary>
/// The Player Pref key to use.
/// </summary>
public string PlayerPreferenceKey
{
get => m_PlayerPreferenceKey;
set => m_PlayerPreferenceKey = value;
}
/// <summary>
/// Registers a callback to <see cref="LocalizationSettings.SelectedLocaleChanged"/> in order to save changes made to the Locale.
/// </summary>
/// <param name="settings"></param>
public void PostInitialization(LocalizationSettings settings)
{
if (Application.isPlaying)
{
// Record the new selected locale so it can persist between runs
var selectedLocale = settings.GetSelectedLocale();
if (selectedLocale != null)
PlayerPrefs.SetString(PlayerPreferenceKey, selectedLocale.Identifier.Code);
}
}
/// <summary>
/// Returns the last locale set or null if no value has been recorded yet.
/// </summary>
/// <param name="availableLocales"></param>
/// <returns></returns>
public Locale GetStartupLocale(ILocalesProvider availableLocales)
{
if (PlayerPrefs.HasKey(PlayerPreferenceKey))
{
var code = PlayerPrefs.GetString(PlayerPreferenceKey);
if (!string.IsNullOrEmpty(code))
{
return availableLocales.GetLocale(code);
}
}
// No locale could be found.
return null;
}
}
}
It will update the preference whenever the locale is changed.
what would happen if null is returned?
Does it mean localizaton package will first select a locale based on device (for example if device is hebrew, and there is a locale for hebrew) or english when only available local is english?
Another question is, how can i simulate real device behavior in editor for example, i want to test if a an app is first time run on a device with lets say some other language like russian or chinese (so i know what would happen in first time use cases)