Search Unity

Bug I'm not able to create a Drop Down menu with the languages

Discussion in 'Localization Tools' started by djovercome, Aug 8, 2022.

  1. djovercome

    djovercome

    Joined:
    Jun 4, 2022
    Posts:
    3
    This is my code:

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEngine.Localization;
    using UnityEngine.UI;


    public class LocaleDropdown : MonoBehaviour
    {
    public Dropdown dropdown;

    IEnumerator Start()
    {
    // Wait for the localization system to initialize, loading Locales, preloading etc.
    yield return LocalizationSettings.InitializationOperation;

    // Generate list of available Locales
    var options = new List<Dropdown.OptionData>();
    int selected = 0;
    for(int i = 0; i < LocalizationSettings.AvailableLocales.Locales.Count; ++i)
    {
    var locale = LocalizationSettings.AvailableLocales.Locales;
    if (LocalizationSettings.SelectedLocale == locale)
    selected = i;
    options.Add(new Dropdown.OptionData(locale.name));
    }
    dropdown.options = options;

    dropdown.value = selected;
    dropdown.onValueChanged.AddListener(LocaleSelected);
    }

    static void LocaleSelected(int index)
    {
    LocalizationSettings.SelectedLocale = LocalizationSettings.AvailableLocales.Locales[index];
    }
    }


    my error:
    Assets\Scripts\Globals\LocaleDropdown.cs(34,47): error CS0103: The name 'LocalizationSettings' does not exist in the current context
     

    Attached Files:

  2. salman_unity26

    salman_unity26

    Joined:
    Jul 30, 2022
    Posts:
    1
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEngine.Localization.Settings;
    using UnityEngine.UI;

    public class LocaleDropdown : MonoBehaviour
    {
    public Dropdown dropdown;

    IEnumerator Start()
    {
    // Wait for the localization system to initialize, loading Locales, preloading etc.
    yield return LocalizationSettings.InitializationOperation;

    // Generate list of available Locales
    var options = new List<Dropdown.OptionData>();
    int selected = 0;
    for (int i = 0; i < LocalizationSettings.AvailableLocales.Locales.Count; ++i)
    {
    var locale = LocalizationSettings.AvailableLocales.Locales;
    if (LocalizationSettings.SelectedLocale == locale)
    selected = i;
    options.Add(new Dropdown.OptionData(locale.name));
    }
    dropdown.options = options;

    dropdown.value = selected;
    dropdown.onValueChanged.AddListener(LocaleSelected);
    }

    static void LocaleSelected(int index)
    {
    LocalizationSettings.SelectedLocale = LocalizationSettings.AvailableLocales.Locales[index];

    }
    }
     
  3. SushiiStellar

    SushiiStellar

    Joined:
    Jan 17, 2021
    Posts:
    2
    If anyone wants a script that uses TMP_Dropdown instead of the classic Dropdown, here it is:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using TMPro;
    4. using UnityEngine;
    5. using UnityEngine.Localization;
    6. using UnityEngine.Localization.Settings;
    7.  
    8. namespace Localization
    9. {
    10.     public class LocaleDropdown : MonoBehaviour
    11.     {
    12.         [SerializeField] private TMP_Dropdown _localesDropdown;
    13.  
    14.         private IEnumerator Start()
    15.         {
    16.             yield return LocalizationSettings.InitializationOperation;
    17.  
    18.             List<TMP_Dropdown.OptionData> options = new List<TMP_Dropdown.OptionData>();
    19.             int selectedLocale = 0; //TODO: Store the selected locale in the player prefs
    20.            
    21.             for(int i = 0; i < LocalizationSettings.AvailableLocales.Locales.Count; i++)
    22.             {
    23.                 Locale locale = LocalizationSettings.AvailableLocales.Locales[i];
    24.                 options.Add(new TMP_Dropdown.OptionData(locale.name));
    25.                 if (locale == LocalizationSettings.SelectedLocale) selectedLocale = i;
    26.             }
    27.            
    28.             _localesDropdown.options = options;
    29.             _localesDropdown.value = selectedLocale;
    30.             _localesDropdown.onValueChanged.AddListener(OnLocaleChanged);
    31.         }
    32.  
    33.         private static void OnLocaleChanged(int index)
    34.         {
    35.             LocalizationSettings.SelectedLocale = LocalizationSettings.AvailableLocales.Locales[index];
    36.         }
    37.     }
    38. }
     
    karl_jones likes this.