In early start app and in method Start() available locales is 0

Hello. I wants get all available locales in Start() method, but it returned me 0. How i can fix this problem?

public class Localization : MonoBehaviour
{
    public static Localization Instance;

    private void Awake()
    {
        if (Instance == null)
        {
            DontDestroyOnLoad(gameObject);
            Instance = this;
        }
        else if (Instance != this)
        {
            Destroy (gameObject);
        }
    }

    private void Start()
    {
        Debug.Log("Count: " + GetAvailableLocales().Count);
        //SetLocale(Instance.GetAvailableLocales().Where(x => x.Identifier.Code == TransferData.Instance.UserData.Settings.LocaleCode).First());

    }

    public string GetUIString(string tableArgument) =>  LocalizationSettings.StringDatabase.GetLocalizedString("UI", tableArgument);

    public List<Locale> GetAvailableLocales() => LocalizationSettings.Instance.GetAvailableLocales().Locales;
    public Locale GetCurrentLocale() => LocalizationSettings.SelectedLocale;
    public void SetLocale(Locale locale) => LocalizationSettings.SelectedLocale = locale;
}

If I want to get the available languages later, let’s say when I open the settings, it works fine, it returns 2 available languages.

Hi,
When the app first starts you need to wait for the initialization operation to complete.
https://docs.unity3d.com/Packages/com.unity.localization@1.3/api/UnityEngine.Localization.Settings.LocalizationSettings.html#UnityEngine_Localization_Settings_LocalizationSettings_InitializationOperation

Thanks!

1 Like