LocalizeTarget_UnityUI_Text.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. using UnityEditor;
  2. using UnityEngine;
  3. using UnityEngine.UI;
  4. namespace I2.Loc
  5. {
  6. #if UNITY_EDITOR
  7. [InitializeOnLoad]
  8. #endif
  9. public class LocalizeTarget_UnityUI_Text : LocalizeTarget<Text>
  10. {
  11. static LocalizeTarget_UnityUI_Text() { AutoRegister(); }
  12. [RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSceneLoad)] static void AutoRegister() { LocalizationManager.RegisterTarget(new LocalizeTargetDesc_Type<Text, LocalizeTarget_UnityUI_Text> { Name = "Text", Priority = 100 }); }
  13. TextAnchor mAlignment_RTL = TextAnchor.UpperRight;
  14. TextAnchor mAlignment_LTR = TextAnchor.UpperLeft;
  15. bool mAlignmentWasRTL;
  16. bool mInitializeAlignment = true;
  17. public override eTermType GetPrimaryTermType(Localize cmp) { return eTermType.Text; }
  18. public override eTermType GetSecondaryTermType(Localize cmp) { return eTermType.Font; }
  19. public override bool CanUseSecondaryTerm () { return true; }
  20. public override bool AllowMainTermToBeRTL () { return true; }
  21. public override bool AllowSecondTermToBeRTL () { return false; }
  22. public override void GetFinalTerms ( Localize cmp, string Main, string Secondary, out string primaryTerm, out string secondaryTerm )
  23. {
  24. primaryTerm = mTarget ? mTarget.text : null;
  25. secondaryTerm = mTarget.font!=null ? mTarget.font.name : string.Empty;
  26. }
  27. public override void DoLocalize ( Localize cmp, string mainTranslation, string secondaryTranslation )
  28. {
  29. //--[ Localize Font Object ]----------
  30. Font newFont = cmp.GetSecondaryTranslatedObj<Font>( ref mainTranslation, ref secondaryTranslation );
  31. if (newFont!=null && newFont!=mTarget.font)
  32. mTarget.font = newFont;
  33. if (mInitializeAlignment)
  34. {
  35. mInitializeAlignment = false;
  36. mAlignmentWasRTL = LocalizationManager.IsRight2Left;
  37. InitAlignment( mAlignmentWasRTL, mTarget.alignment, out mAlignment_LTR, out mAlignment_RTL );
  38. }
  39. else
  40. {
  41. TextAnchor alignRTL, alignLTR;
  42. InitAlignment( mAlignmentWasRTL, mTarget.alignment, out alignLTR, out alignRTL );
  43. if (mAlignmentWasRTL && mAlignment_RTL!=alignRTL ||
  44. !mAlignmentWasRTL && mAlignment_LTR != alignLTR)
  45. {
  46. mAlignment_LTR = alignLTR;
  47. mAlignment_RTL = alignRTL;
  48. }
  49. mAlignmentWasRTL = LocalizationManager.IsRight2Left;
  50. }
  51. if (mainTranslation!=null && mTarget.text != mainTranslation)
  52. {
  53. if (cmp.CorrectAlignmentForRTL)
  54. {
  55. mTarget.alignment = LocalizationManager.IsRight2Left ? mAlignment_RTL : mAlignment_LTR;
  56. }
  57. mTarget.text = mainTranslation;
  58. mTarget.SetVerticesDirty();
  59. // In the editor, sometimes unity "forgets" to show the changes
  60. #if UNITY_EDITOR
  61. if (!Application.isPlaying)
  62. EditorUtility.SetDirty( mTarget );
  63. #endif
  64. }
  65. }
  66. void InitAlignment ( bool isRTL, TextAnchor alignment, out TextAnchor alignLTR, out TextAnchor alignRTL )
  67. {
  68. alignLTR = alignRTL = alignment;
  69. if (isRTL)
  70. {
  71. switch (alignment)
  72. {
  73. case TextAnchor.UpperRight: alignLTR = TextAnchor.UpperLeft; break;
  74. case TextAnchor.MiddleRight: alignLTR = TextAnchor.MiddleLeft; break;
  75. case TextAnchor.LowerRight: alignLTR = TextAnchor.LowerLeft; break;
  76. case TextAnchor.UpperLeft: alignLTR = TextAnchor.UpperRight; break;
  77. case TextAnchor.MiddleLeft: alignLTR = TextAnchor.MiddleRight; break;
  78. case TextAnchor.LowerLeft: alignLTR = TextAnchor.LowerRight; break;
  79. }
  80. }
  81. else
  82. {
  83. switch (alignment)
  84. {
  85. case TextAnchor.UpperRight: alignRTL = TextAnchor.UpperLeft; break;
  86. case TextAnchor.MiddleRight: alignRTL = TextAnchor.MiddleLeft; break;
  87. case TextAnchor.LowerRight: alignRTL = TextAnchor.LowerLeft; break;
  88. case TextAnchor.UpperLeft: alignRTL = TextAnchor.UpperRight; break;
  89. case TextAnchor.MiddleLeft: alignRTL = TextAnchor.MiddleRight; break;
  90. case TextAnchor.LowerLeft: alignRTL = TextAnchor.LowerRight; break;
  91. }
  92. }
  93. }
  94. }
  95. }