Localize.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using UnityEngine;
  6. using UnityEngine.Events;
  7. namespace I2.Loc
  8. {
  9. [AddComponentMenu("I2/Localization/I2 Localize")]
  10. public class Localize : MonoBehaviour
  11. {
  12. public string Term
  13. {
  14. get
  15. {
  16. return this.mTerm;
  17. }
  18. set
  19. {
  20. this.SetTerm(value);
  21. }
  22. }
  23. public string SecondaryTerm
  24. {
  25. get
  26. {
  27. return this.mTermSecondary;
  28. }
  29. set
  30. {
  31. this.SetTerm(null, value);
  32. }
  33. }
  34. private void Awake()
  35. {
  36. this.UpdateAssetDictionary();
  37. this.FindTarget();
  38. if (this.LocalizeOnAwake)
  39. {
  40. this.OnLocalize(false);
  41. }
  42. }
  43. private void OnEnable()
  44. {
  45. this.OnLocalize(false);
  46. }
  47. public bool HasCallback()
  48. {
  49. return this.LocalizeCallBack.HasCallback() || this.LocalizeEvent.GetPersistentEventCount() > 0;
  50. }
  51. public void OnLocalize(bool Force = false)
  52. {
  53. if (!Force && (!base.enabled || base.gameObject == null || !base.gameObject.activeInHierarchy))
  54. {
  55. return;
  56. }
  57. if (string.IsNullOrEmpty(LocalizationManager.CurrentLanguage))
  58. {
  59. return;
  60. }
  61. if (!this.AlwaysForceLocalize && !Force && !this.HasCallback() && this.LastLocalizedLanguage == LocalizationManager.CurrentLanguage)
  62. {
  63. return;
  64. }
  65. this.LastLocalizedLanguage = LocalizationManager.CurrentLanguage;
  66. if (string.IsNullOrEmpty(this.FinalTerm) || string.IsNullOrEmpty(this.FinalSecondaryTerm))
  67. {
  68. this.GetFinalTerms(out this.FinalTerm, out this.FinalSecondaryTerm);
  69. }
  70. bool flag = I2Utils.IsPlaying() && this.HasCallback();
  71. if (!flag && string.IsNullOrEmpty(this.FinalTerm) && string.IsNullOrEmpty(this.FinalSecondaryTerm))
  72. {
  73. return;
  74. }
  75. Localize.CallBackTerm = this.FinalTerm;
  76. Localize.CallBackSecondaryTerm = this.FinalSecondaryTerm;
  77. Localize.MainTranslation = ((!string.IsNullOrEmpty(this.FinalTerm) && !(this.FinalTerm == "-")) ? LocalizationManager.GetTranslation(this.FinalTerm, false, 0, true, false, null, null) : null);
  78. Localize.SecondaryTranslation = ((!string.IsNullOrEmpty(this.FinalSecondaryTerm) && !(this.FinalSecondaryTerm == "-")) ? LocalizationManager.GetTranslation(this.FinalSecondaryTerm, false, 0, true, false, null, null) : null);
  79. if (!flag && string.IsNullOrEmpty(this.FinalTerm) && string.IsNullOrEmpty(Localize.SecondaryTranslation))
  80. {
  81. return;
  82. }
  83. Localize.CurrentLocalizeComponent = this;
  84. this.LocalizeCallBack.Execute(this);
  85. this.LocalizeEvent.Invoke();
  86. LocalizationManager.ApplyLocalizationParams(ref Localize.MainTranslation, base.gameObject);
  87. if (!this.FindTarget())
  88. {
  89. return;
  90. }
  91. bool flag2 = LocalizationManager.IsRight2Left && !this.IgnoreRTL;
  92. if (Localize.MainTranslation != null)
  93. {
  94. switch (this.PrimaryTermModifier)
  95. {
  96. case Localize.TermModification.ToUpper:
  97. Localize.MainTranslation = Localize.MainTranslation.ToUpper();
  98. break;
  99. case Localize.TermModification.ToLower:
  100. Localize.MainTranslation = Localize.MainTranslation.ToLower();
  101. break;
  102. case Localize.TermModification.ToUpperFirst:
  103. Localize.MainTranslation = GoogleTranslation.UppercaseFirst(Localize.MainTranslation);
  104. break;
  105. case Localize.TermModification.ToTitle:
  106. Localize.MainTranslation = GoogleTranslation.TitleCase(Localize.MainTranslation);
  107. break;
  108. }
  109. if (!string.IsNullOrEmpty(this.TermPrefix))
  110. {
  111. Localize.MainTranslation = ((!flag2) ? (this.TermPrefix + Localize.MainTranslation) : (Localize.MainTranslation + this.TermPrefix));
  112. }
  113. if (!string.IsNullOrEmpty(this.TermSuffix))
  114. {
  115. Localize.MainTranslation = ((!flag2) ? (Localize.MainTranslation + this.TermSuffix) : (this.TermSuffix + Localize.MainTranslation));
  116. }
  117. if (this.AddSpacesToJoinedLanguages && LocalizationManager.HasJoinedWords && !string.IsNullOrEmpty(Localize.MainTranslation))
  118. {
  119. StringBuilder stringBuilder = new StringBuilder();
  120. stringBuilder.Append(Localize.MainTranslation[0]);
  121. int i = 1;
  122. int length = Localize.MainTranslation.Length;
  123. while (i < length)
  124. {
  125. stringBuilder.Append(' ');
  126. stringBuilder.Append(Localize.MainTranslation[i]);
  127. i++;
  128. }
  129. Localize.MainTranslation = stringBuilder.ToString();
  130. }
  131. if (flag2 && this.mLocalizeTarget.AllowMainTermToBeRTL() && !string.IsNullOrEmpty(Localize.MainTranslation))
  132. {
  133. Localize.MainTranslation = LocalizationManager.ApplyRTLfix(Localize.MainTranslation, this.MaxCharactersInRTL, this.IgnoreNumbersInRTL);
  134. }
  135. }
  136. if (Localize.SecondaryTranslation != null)
  137. {
  138. switch (this.SecondaryTermModifier)
  139. {
  140. case Localize.TermModification.ToUpper:
  141. Localize.SecondaryTranslation = Localize.SecondaryTranslation.ToUpper();
  142. break;
  143. case Localize.TermModification.ToLower:
  144. Localize.SecondaryTranslation = Localize.SecondaryTranslation.ToLower();
  145. break;
  146. case Localize.TermModification.ToUpperFirst:
  147. Localize.SecondaryTranslation = GoogleTranslation.UppercaseFirst(Localize.SecondaryTranslation);
  148. break;
  149. case Localize.TermModification.ToTitle:
  150. Localize.SecondaryTranslation = GoogleTranslation.TitleCase(Localize.SecondaryTranslation);
  151. break;
  152. }
  153. if (flag2 && this.mLocalizeTarget.AllowSecondTermToBeRTL() && !string.IsNullOrEmpty(Localize.SecondaryTranslation))
  154. {
  155. Localize.SecondaryTranslation = LocalizationManager.ApplyRTLfix(Localize.SecondaryTranslation);
  156. }
  157. }
  158. if (LocalizationManager.HighlightLocalizedTargets)
  159. {
  160. Localize.MainTranslation = "LOC:" + this.FinalTerm;
  161. }
  162. this.mLocalizeTarget.DoLocalize(this, Localize.MainTranslation, Localize.SecondaryTranslation);
  163. Localize.CurrentLocalizeComponent = null;
  164. }
  165. public bool FindTarget()
  166. {
  167. if (this.mLocalizeTarget != null && this.mLocalizeTarget.IsValid(this))
  168. {
  169. return true;
  170. }
  171. if (this.mLocalizeTarget != null)
  172. {
  173. UnityEngine.Object.DestroyImmediate(this.mLocalizeTarget);
  174. this.mLocalizeTarget = null;
  175. this.mLocalizeTargetName = null;
  176. }
  177. if (!string.IsNullOrEmpty(this.mLocalizeTargetName))
  178. {
  179. foreach (ILocalizeTargetDescriptor localizeTargetDescriptor in LocalizationManager.mLocalizeTargets)
  180. {
  181. if (this.mLocalizeTargetName == localizeTargetDescriptor.GetTargetType().ToString())
  182. {
  183. if (localizeTargetDescriptor.CanLocalize(this))
  184. {
  185. this.mLocalizeTarget = localizeTargetDescriptor.CreateTarget(this);
  186. }
  187. if (this.mLocalizeTarget != null)
  188. {
  189. return true;
  190. }
  191. }
  192. }
  193. }
  194. foreach (ILocalizeTargetDescriptor localizeTargetDescriptor2 in LocalizationManager.mLocalizeTargets)
  195. {
  196. if (localizeTargetDescriptor2.CanLocalize(this))
  197. {
  198. this.mLocalizeTarget = localizeTargetDescriptor2.CreateTarget(this);
  199. this.mLocalizeTargetName = localizeTargetDescriptor2.GetTargetType().ToString();
  200. if (this.mLocalizeTarget != null)
  201. {
  202. return true;
  203. }
  204. }
  205. }
  206. return false;
  207. }
  208. public void GetFinalTerms(out string primaryTerm, out string secondaryTerm)
  209. {
  210. primaryTerm = string.Empty;
  211. secondaryTerm = string.Empty;
  212. if (!this.FindTarget())
  213. {
  214. return;
  215. }
  216. if (this.mLocalizeTarget != null)
  217. {
  218. this.mLocalizeTarget.GetFinalTerms(this, this.mTerm, this.mTermSecondary, out primaryTerm, out secondaryTerm);
  219. primaryTerm = I2Utils.RemoveNonASCII(primaryTerm, false);
  220. }
  221. if (!string.IsNullOrEmpty(this.mTerm))
  222. {
  223. primaryTerm = this.mTerm;
  224. }
  225. if (!string.IsNullOrEmpty(this.mTermSecondary))
  226. {
  227. secondaryTerm = this.mTermSecondary;
  228. }
  229. if (primaryTerm != null)
  230. {
  231. primaryTerm = primaryTerm.Trim();
  232. }
  233. if (secondaryTerm != null)
  234. {
  235. secondaryTerm = secondaryTerm.Trim();
  236. }
  237. }
  238. public string GetMainTargetsText()
  239. {
  240. string text = null;
  241. string text2 = null;
  242. if (this.mLocalizeTarget != null)
  243. {
  244. this.mLocalizeTarget.GetFinalTerms(this, null, null, out text, out text2);
  245. }
  246. return (!string.IsNullOrEmpty(text)) ? text : this.mTerm;
  247. }
  248. public void SetFinalTerms(string Main, string Secondary, out string primaryTerm, out string secondaryTerm, bool RemoveNonASCII)
  249. {
  250. primaryTerm = ((!RemoveNonASCII) ? Main : I2Utils.RemoveNonASCII(Main, false));
  251. secondaryTerm = Secondary;
  252. }
  253. public void SetTerm(string primary)
  254. {
  255. if (!string.IsNullOrEmpty(primary))
  256. {
  257. this.mTerm = primary;
  258. this.FinalTerm = primary;
  259. }
  260. this.OnLocalize(true);
  261. }
  262. public void SetTerm(string primary, string secondary)
  263. {
  264. if (!string.IsNullOrEmpty(primary))
  265. {
  266. this.mTerm = primary;
  267. this.FinalTerm = primary;
  268. }
  269. this.mTermSecondary = secondary;
  270. this.FinalSecondaryTerm = secondary;
  271. this.OnLocalize(true);
  272. }
  273. internal T GetSecondaryTranslatedObj<T>(ref string mainTranslation, ref string secondaryTranslation) where T : UnityEngine.Object
  274. {
  275. string text;
  276. string text2;
  277. this.DeserializeTranslation(mainTranslation, out text, out text2);
  278. T t = (T)((object)null);
  279. if (!string.IsNullOrEmpty(text2))
  280. {
  281. t = this.GetObject<T>(text2);
  282. if (t != null)
  283. {
  284. mainTranslation = text;
  285. secondaryTranslation = text2;
  286. }
  287. }
  288. if (t == null)
  289. {
  290. t = this.GetObject<T>(secondaryTranslation);
  291. }
  292. return t;
  293. }
  294. public void UpdateAssetDictionary()
  295. {
  296. this.TranslatedObjects.RemoveAll((UnityEngine.Object x) => x == null);
  297. this.mAssetDictionary = this.TranslatedObjects.Distinct<UnityEngine.Object>().ToDictionary((UnityEngine.Object o) => o.name);
  298. }
  299. internal T GetObject<T>(string Translation) where T : UnityEngine.Object
  300. {
  301. if (string.IsNullOrEmpty(Translation))
  302. {
  303. return (T)((object)null);
  304. }
  305. T translatedObject = this.GetTranslatedObject<T>(Translation);
  306. if (translatedObject == null)
  307. {
  308. translatedObject = this.GetTranslatedObject<T>(Translation);
  309. }
  310. return translatedObject;
  311. }
  312. private T GetTranslatedObject<T>(string Translation) where T : UnityEngine.Object
  313. {
  314. return this.FindTranslatedObject<T>(Translation);
  315. }
  316. private void DeserializeTranslation(string translation, out string value, out string secondary)
  317. {
  318. if (!string.IsNullOrEmpty(translation) && translation.Length > 1 && translation[0] == '[')
  319. {
  320. int num = translation.IndexOf(']');
  321. if (num > 0)
  322. {
  323. secondary = translation.Substring(1, num - 1);
  324. value = translation.Substring(num + 1);
  325. return;
  326. }
  327. }
  328. value = translation;
  329. secondary = string.Empty;
  330. }
  331. public T FindTranslatedObject<T>(string value) where T : UnityEngine.Object
  332. {
  333. if (string.IsNullOrEmpty(value))
  334. {
  335. return (T)((object)null);
  336. }
  337. if (this.mAssetDictionary == null || this.mAssetDictionary.Count != this.TranslatedObjects.Count)
  338. {
  339. this.UpdateAssetDictionary();
  340. }
  341. foreach (KeyValuePair<string, UnityEngine.Object> keyValuePair in this.mAssetDictionary)
  342. {
  343. if (keyValuePair.Value is T && value.EndsWith(keyValuePair.Key, StringComparison.OrdinalIgnoreCase) && string.Compare(value, keyValuePair.Key, StringComparison.OrdinalIgnoreCase) == 0)
  344. {
  345. return (T)((object)keyValuePair.Value);
  346. }
  347. }
  348. T t = LocalizationManager.FindAsset(value) as T;
  349. if (t)
  350. {
  351. return t;
  352. }
  353. return ResourceManager.pInstance.GetAsset<T>(value);
  354. }
  355. public bool HasTranslatedObject(UnityEngine.Object Obj)
  356. {
  357. return this.TranslatedObjects.Contains(Obj) || ResourceManager.pInstance.HasAsset(Obj);
  358. }
  359. public void AddTranslatedObject(UnityEngine.Object Obj)
  360. {
  361. if (this.TranslatedObjects.Contains(Obj))
  362. {
  363. return;
  364. }
  365. this.TranslatedObjects.Add(Obj);
  366. this.UpdateAssetDictionary();
  367. }
  368. public void SetGlobalLanguage(string Language)
  369. {
  370. LocalizationManager.CurrentLanguage = Language;
  371. }
  372. public string mTerm = string.Empty;
  373. public string mTermSecondary = string.Empty;
  374. [NonSerialized]
  375. public string FinalTerm;
  376. [NonSerialized]
  377. public string FinalSecondaryTerm;
  378. public Localize.TermModification PrimaryTermModifier;
  379. public Localize.TermModification SecondaryTermModifier;
  380. public string TermPrefix;
  381. public string TermSuffix;
  382. public bool LocalizeOnAwake = true;
  383. private string LastLocalizedLanguage;
  384. public bool IgnoreRTL;
  385. public int MaxCharactersInRTL;
  386. public bool IgnoreNumbersInRTL = true;
  387. public bool CorrectAlignmentForRTL = true;
  388. public bool AddSpacesToJoinedLanguages;
  389. public List<UnityEngine.Object> TranslatedObjects = new List<UnityEngine.Object>();
  390. [NonSerialized]
  391. public Dictionary<string, UnityEngine.Object> mAssetDictionary = new Dictionary<string, UnityEngine.Object>(StringComparer.Ordinal);
  392. public UnityEvent LocalizeEvent = new UnityEvent();
  393. public static string MainTranslation;
  394. public static string SecondaryTranslation;
  395. public static string CallBackTerm;
  396. public static string CallBackSecondaryTerm;
  397. public static Localize CurrentLocalizeComponent;
  398. public bool AlwaysForceLocalize;
  399. [SerializeField]
  400. public EventCallback LocalizeCallBack = new EventCallback();
  401. public bool mGUI_ShowReferences;
  402. public bool mGUI_ShowTems = true;
  403. public bool mGUI_ShowCallback;
  404. public ILocalizeTarget mLocalizeTarget;
  405. public string mLocalizeTargetName;
  406. public enum TermModification
  407. {
  408. DontModify,
  409. ToUpper,
  410. ToLower,
  411. ToUpperFirst,
  412. ToTitle
  413. }
  414. }
  415. }