123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990 |
- using TMPro;
- using UnityEngine;
- namespace ICTS.Localization
- {
- public class UILocalize : MonoBehaviour
- {
- /// <summary>
- /// Localization key.
- /// </summary>
- public bool Upper;
- public string key;
- string mLanguage;
- bool mStarted = false;
- /// <summary>
- /// This function is called by the Localization manager via a broadcast SendMessage.
- /// </summary>
- void OnLocalize(Localization loc) { if (mLanguage != loc.currentLanguage) Localize(); }
- /// <summary>
- /// Localize the widget on enable, but only if it has been started already.
- /// </summary>
- void OnEnable() { if (mStarted && Localization.instance != null) Localize(); }
- /// <summary>
- /// Localize the widget on start.
- /// </summary>
- void Start()
- {
- mStarted = true;
- if (Localization.instance != null) Localize();
- }
- /// <summary>
- /// Force-localize the widget.
- /// </summary>
- public void Localize()
- {
- Localization loc = Localization.instance;
- TMP_Text lbl = GetComponent<TMP_Text>();
-
- // If no localization key has been specified, use the label's text as the key
- if (string.IsNullOrEmpty(mLanguage) && string.IsNullOrEmpty(key) && lbl != null) key = lbl.text;
- // If we still don't have a key, leave the value as blank
- string val = string.IsNullOrEmpty(key) ? "" : loc.Get(key);
- if (lbl != null)
- {
- // If this is a label used by input, we should localize its default value instead
- TMP_InputField input = transform.GetComponent<TMP_InputField>();
- if (input != null && input == lbl)
- {
- if (Upper)
- {
- input.text = val.ToUpper();
- }
- else
- {
- input.text = val;
- }
- input.text = val;
- }
- else
- {
- if (Upper)
- {
- lbl.text = val.ToUpper();
- }
- else
- {
- lbl.text = val;
- }
- }
- }
- mLanguage = loc.currentLanguage;
- }
- }
- }
|