LocalizeDropdown.cs 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. using System;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. namespace I2.Loc
  6. {
  7. [AddComponentMenu("I2/Localization/Localize Dropdown")]
  8. public class LocalizeDropdown : MonoBehaviour
  9. {
  10. public void Start()
  11. {
  12. LocalizationManager.OnLocalizeEvent += this.OnLocalize;
  13. this.OnLocalize();
  14. }
  15. public void OnDestroy()
  16. {
  17. LocalizationManager.OnLocalizeEvent -= this.OnLocalize;
  18. }
  19. private void OnEnable()
  20. {
  21. if (this._Terms.Count == 0)
  22. {
  23. this.FillValues();
  24. }
  25. this.OnLocalize();
  26. }
  27. public void OnLocalize()
  28. {
  29. if (!base.enabled || base.gameObject == null || !base.gameObject.activeInHierarchy)
  30. {
  31. return;
  32. }
  33. if (string.IsNullOrEmpty(LocalizationManager.CurrentLanguage))
  34. {
  35. return;
  36. }
  37. this.UpdateLocalization();
  38. }
  39. private void FillValues()
  40. {
  41. Dropdown component = base.GetComponent<Dropdown>();
  42. if (component == null && I2Utils.IsPlaying())
  43. {
  44. return;
  45. }
  46. foreach (Dropdown.OptionData optionData in component.options)
  47. {
  48. this._Terms.Add(optionData.text);
  49. }
  50. }
  51. public void UpdateLocalization()
  52. {
  53. Dropdown component = base.GetComponent<Dropdown>();
  54. if (component == null)
  55. {
  56. return;
  57. }
  58. component.options.Clear();
  59. foreach (string term in this._Terms)
  60. {
  61. string translation = LocalizationManager.GetTranslation(term, true, 0, true, false, null, null);
  62. component.options.Add(new Dropdown.OptionData(translation));
  63. }
  64. component.RefreshShownValue();
  65. }
  66. public List<string> _Terms = new List<string>();
  67. }
  68. }