using I2.Loc; using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; public class ChangeLanguage : MonoBehaviour { [SerializeField] private Text activeItemText; [SerializeField] private Image activeItemImg; void OnEnable() { var dropdown = GetComponent(); if (dropdown == null) return; string currentLanguage = LocalizationManager.CurrentLanguage; if (LocalizationManager.Sources.Count == 0) LocalizationManager.UpdateSources(); List languages = LocalizationManager.GetAllLanguages(); // Fill the dropdown elements dropdown.ClearOptions(); dropdown.AddOptions(languages); dropdown.value = languages.IndexOf(currentLanguage); dropdown.onValueChanged.RemoveListener(OnValueChanged); dropdown.onValueChanged.AddListener(OnValueChanged); UpdateActiveItem(); } private void Start() { UpdateActiveItem(); } private void OnValueChanged(int index) { var dropdown = GetComponent(); if (index < 0) { index = 0; dropdown.value = index; } LocalizationManager.CurrentLanguage = dropdown.options[index].text; UpdateActiveItem(); } private void UpdateActiveItem() { activeItemText.text = LocalizationManager.GetTranslation(LocalizationManager.CurrentLanguage); if (LocalizationManager.TryGetTranslation("FlagSprite", out string translation, true, 0, true, false, null, LocalizationManager.CurrentLanguage, false)) activeItemImg.sprite = LocalizationManager.GetTranslatedObject(translation); } }