using System; using System.Collections.Generic; using System.Linq; using System.Text; using UnityEngine; using UnityEngine.Events; namespace I2.Loc { [AddComponentMenu("I2/Localization/I2 Localize")] public class Localize : MonoBehaviour { public string Term { get { return this.mTerm; } set { this.SetTerm(value); } } public string SecondaryTerm { get { return this.mTermSecondary; } set { this.SetTerm(null, value); } } public Localize.ArgsPair[] TermArgs { get { return this.mTermArgs; } set { this.mTermArgs = value; } } private void Awake() { this.UpdateAssetDictionary(); this.FindTarget(); if (this.LocalizeOnAwake) { this.OnLocalize(false); } } private void OnEnable() { this.OnLocalize(false); } public bool HasCallback() { return this.LocalizeCallBack.HasCallback() || this.LocalizeEvent.GetPersistentEventCount() > 0 || this.LocalizeEventExecEnd.GetPersistentEventCount() > 0; } public void OnLocalize(bool Force = false) { if (!Force && (!base.enabled || base.gameObject == null || !base.gameObject.activeInHierarchy)) { return; } if (string.IsNullOrEmpty(LocalizationManager.CurrentLanguage)) { return; } if (!this.AlwaysForceLocalize && !Force && !this.HasCallback() && this.LastLocalizedLanguage == LocalizationManager.CurrentLanguage) { return; } this.LastLocalizedLanguage = LocalizationManager.CurrentLanguage; if (string.IsNullOrEmpty(this.FinalTerm) || string.IsNullOrEmpty(this.FinalSecondaryTerm)) { this.GetFinalTerms(out this.FinalTerm, out this.FinalSecondaryTerm); } bool flag = I2Utils.IsPlaying() && this.HasCallback(); if (!flag && string.IsNullOrEmpty(this.FinalTerm) && string.IsNullOrEmpty(this.FinalSecondaryTerm)) { return; } Localize.CallBackTerm = this.FinalTerm; Localize.CallBackSecondaryTerm = this.FinalSecondaryTerm; Localize.MainTranslation = ((!string.IsNullOrEmpty(this.FinalTerm) && !(this.FinalTerm == "-")) ? LocalizationManager.GetTranslation(this.FinalTerm, false, 0, true, false, null, null) : null); Localize.SecondaryTranslation = ((!string.IsNullOrEmpty(this.FinalSecondaryTerm) && !(this.FinalSecondaryTerm == "-")) ? LocalizationManager.GetTranslation(this.FinalSecondaryTerm, false, 0, true, false, null, null) : null); if (!flag && string.IsNullOrEmpty(this.FinalTerm) && string.IsNullOrEmpty(Localize.SecondaryTranslation)) { return; } Localize.CurrentLocalizeComponent = this; this.LocalizeCallBack.Execute(this); this.LocalizeEvent.Invoke(); LocalizationManager.ApplyLocalizationParams(ref Localize.MainTranslation, base.gameObject); if (!this.FindTarget()) { return; } bool flag2 = LocalizationManager.IsRight2Left && !this.IgnoreRTL; if (Localize.MainTranslation != null) { switch (this.PrimaryTermModifier) { case Localize.TermModification.ToUpper: Localize.MainTranslation = Localize.MainTranslation.ToUpper(); break; case Localize.TermModification.ToLower: Localize.MainTranslation = Localize.MainTranslation.ToLower(); break; case Localize.TermModification.ToUpperFirst: Localize.MainTranslation = GoogleTranslation.UppercaseFirst(Localize.MainTranslation); break; case Localize.TermModification.ToTitle: Localize.MainTranslation = GoogleTranslation.TitleCase(Localize.MainTranslation); break; } if (!string.IsNullOrEmpty(this.TermPrefix)) { Localize.MainTranslation = ((!flag2) ? (this.TermPrefix + Localize.MainTranslation) : (Localize.MainTranslation + this.TermPrefix)); } if (!string.IsNullOrEmpty(this.TermSuffix)) { Localize.MainTranslation = ((!flag2) ? (Localize.MainTranslation + this.TermSuffix) : (this.TermSuffix + Localize.MainTranslation)); } if (this.AddSpacesToJoinedLanguages && LocalizationManager.HasJoinedWords && !string.IsNullOrEmpty(Localize.MainTranslation)) { StringBuilder stringBuilder = new StringBuilder(); stringBuilder.Append(Localize.MainTranslation[0]); int i = 1; int length = Localize.MainTranslation.Length; while (i < length) { stringBuilder.Append(' '); stringBuilder.Append(Localize.MainTranslation[i]); i++; } Localize.MainTranslation = stringBuilder.ToString(); } if (flag2 && this.mLocalizeTarget.AllowMainTermToBeRTL() && !string.IsNullOrEmpty(Localize.MainTranslation)) { Localize.MainTranslation = LocalizationManager.ApplyRTLfix(Localize.MainTranslation, this.MaxCharactersInRTL, this.IgnoreNumbersInRTL); } } if (Localize.SecondaryTranslation != null) { switch (this.SecondaryTermModifier) { case Localize.TermModification.ToUpper: Localize.SecondaryTranslation = Localize.SecondaryTranslation.ToUpper(); break; case Localize.TermModification.ToLower: Localize.SecondaryTranslation = Localize.SecondaryTranslation.ToLower(); break; case Localize.TermModification.ToUpperFirst: Localize.SecondaryTranslation = GoogleTranslation.UppercaseFirst(Localize.SecondaryTranslation); break; case Localize.TermModification.ToTitle: Localize.SecondaryTranslation = GoogleTranslation.TitleCase(Localize.SecondaryTranslation); break; } if (flag2 && this.mLocalizeTarget.AllowSecondTermToBeRTL() && !string.IsNullOrEmpty(Localize.SecondaryTranslation)) { Localize.SecondaryTranslation = LocalizationManager.ApplyRTLfix(Localize.SecondaryTranslation); } } if (LocalizationManager.HighlightLocalizedTargets) { Localize.MainTranslation = "LOC:" + this.FinalTerm; } this.mLocalizeTarget.DoLocalize(this, Localize.MainTranslation, Localize.SecondaryTranslation); this.LocalizeEventExecEnd.Invoke(); Localize.CurrentLocalizeComponent = null; } public bool FindTarget() { if (this.mLocalizeTarget != null && this.mLocalizeTarget.IsValid(this)) { return true; } if (this.mLocalizeTarget != null) { UnityEngine.Object.DestroyImmediate(this.mLocalizeTarget); this.mLocalizeTarget = null; this.mLocalizeTargetName = null; } if (!string.IsNullOrEmpty(this.mLocalizeTargetName)) { foreach (ILocalizeTargetDescriptor localizeTargetDescriptor in LocalizationManager.mLocalizeTargets) { if (this.mLocalizeTargetName == localizeTargetDescriptor.GetTargetType().ToString()) { if (localizeTargetDescriptor.CanLocalize(this)) { this.mLocalizeTarget = localizeTargetDescriptor.CreateTarget(this); } if (this.mLocalizeTarget != null) { return true; } } } } foreach (ILocalizeTargetDescriptor localizeTargetDescriptor2 in LocalizationManager.mLocalizeTargets) { if (localizeTargetDescriptor2.CanLocalize(this)) { this.mLocalizeTarget = localizeTargetDescriptor2.CreateTarget(this); this.mLocalizeTargetName = localizeTargetDescriptor2.GetTargetType().ToString(); if (this.mLocalizeTarget != null) { return true; } } } return false; } public void GetFinalTerms(out string primaryTerm, out string secondaryTerm) { primaryTerm = string.Empty; secondaryTerm = string.Empty; if (!this.FindTarget()) { return; } if (this.mLocalizeTarget != null) { this.mLocalizeTarget.GetFinalTerms(this, this.mTerm, this.mTermSecondary, out primaryTerm, out secondaryTerm); primaryTerm = I2Utils.RemoveNonASCII(primaryTerm, false); } if (!string.IsNullOrEmpty(this.mTerm)) { primaryTerm = this.mTerm; } if (!string.IsNullOrEmpty(this.mTermSecondary)) { secondaryTerm = this.mTermSecondary; } if (primaryTerm != null) { primaryTerm = primaryTerm.Trim(); } if (secondaryTerm != null) { secondaryTerm = secondaryTerm.Trim(); } } public string GetMainTargetsText() { string text = null; string text2 = null; if (this.mLocalizeTarget != null) { this.mLocalizeTarget.GetFinalTerms(this, null, null, out text, out text2); } return (!string.IsNullOrEmpty(text)) ? text : this.mTerm; } public void SetFinalTerms(string Main, string Secondary, out string primaryTerm, out string secondaryTerm, bool RemoveNonASCII) { primaryTerm = ((!RemoveNonASCII) ? Main : I2Utils.RemoveNonASCII(Main, false)); secondaryTerm = Secondary; } public void SetTerm(string primary) { if (!string.IsNullOrEmpty(primary)) { this.mTerm = primary; this.FinalTerm = primary; } this.OnLocalize(true); } public void SetTerm(string primary, string secondary) { if (!string.IsNullOrEmpty(primary)) { this.mTerm = primary; this.FinalTerm = primary; } this.mTermSecondary = secondary; this.FinalSecondaryTerm = secondary; this.OnLocalize(true); } internal T GetSecondaryTranslatedObj(ref string mainTranslation, ref string secondaryTranslation) where T : UnityEngine.Object { string text; string text2; this.DeserializeTranslation(mainTranslation, out text, out text2); T t = (T)((object)null); if (!string.IsNullOrEmpty(text2)) { t = this.GetObject(text2); if (t != null) { mainTranslation = text; secondaryTranslation = text2; } } if (t == null) { t = this.GetObject(secondaryTranslation); } return t; } public void UpdateAssetDictionary() { this.TranslatedObjects.RemoveAll((UnityEngine.Object x) => x == null); this.mAssetDictionary = this.TranslatedObjects.Distinct().ToDictionary((UnityEngine.Object o) => o.name); } internal T GetObject(string Translation) where T : UnityEngine.Object { if (string.IsNullOrEmpty(Translation)) { return (T)((object)null); } T translatedObject = this.GetTranslatedObject(Translation); if (translatedObject == null) { translatedObject = this.GetTranslatedObject(Translation); } return translatedObject; } private T GetTranslatedObject(string Translation) where T : UnityEngine.Object { return this.FindTranslatedObject(Translation); } private void DeserializeTranslation(string translation, out string value, out string secondary) { if (!string.IsNullOrEmpty(translation) && translation.Length > 1 && translation[0] == '[') { int num = translation.IndexOf(']'); if (num > 0) { secondary = translation.Substring(1, num - 1); value = translation.Substring(num + 1); return; } } value = translation; secondary = string.Empty; } public T FindTranslatedObject(string value) where T : UnityEngine.Object { if (string.IsNullOrEmpty(value)) { return (T)((object)null); } if (this.mAssetDictionary == null || this.mAssetDictionary.Count != this.TranslatedObjects.Count) { this.UpdateAssetDictionary(); } foreach (KeyValuePair keyValuePair in this.mAssetDictionary) { if (keyValuePair.Value is T && value.EndsWith(keyValuePair.Key, StringComparison.OrdinalIgnoreCase) && string.Compare(value, keyValuePair.Key, StringComparison.OrdinalIgnoreCase) == 0) { return (T)((object)keyValuePair.Value); } } T t = LocalizationManager.FindAsset(value) as T; if (t) { return t; } return ResourceManager.pInstance.GetAsset(value); } public bool HasTranslatedObject(UnityEngine.Object Obj) { return this.TranslatedObjects.Contains(Obj) || ResourceManager.pInstance.HasAsset(Obj); } public void AddTranslatedObject(UnityEngine.Object Obj) { if (this.TranslatedObjects.Contains(Obj)) { return; } this.TranslatedObjects.Add(Obj); this.UpdateAssetDictionary(); } public void SetGlobalLanguage(string Language) { LocalizationManager.CurrentLanguage = Language; } public string mTerm = string.Empty; public string mTermSecondary = string.Empty; public Localize.ArgsPair[] mTermArgs; [NonSerialized] public string FinalTerm; [NonSerialized] public string FinalSecondaryTerm; public Localize.TermModification PrimaryTermModifier; public Localize.TermModification SecondaryTermModifier; public string TermPrefix; public string TermSuffix; public bool LocalizeOnAwake = true; private string LastLocalizedLanguage; public bool IgnoreRTL; public int MaxCharactersInRTL; public bool IgnoreNumbersInRTL = true; public bool CorrectAlignmentForRTL = true; public bool ForceOverFlowShrink = true; public bool NotAdjustScale; public bool AddSpacesToJoinedLanguages; public List TranslatedObjects = new List(); [NonSerialized] public Dictionary mAssetDictionary = new Dictionary(StringComparer.Ordinal); public UnityEvent LocalizeEvent = new UnityEvent(); public UnityEvent LocalizeEventExecEnd = new UnityEvent(); public static string MainTranslation; public static string SecondaryTranslation; public static string CallBackTerm; public static string CallBackSecondaryTerm; public static Localize CurrentLocalizeComponent; public bool AlwaysForceLocalize; [SerializeField] public EventCallback LocalizeCallBack = new EventCallback(); public bool mGUI_ShowReferences; public bool mGUI_ShowTems = true; public bool mGUI_ShowCallback; public ILocalizeTarget mLocalizeTarget; public string mLocalizeTargetName; [Serializable] public struct ArgsPair { public static Localize.ArgsPair Create(string setText) { return new Localize.ArgsPair { text = setText, isTerm = false }; } public static Localize.ArgsPair Create(string setText, bool setIsTerm) { return new Localize.ArgsPair { text = setText, isTerm = setIsTerm }; } public string text; public bool isTerm; } public enum TermModification { DontModify, ToUpper, ToLower, ToUpperFirst, ToTitle } } }