- using System;
- using CIG.Translation;
- using CIG.Translation.ArabicSupport;
- using UnityEngine;
- using UnityEngine.UI;
- using UPersian.Utils;
-
- [RequireComponent(typeof(Text))]
- public class LocalizedText : MonoBehaviour
- {
- private void Start()
- {
- if (!string.IsNullOrEmpty(this._staticKey) && this.LocalizedString == null)
- {
- this.LocalizedString = Localization.Key(this._staticKey);
- }
- }
-
- public Text TextField
- {
- get
- {
- if (this._text == null)
- {
- this._text = base.GetComponent<Text>();
- }
- this._originalAlignment = this._text.alignment;
- return this._text;
- }
- }
-
- public ILocalizedString LocalizedString
- {
- get
- {
- return this._value;
- }
- set
- {
- this._value = value;
- this.Apply();
- }
- }
-
- private void Apply()
- {
- if (this._value != null)
- {
- ILocalizedString localizedString = this._value;
- if (this._toUpper)
- {
- localizedString = Localization.ToUpper(localizedString);
- }
- string text = localizedString.Translate();
- if (Localization.IsCurrentCultureArabic)
- {
- string twoLetterISOLanguageName = Localization.CurrentCulture.Info.TwoLetterISOLanguageName;
- if (twoLetterISOLanguageName != null)
- {
- if (!(twoLetterISOLanguageName == "ar"))
- {
- if (twoLetterISOLanguageName == "fa")
- {
- text = text.RtlFix();
- }
- }
- else
- {
- text = ArabicFixer.Fix(text);
- }
- }
- }
- if (this._adjustAlignment)
- {
- if (Localization.CurrentCulture.Info.TextInfo.IsRightToLeft)
- {
- this.TextField.alignment = this.HorizontallySwapTextAnchor(this._originalAlignment);
- }
- else
- {
- this.TextField.alignment = this._originalAlignment;
- }
- }
- this.TextField.text = text;
- }
- else
- {
- this.TextField.text = string.Empty;
- }
- }
-
- private TextAnchor HorizontallySwapTextAnchor(TextAnchor anchor)
- {
- switch (anchor)
- {
- case TextAnchor.UpperLeft:
- return TextAnchor.UpperRight;
- case TextAnchor.UpperRight:
- return TextAnchor.UpperLeft;
- case TextAnchor.MiddleLeft:
- return TextAnchor.MiddleRight;
- case TextAnchor.MiddleRight:
- return TextAnchor.MiddleLeft;
- case TextAnchor.LowerLeft:
- return TextAnchor.LowerRight;
- case TextAnchor.LowerRight:
- return TextAnchor.LowerLeft;
- }
- return anchor;
- }
-
- [SerializeField]
- private string _staticKey = string.Empty;
-
- [SerializeField]
- private Text _text;
-
- [SerializeField]
- private bool _adjustAlignment = true;
-
- [SerializeField]
- private bool _toUpper;
-
- private ILocalizedString _value;
-
- private TextAnchor _originalAlignment;
- }
|