- Home /
Set locale on launch based on player prefs
I wan't to set the locale on launch, based on the settings the player configured the last time the app was used. The information is stored in player prefs and read on launch. If I try to set the locale however I get the following error message:
Locales PreloadOperation has not been initialized, can not return the available locales. UnityEngine.Localization.Settings.LocalesProvider:get_Locales () SettingsManager:SetLanguageSettings (string) (at Assets/Scripts/Settings/SettingsManager.cs:40) SettingsManager:InitializeSettings () (at Assets/Scripts/Settings/SettingsManager.cs:18) GameManager:Start () (at Assets/Scripts/GameManager.cs:17)
Here is my code for setting the locale:
public void SetLanguageSettings(string localeIdentifierCode) {
if (LocalizationSettings.AvailableLocales == null) return;
var locales = LocalizationSettings.AvailableLocales.Locales;
if (locales == null) return;
var selectedLocale = locales.Find(locale => locale.Identifier.Code == localeIdentifierCode);
LocalizationSettings.SelectedLocale = selectedLocale;
// set player prefs using the selected localeIdentifierCode
PlayerPrefs.SetString(PlayerPrefsKeys.Language.ToString(), localeIdentifierCode);
}
Is there anything wrong with the way I set the locale?
I am using Unity 2020.3.24f and the localization package version 1.0.5.
Answer by christopher_hof · Dec 13, 2021 at 02:24 PM
Calling LocalizationSettings.InitializationOperation.WaitForCompletion();
before accessing LocalizationSettings
seemed to fix it.