LocalizeTarget_UnityUI_Text.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. using System;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. namespace I2.Loc
  6. {
  7. public class LocalizeTarget_UnityUI_Text : LocalizeTarget<Text>
  8. {
  9. static LocalizeTarget_UnityUI_Text()
  10. {
  11. LocalizeTarget_UnityUI_Text.AutoRegister();
  12. }
  13. [RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSceneLoad)]
  14. private static void AutoRegister()
  15. {
  16. LocalizationManager.RegisterTarget(new LocalizeTargetDesc_Type<Text, LocalizeTarget_UnityUI_Text>
  17. {
  18. Name = "Text",
  19. Priority = 100
  20. });
  21. }
  22. public override eTermType GetPrimaryTermType(Localize cmp)
  23. {
  24. return eTermType.Text;
  25. }
  26. public override eTermType GetSecondaryTermType(Localize cmp)
  27. {
  28. return eTermType.Font;
  29. }
  30. public override bool CanUseSecondaryTerm()
  31. {
  32. return true;
  33. }
  34. public override bool AllowMainTermToBeRTL()
  35. {
  36. return true;
  37. }
  38. public override bool AllowSecondTermToBeRTL()
  39. {
  40. return false;
  41. }
  42. public override void GetFinalTerms(Localize cmp, string Main, string Secondary, out string primaryTerm, out string secondaryTerm)
  43. {
  44. primaryTerm = ((!this.mTarget) ? null : this.mTarget.text);
  45. secondaryTerm = ((!(this.mTarget.font != null)) ? string.Empty : this.mTarget.font.name);
  46. }
  47. public override void DoLocalize(Localize cmp, string mainTranslation, string secondaryTranslation)
  48. {
  49. Font secondaryTranslatedObj = cmp.GetSecondaryTranslatedObj<Font>(ref mainTranslation, ref secondaryTranslation);
  50. if (secondaryTranslatedObj != null && secondaryTranslatedObj != this.mTarget.font)
  51. {
  52. this.mTarget.font = secondaryTranslatedObj;
  53. }
  54. if (this.mInitializeAlignment)
  55. {
  56. this.mInitializeAlignment = false;
  57. this.mAlignmentWasRTL = LocalizationManager.IsRight2Left;
  58. this.InitAlignment(this.mAlignmentWasRTL, this.mTarget.alignment, out this.mAlignment_LTR, out this.mAlignment_RTL);
  59. }
  60. else
  61. {
  62. TextAnchor textAnchor;
  63. TextAnchor textAnchor2;
  64. this.InitAlignment(this.mAlignmentWasRTL, this.mTarget.alignment, out textAnchor, out textAnchor2);
  65. if ((this.mAlignmentWasRTL && this.mAlignment_RTL != textAnchor2) || (!this.mAlignmentWasRTL && this.mAlignment_LTR != textAnchor))
  66. {
  67. this.mAlignment_LTR = textAnchor;
  68. this.mAlignment_RTL = textAnchor2;
  69. }
  70. this.mAlignmentWasRTL = LocalizationManager.IsRight2Left;
  71. }
  72. if (mainTranslation != null && this.mTarget.text != mainTranslation)
  73. {
  74. if (cmp.CorrectAlignmentForRTL)
  75. {
  76. this.mTarget.alignment = ((!LocalizationManager.IsRight2Left) ? this.mAlignment_LTR : this.mAlignment_RTL);
  77. }
  78. if (!string.IsNullOrEmpty(mainTranslation) && cmp.mTermArgs != null && 0 < cmp.mTermArgs.Length)
  79. {
  80. List<string> list = new List<string>();
  81. for (int i = 0; i < cmp.mTermArgs.Length; i++)
  82. {
  83. string text = cmp.mTermArgs[i].text;
  84. if (!cmp.mTermArgs[i].isTerm)
  85. {
  86. if (text.IndexOf('{') != -1 && text.IndexOf('}') != -1)
  87. {
  88. mainTranslation += text;
  89. }
  90. else
  91. {
  92. list.Add(text);
  93. }
  94. }
  95. else
  96. {
  97. list.Add((text == null) ? string.Empty : LocalizationManager.GetTranslation(text, false, 0, true, false, null, null));
  98. }
  99. }
  100. mainTranslation = string.Format(mainTranslation, list.ToArray());
  101. }
  102. this.mTarget.text = mainTranslation;
  103. this.mTarget.SetVerticesDirty();
  104. }
  105. }
  106. private void InitAlignment(bool isRTL, TextAnchor alignment, out TextAnchor alignLTR, out TextAnchor alignRTL)
  107. {
  108. alignRTL = alignment;
  109. alignLTR = alignment;
  110. if (isRTL)
  111. {
  112. switch (alignment)
  113. {
  114. case TextAnchor.UpperLeft:
  115. alignLTR = TextAnchor.UpperRight;
  116. break;
  117. case TextAnchor.UpperRight:
  118. alignLTR = TextAnchor.UpperLeft;
  119. break;
  120. case TextAnchor.MiddleLeft:
  121. alignLTR = TextAnchor.MiddleRight;
  122. break;
  123. case TextAnchor.MiddleRight:
  124. alignLTR = TextAnchor.MiddleLeft;
  125. break;
  126. case TextAnchor.LowerLeft:
  127. alignLTR = TextAnchor.LowerRight;
  128. break;
  129. case TextAnchor.LowerRight:
  130. alignLTR = TextAnchor.LowerLeft;
  131. break;
  132. }
  133. }
  134. else
  135. {
  136. switch (alignment)
  137. {
  138. case TextAnchor.UpperLeft:
  139. alignRTL = TextAnchor.UpperRight;
  140. break;
  141. case TextAnchor.UpperRight:
  142. alignRTL = TextAnchor.UpperLeft;
  143. break;
  144. case TextAnchor.MiddleLeft:
  145. alignRTL = TextAnchor.MiddleRight;
  146. break;
  147. case TextAnchor.MiddleRight:
  148. alignRTL = TextAnchor.MiddleLeft;
  149. break;
  150. case TextAnchor.LowerLeft:
  151. alignRTL = TextAnchor.LowerRight;
  152. break;
  153. case TextAnchor.LowerRight:
  154. alignRTL = TextAnchor.LowerLeft;
  155. break;
  156. }
  157. }
  158. }
  159. private TextAnchor mAlignment_RTL = TextAnchor.UpperRight;
  160. private TextAnchor mAlignment_LTR;
  161. private bool mAlignmentWasRTL;
  162. private bool mInitializeAlignment = true;
  163. }
  164. }