Localize.cs 14 KB

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