LocalizeTarget_NGUI_Label.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. using System;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. namespace I2.Loc
  5. {
  6. public class LocalizeTarget_NGUI_Label : LocalizeTarget<UILabel>
  7. {
  8. static LocalizeTarget_NGUI_Label()
  9. {
  10. LocalizeTarget_NGUI_Label.AutoRegister();
  11. }
  12. [RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSceneLoad)]
  13. private static void AutoRegister()
  14. {
  15. LocalizationManager.RegisterTarget(new LocalizeTargetDesc_Type<UILabel, LocalizeTarget_NGUI_Label>
  16. {
  17. Name = "NGUI Label",
  18. Priority = 100
  19. });
  20. }
  21. public override eTermType GetPrimaryTermType(Localize cmp)
  22. {
  23. return eTermType.Text;
  24. }
  25. public override eTermType GetSecondaryTermType(Localize cmp)
  26. {
  27. return eTermType.UIFont;
  28. }
  29. public override bool CanUseSecondaryTerm()
  30. {
  31. return true;
  32. }
  33. public override bool AllowMainTermToBeRTL()
  34. {
  35. return true;
  36. }
  37. public override bool AllowSecondTermToBeRTL()
  38. {
  39. return false;
  40. }
  41. public override void GetFinalTerms(Localize cmp, string Main, string Secondary, out string primaryTerm, out string secondaryTerm)
  42. {
  43. primaryTerm = ((!this.mTarget) ? null : this.mTarget.text);
  44. secondaryTerm = ((!(this.mTarget.ambigiousFont != null)) ? string.Empty : this.mTarget.ambigiousFont.name);
  45. }
  46. public override void DoLocalize(Localize cmp, string mainTranslation, string secondaryTranslation)
  47. {
  48. Font secondaryTranslatedObj = cmp.GetSecondaryTranslatedObj<Font>(ref mainTranslation, ref secondaryTranslation);
  49. if (secondaryTranslatedObj != null)
  50. {
  51. if (secondaryTranslatedObj != this.mTarget.ambigiousFont)
  52. {
  53. this.mTarget.ambigiousFont = secondaryTranslatedObj;
  54. }
  55. }
  56. else
  57. {
  58. UIFont secondaryTranslatedObj2 = cmp.GetSecondaryTranslatedObj<UIFont>(ref mainTranslation, ref secondaryTranslation);
  59. if (secondaryTranslatedObj2 != null && this.mTarget.ambigiousFont != secondaryTranslatedObj2)
  60. {
  61. this.mTarget.ambigiousFont = secondaryTranslatedObj2;
  62. }
  63. }
  64. if (this.mInitializeAlignment)
  65. {
  66. this.mInitializeAlignment = false;
  67. this.mAlignment_LTR = (this.mAlignment_RTL = this.mTarget.alignment);
  68. if (LocalizationManager.IsRight2Left && this.mAlignment_RTL == NGUIText.Alignment.Right)
  69. {
  70. this.mAlignment_LTR = NGUIText.Alignment.Left;
  71. }
  72. if (!LocalizationManager.IsRight2Left && this.mAlignment_LTR == NGUIText.Alignment.Left)
  73. {
  74. this.mAlignment_RTL = NGUIText.Alignment.Right;
  75. }
  76. }
  77. UIInput uiinput = NGUITools.FindInParents<UIInput>(this.mTarget.gameObject);
  78. if (uiinput != null && uiinput.label == this.mTarget)
  79. {
  80. if (mainTranslation != null && uiinput.defaultText != mainTranslation)
  81. {
  82. if (cmp.CorrectAlignmentForRTL && (uiinput.label.alignment == NGUIText.Alignment.Left || uiinput.label.alignment == NGUIText.Alignment.Right))
  83. {
  84. uiinput.label.alignment = ((!LocalizationManager.IsRight2Left) ? this.mAlignment_LTR : this.mAlignment_RTL);
  85. }
  86. uiinput.defaultText = mainTranslation;
  87. }
  88. }
  89. else if (mainTranslation != null && this.mTarget.text != mainTranslation)
  90. {
  91. if (cmp.CorrectAlignmentForRTL && (this.mTarget.alignment == NGUIText.Alignment.Left || this.mTarget.alignment == NGUIText.Alignment.Right))
  92. {
  93. this.mTarget.alignment = ((!LocalizationManager.IsRight2Left) ? this.mAlignment_LTR : this.mAlignment_RTL);
  94. }
  95. if (((!this.mTarget.useFloatSpacing && (this.mTarget.spacingX != 0 || this.mTarget.spacingY != 0)) || (this.mTarget.useFloatSpacing && (this.mTarget.floatSpacingX != 0f || this.mTarget.floatSpacingY != 0f))) && cmp.gameObject.GetComponent<NGUILabelLocalizeSupport>() == null)
  96. {
  97. NGUILabelLocalizeSupport nguilabelLocalizeSupport = cmp.gameObject.AddComponent<NGUILabelLocalizeSupport>();
  98. if (!cmp.ForceOverFlowShrink)
  99. {
  100. nguilabelLocalizeSupport.overRidePropertys.overFlow.enabled = false;
  101. }
  102. }
  103. if (cmp.ForceOverFlowShrink && cmp.gameObject.GetComponent<NGUILabelLocalizeSupport>() == null)
  104. {
  105. cmp.gameObject.AddComponent<NGUILabelLocalizeSupport>();
  106. }
  107. if (!string.IsNullOrEmpty(mainTranslation) && cmp.mTermArgs != null && 0 < cmp.mTermArgs.Length)
  108. {
  109. List<string> list = new List<string>();
  110. for (int i = 0; i < cmp.mTermArgs.Length; i++)
  111. {
  112. string text = cmp.mTermArgs[i].text;
  113. if (!cmp.mTermArgs[i].isTerm)
  114. {
  115. if (text.IndexOf('{') != -1 && text.IndexOf('}') != -1)
  116. {
  117. mainTranslation += text;
  118. }
  119. else
  120. {
  121. list.Add(text);
  122. }
  123. }
  124. else
  125. {
  126. list.Add((text == null) ? string.Empty : LocalizationManager.GetTranslation(text, false, 0, true, false, null, null));
  127. }
  128. }
  129. mainTranslation = string.Format(mainTranslation, list.ToArray());
  130. }
  131. this.mTarget.text = mainTranslation;
  132. }
  133. }
  134. private NGUIText.Alignment mAlignment_RTL = NGUIText.Alignment.Right;
  135. private NGUIText.Alignment mAlignment_LTR = NGUIText.Alignment.Left;
  136. private bool mAlignmentWasRTL;
  137. private bool mInitializeAlignment = true;
  138. }
  139. }