summaryrefslogtreecommitdiff
path: root/Thronefall_1_57/Decompile/I2.Loc/LocalizeDropdown.cs
blob: 57fd32852392269533e8650f0af5bb370668858c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
using System.Collections.Generic;
using TMPro;
using UnityEngine;
using UnityEngine.UI;

namespace I2.Loc;

[AddComponentMenu("I2/Localization/Localize Dropdown")]
public class LocalizeDropdown : MonoBehaviour
{
	public List<string> _Terms = new List<string>();

	public void Start()
	{
		LocalizationManager.OnLocalizeEvent += OnLocalize;
		OnLocalize();
	}

	public void OnDestroy()
	{
		LocalizationManager.OnLocalizeEvent -= OnLocalize;
	}

	private void OnEnable()
	{
		if (_Terms.Count == 0)
		{
			FillValues();
		}
		OnLocalize();
	}

	public void OnLocalize()
	{
		if (base.enabled && !(base.gameObject == null) && base.gameObject.activeInHierarchy && !string.IsNullOrEmpty(LocalizationManager.CurrentLanguage))
		{
			UpdateLocalization();
		}
	}

	private void FillValues()
	{
		Dropdown component = GetComponent<Dropdown>();
		if (component == null && I2Utils.IsPlaying())
		{
			FillValuesTMPro();
			return;
		}
		foreach (Dropdown.OptionData option in component.options)
		{
			_Terms.Add(option.text);
		}
	}

	public void UpdateLocalization()
	{
		Dropdown component = GetComponent<Dropdown>();
		if (component == null)
		{
			UpdateLocalizationTMPro();
			return;
		}
		component.options.Clear();
		foreach (string term in _Terms)
		{
			string text = TextTranslator.Translate(term);
			component.options.Add(new Dropdown.OptionData(text));
		}
		component.RefreshShownValue();
	}

	public void UpdateLocalizationTMPro()
	{
		TMP_Dropdown component = GetComponent<TMP_Dropdown>();
		if (component == null)
		{
			return;
		}
		component.options.Clear();
		foreach (string term in _Terms)
		{
			string text = TextTranslator.Translate(term);
			component.options.Add(new TMP_Dropdown.OptionData(text));
		}
		component.RefreshShownValue();
	}

	private void FillValuesTMPro()
	{
		TMP_Dropdown component = GetComponent<TMP_Dropdown>();
		if (component == null)
		{
			return;
		}
		foreach (TMP_Dropdown.OptionData option in component.options)
		{
			_Terms.Add(option.text);
		}
	}
}