123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178 |
- using System;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.UI;
- namespace I2.Loc
- {
- public class LocalizeTarget_UnityUI_Text : LocalizeTarget<Text>
- {
- static LocalizeTarget_UnityUI_Text()
- {
- LocalizeTarget_UnityUI_Text.AutoRegister();
- }
- [RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSceneLoad)]
- private static void AutoRegister()
- {
- LocalizationManager.RegisterTarget(new LocalizeTargetDesc_Type<Text, LocalizeTarget_UnityUI_Text>
- {
- Name = "Text",
- Priority = 100
- });
- }
- public override eTermType GetPrimaryTermType(Localize cmp)
- {
- return eTermType.Text;
- }
- public override eTermType GetSecondaryTermType(Localize cmp)
- {
- return eTermType.Font;
- }
- public override bool CanUseSecondaryTerm()
- {
- return true;
- }
- public override bool AllowMainTermToBeRTL()
- {
- return true;
- }
- public override bool AllowSecondTermToBeRTL()
- {
- return false;
- }
- public override void GetFinalTerms(Localize cmp, string Main, string Secondary, out string primaryTerm, out string secondaryTerm)
- {
- primaryTerm = ((!this.mTarget) ? null : this.mTarget.text);
- secondaryTerm = ((!(this.mTarget.font != null)) ? string.Empty : this.mTarget.font.name);
- }
- public override void DoLocalize(Localize cmp, string mainTranslation, string secondaryTranslation)
- {
- Font secondaryTranslatedObj = cmp.GetSecondaryTranslatedObj<Font>(ref mainTranslation, ref secondaryTranslation);
- if (secondaryTranslatedObj != null && secondaryTranslatedObj != this.mTarget.font)
- {
- this.mTarget.font = secondaryTranslatedObj;
- }
- if (this.mInitializeAlignment)
- {
- this.mInitializeAlignment = false;
- this.mAlignmentWasRTL = LocalizationManager.IsRight2Left;
- this.InitAlignment(this.mAlignmentWasRTL, this.mTarget.alignment, out this.mAlignment_LTR, out this.mAlignment_RTL);
- }
- else
- {
- TextAnchor textAnchor;
- TextAnchor textAnchor2;
- this.InitAlignment(this.mAlignmentWasRTL, this.mTarget.alignment, out textAnchor, out textAnchor2);
- if ((this.mAlignmentWasRTL && this.mAlignment_RTL != textAnchor2) || (!this.mAlignmentWasRTL && this.mAlignment_LTR != textAnchor))
- {
- this.mAlignment_LTR = textAnchor;
- this.mAlignment_RTL = textAnchor2;
- }
- this.mAlignmentWasRTL = LocalizationManager.IsRight2Left;
- }
- if (mainTranslation != null && this.mTarget.text != mainTranslation)
- {
- if (cmp.CorrectAlignmentForRTL)
- {
- this.mTarget.alignment = ((!LocalizationManager.IsRight2Left) ? this.mAlignment_LTR : this.mAlignment_RTL);
- }
- if (!string.IsNullOrEmpty(mainTranslation) && cmp.mTermArgs != null && 0 < cmp.mTermArgs.Length)
- {
- List<string> list = new List<string>();
- for (int i = 0; i < cmp.mTermArgs.Length; i++)
- {
- string text = cmp.mTermArgs[i].text;
- if (!cmp.mTermArgs[i].isTerm)
- {
- if (text.IndexOf('{') != -1 && text.IndexOf('}') != -1)
- {
- mainTranslation += text;
- }
- else
- {
- list.Add(text);
- }
- }
- else
- {
- list.Add((text == null) ? string.Empty : LocalizationManager.GetTranslation(text, false, 0, true, false, null, null));
- }
- }
- mainTranslation = string.Format(mainTranslation, list.ToArray());
- }
- this.mTarget.text = mainTranslation;
- this.mTarget.SetVerticesDirty();
- }
- }
- private void InitAlignment(bool isRTL, TextAnchor alignment, out TextAnchor alignLTR, out TextAnchor alignRTL)
- {
- alignRTL = alignment;
- alignLTR = alignment;
- if (isRTL)
- {
- switch (alignment)
- {
- case TextAnchor.UpperLeft:
- alignLTR = TextAnchor.UpperRight;
- break;
- case TextAnchor.UpperRight:
- alignLTR = TextAnchor.UpperLeft;
- break;
- case TextAnchor.MiddleLeft:
- alignLTR = TextAnchor.MiddleRight;
- break;
- case TextAnchor.MiddleRight:
- alignLTR = TextAnchor.MiddleLeft;
- break;
- case TextAnchor.LowerLeft:
- alignLTR = TextAnchor.LowerRight;
- break;
- case TextAnchor.LowerRight:
- alignLTR = TextAnchor.LowerLeft;
- break;
- }
- }
- else
- {
- switch (alignment)
- {
- case TextAnchor.UpperLeft:
- alignRTL = TextAnchor.UpperRight;
- break;
- case TextAnchor.UpperRight:
- alignRTL = TextAnchor.UpperLeft;
- break;
- case TextAnchor.MiddleLeft:
- alignRTL = TextAnchor.MiddleRight;
- break;
- case TextAnchor.MiddleRight:
- alignRTL = TextAnchor.MiddleLeft;
- break;
- case TextAnchor.LowerLeft:
- alignRTL = TextAnchor.LowerRight;
- break;
- case TextAnchor.LowerRight:
- alignRTL = TextAnchor.LowerLeft;
- break;
- }
- }
- }
- private TextAnchor mAlignment_RTL = TextAnchor.UpperRight;
- private TextAnchor mAlignment_LTR;
- private bool mAlignmentWasRTL;
- private bool mInitializeAlignment = true;
- }
- }
|