SamsonGame/Assets/Scripts/Game/UI/ChangeLanguage.cs

63 lines
1.7 KiB
C#

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<Dropdown>();
if (dropdown == null)
return;
string currentLanguage = LocalizationManager.CurrentLanguage;
if (LocalizationManager.Sources.Count == 0)
LocalizationManager.UpdateSources();
List<string> 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<Dropdown>();
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<Sprite>(translation);
}
}