Control which language the application starts in.
At startup, the selectors in the Localization settings run in order and the first one that returns an enabled locale decides the language. This example writes a selector that restores the user’s saved choice, with the same pattern as the built-in Player Pref selector. Use it as a starting point to store the choice in a save file, a cloud profile, or your own settings system.
Before you start, make sure you have the following:
Implement IStartupLocaleSelector to choose the locale, and IStartupLocaleInitialize to run one-time setup after initialization. In this example, the setup subscribes to locale changes so the selector saves them:
[Serializable]
public class SaveFileLocaleSelector : IStartupLocaleSelector, IStartupLocaleInitialize
{
[SerializeField] string m_PreferenceKey = "preferred-language";
public Locale GetStartupLocale(LocalizationSettings settings)
{
// Returning null lets the next selector in the list decide.
var code = PlayerPrefs.GetString(m_PreferenceKey);
return string.IsNullOrEmpty(code) ? null : settings.GetLocale(new LocaleIdentifier(code));
}
public void PostInitialize(LocalizationSettings settings)
{
// Store later language changes so the next launch starts in the player's language.
LocalizationSettings.SelectedLocaleChanged += locale => PlayerPrefs.SetString(m_PreferenceKey, locale.Code);
}
}
The selector class must follow two constraints:
[Serializable] attribute. The settings serialize the selector list, and serialized fields, such as m_PreferenceKey, appear as options in the settings UI.null when it has no locale to offer. A null return passes the decision to the next selector, and localization treats a disabled locale the same way.To register your selector:
This selector follows the same pattern as the built-in Player Pref Locale Selector, but writes the choice to a file instead of PlayerPrefs. To verify that it persists the choice: