No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.
 
 
 

128 líneas
2.6 KiB

  1. using System;
  2. using CIG.Translation;
  3. using CIG.Translation.ArabicSupport;
  4. using UnityEngine;
  5. using UnityEngine.UI;
  6. using UPersian.Utils;
  7. [RequireComponent(typeof(Text))]
  8. public class LocalizedText : MonoBehaviour
  9. {
  10. private void Start()
  11. {
  12. if (!string.IsNullOrEmpty(this._staticKey) && this.LocalizedString == null)
  13. {
  14. this.LocalizedString = Localization.Key(this._staticKey);
  15. }
  16. }
  17. public Text TextField
  18. {
  19. get
  20. {
  21. if (this._text == null)
  22. {
  23. this._text = base.GetComponent<Text>();
  24. }
  25. this._originalAlignment = this._text.alignment;
  26. return this._text;
  27. }
  28. }
  29. public ILocalizedString LocalizedString
  30. {
  31. get
  32. {
  33. return this._value;
  34. }
  35. set
  36. {
  37. this._value = value;
  38. this.Apply();
  39. }
  40. }
  41. private void Apply()
  42. {
  43. if (this._value != null)
  44. {
  45. ILocalizedString localizedString = this._value;
  46. if (this._toUpper)
  47. {
  48. localizedString = Localization.ToUpper(localizedString);
  49. }
  50. string text = localizedString.Translate();
  51. if (Localization.IsCurrentCultureArabic)
  52. {
  53. string twoLetterISOLanguageName = Localization.CurrentCulture.Info.TwoLetterISOLanguageName;
  54. if (twoLetterISOLanguageName != null)
  55. {
  56. if (!(twoLetterISOLanguageName == "ar"))
  57. {
  58. if (twoLetterISOLanguageName == "fa")
  59. {
  60. text = text.RtlFix();
  61. }
  62. }
  63. else
  64. {
  65. text = ArabicFixer.Fix(text);
  66. }
  67. }
  68. }
  69. if (this._adjustAlignment)
  70. {
  71. if (Localization.CurrentCulture.Info.TextInfo.IsRightToLeft)
  72. {
  73. this.TextField.alignment = this.HorizontallySwapTextAnchor(this._originalAlignment);
  74. }
  75. else
  76. {
  77. this.TextField.alignment = this._originalAlignment;
  78. }
  79. }
  80. this.TextField.text = text;
  81. }
  82. else
  83. {
  84. this.TextField.text = string.Empty;
  85. }
  86. }
  87. private TextAnchor HorizontallySwapTextAnchor(TextAnchor anchor)
  88. {
  89. switch (anchor)
  90. {
  91. case TextAnchor.UpperLeft:
  92. return TextAnchor.UpperRight;
  93. case TextAnchor.UpperRight:
  94. return TextAnchor.UpperLeft;
  95. case TextAnchor.MiddleLeft:
  96. return TextAnchor.MiddleRight;
  97. case TextAnchor.MiddleRight:
  98. return TextAnchor.MiddleLeft;
  99. case TextAnchor.LowerLeft:
  100. return TextAnchor.LowerRight;
  101. case TextAnchor.LowerRight:
  102. return TextAnchor.LowerLeft;
  103. }
  104. return anchor;
  105. }
  106. [SerializeField]
  107. private string _staticKey = string.Empty;
  108. [SerializeField]
  109. private Text _text;
  110. [SerializeField]
  111. private bool _adjustAlignment = true;
  112. [SerializeField]
  113. private bool _toUpper;
  114. private ILocalizedString _value;
  115. private TextAnchor _originalAlignment;
  116. }