LocalizationManager.cs 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Globalization;
  5. using System.Linq;
  6. using System.Text.RegularExpressions;
  7. using System.Threading;
  8. using UnityEngine;
  9. namespace I2.Loc
  10. {
  11. public static class LocalizationManager
  12. {
  13. public static void InitializeIfNeeded()
  14. {
  15. if (string.IsNullOrEmpty(LocalizationManager.mCurrentLanguage) || LocalizationManager.Sources.Count == 0)
  16. {
  17. LocalizationManager.UpdateSources();
  18. LocalizationManager.SelectStartupLanguage();
  19. }
  20. }
  21. public static string GetVersion()
  22. {
  23. return "2.8.6 f2";
  24. }
  25. public static int GetRequiredWebServiceVersion()
  26. {
  27. return 5;
  28. }
  29. public static string GetWebServiceURL(LanguageSource source = null)
  30. {
  31. if (source != null && !string.IsNullOrEmpty(source.Google_WebServiceURL))
  32. {
  33. return source.Google_WebServiceURL;
  34. }
  35. LocalizationManager.InitializeIfNeeded();
  36. for (int i = 0; i < LocalizationManager.Sources.Count; i++)
  37. {
  38. if (LocalizationManager.Sources[i] != null && !string.IsNullOrEmpty(LocalizationManager.Sources[i].Google_WebServiceURL))
  39. {
  40. return LocalizationManager.Sources[i].Google_WebServiceURL;
  41. }
  42. }
  43. return string.Empty;
  44. }
  45. public static string CurrentLanguage
  46. {
  47. get
  48. {
  49. LocalizationManager.InitializeIfNeeded();
  50. return LocalizationManager.mCurrentLanguage;
  51. }
  52. set
  53. {
  54. LocalizationManager.InitializeIfNeeded();
  55. string supportedLanguage = LocalizationManager.GetSupportedLanguage(value);
  56. if (!string.IsNullOrEmpty(supportedLanguage) && LocalizationManager.mCurrentLanguage != supportedLanguage)
  57. {
  58. LocalizationManager.SetLanguageAndCode(supportedLanguage, LocalizationManager.GetLanguageCode(supportedLanguage), true, false);
  59. }
  60. }
  61. }
  62. public static string CurrentLanguageCode
  63. {
  64. get
  65. {
  66. LocalizationManager.InitializeIfNeeded();
  67. return LocalizationManager.mLanguageCode;
  68. }
  69. set
  70. {
  71. LocalizationManager.InitializeIfNeeded();
  72. if (LocalizationManager.mLanguageCode != value)
  73. {
  74. string languageFromCode = LocalizationManager.GetLanguageFromCode(value, true);
  75. if (!string.IsNullOrEmpty(languageFromCode))
  76. {
  77. LocalizationManager.SetLanguageAndCode(languageFromCode, value, true, false);
  78. }
  79. }
  80. }
  81. }
  82. public static string CurrentRegion
  83. {
  84. get
  85. {
  86. string currentLanguage = LocalizationManager.CurrentLanguage;
  87. int num = currentLanguage.IndexOfAny("/\\".ToCharArray());
  88. if (num > 0)
  89. {
  90. return currentLanguage.Substring(num + 1);
  91. }
  92. num = currentLanguage.IndexOfAny("[(".ToCharArray());
  93. int num2 = currentLanguage.LastIndexOfAny("])".ToCharArray());
  94. if (num > 0 && num != num2)
  95. {
  96. return currentLanguage.Substring(num + 1, num2 - num - 1);
  97. }
  98. return string.Empty;
  99. }
  100. set
  101. {
  102. string text = LocalizationManager.CurrentLanguage;
  103. int num = text.IndexOfAny("/\\".ToCharArray());
  104. if (num > 0)
  105. {
  106. LocalizationManager.CurrentLanguage = text.Substring(num + 1) + value;
  107. return;
  108. }
  109. num = text.IndexOfAny("[(".ToCharArray());
  110. int num2 = text.LastIndexOfAny("])".ToCharArray());
  111. if (num > 0 && num != num2)
  112. {
  113. text = text.Substring(num);
  114. }
  115. LocalizationManager.CurrentLanguage = text + "(" + value + ")";
  116. }
  117. }
  118. public static string CurrentRegionCode
  119. {
  120. get
  121. {
  122. string currentLanguageCode = LocalizationManager.CurrentLanguageCode;
  123. int num = currentLanguageCode.IndexOfAny(" -_/\\".ToCharArray());
  124. return (num >= 0) ? currentLanguageCode.Substring(num + 1) : string.Empty;
  125. }
  126. set
  127. {
  128. string text = LocalizationManager.CurrentLanguageCode;
  129. int num = text.IndexOfAny(" -_/\\".ToCharArray());
  130. if (num > 0)
  131. {
  132. text = text.Substring(0, num);
  133. }
  134. LocalizationManager.CurrentLanguageCode = text + "-" + value;
  135. }
  136. }
  137. public static CultureInfo CurrentCulture
  138. {
  139. get
  140. {
  141. return LocalizationManager.mCurrentCulture;
  142. }
  143. }
  144. public static void SetLanguageAndCode(string LanguageName, string LanguageCode, bool RememberLanguage = true, bool Force = false)
  145. {
  146. if (LocalizationManager.mCurrentLanguage != LanguageName || LocalizationManager.mLanguageCode != LanguageCode || Force)
  147. {
  148. if (RememberLanguage)
  149. {
  150. PersistentStorage.SetSetting_String("I2 Language", LanguageName);
  151. }
  152. LocalizationManager.mCurrentLanguage = LanguageName;
  153. LocalizationManager.mLanguageCode = LanguageCode;
  154. LocalizationManager.mCurrentCulture = LocalizationManager.CreateCultureForCode(LanguageCode);
  155. if (LocalizationManager.mChangeCultureInfo)
  156. {
  157. LocalizationManager.SetCurrentCultureInfo();
  158. }
  159. LocalizationManager.IsRight2Left = LocalizationManager.IsRTL(LocalizationManager.mLanguageCode);
  160. LocalizationManager.HasJoinedWords = GoogleLanguages.LanguageCode_HasJoinedWord(LocalizationManager.mLanguageCode);
  161. LocalizationManager.LocalizeAll(Force);
  162. }
  163. }
  164. private static CultureInfo CreateCultureForCode(string code)
  165. {
  166. CultureInfo result;
  167. try
  168. {
  169. result = CultureInfo.CreateSpecificCulture(code);
  170. }
  171. catch (Exception)
  172. {
  173. result = CultureInfo.InvariantCulture;
  174. }
  175. return result;
  176. }
  177. public static void EnableChangingCultureInfo(bool bEnable)
  178. {
  179. if (!LocalizationManager.mChangeCultureInfo && bEnable)
  180. {
  181. LocalizationManager.SetCurrentCultureInfo();
  182. }
  183. LocalizationManager.mChangeCultureInfo = bEnable;
  184. }
  185. private static void SetCurrentCultureInfo()
  186. {
  187. Thread.CurrentThread.CurrentCulture = LocalizationManager.mCurrentCulture;
  188. }
  189. private static void SelectStartupLanguage()
  190. {
  191. if (LocalizationManager.Sources.Count == 0)
  192. {
  193. return;
  194. }
  195. string setting_String = PersistentStorage.GetSetting_String("I2 Language", string.Empty);
  196. string currentDeviceLanguage = LocalizationManager.GetCurrentDeviceLanguage();
  197. if (!string.IsNullOrEmpty(setting_String) && LocalizationManager.HasLanguage(setting_String, true, false, true))
  198. {
  199. LocalizationManager.SetLanguageAndCode(setting_String, LocalizationManager.GetLanguageCode(setting_String), true, false);
  200. return;
  201. }
  202. if (!LocalizationManager.Sources[0].IgnoreDeviceLanguage)
  203. {
  204. string supportedLanguage = LocalizationManager.GetSupportedLanguage(currentDeviceLanguage);
  205. if (!string.IsNullOrEmpty(supportedLanguage))
  206. {
  207. LocalizationManager.SetLanguageAndCode(supportedLanguage, LocalizationManager.GetLanguageCode(supportedLanguage), false, false);
  208. return;
  209. }
  210. }
  211. int i = 0;
  212. int count = LocalizationManager.Sources.Count;
  213. while (i < count)
  214. {
  215. if (LocalizationManager.Sources[i].mLanguages.Count > 0)
  216. {
  217. for (int j = 0; j < LocalizationManager.Sources[i].mLanguages.Count; j++)
  218. {
  219. if (LocalizationManager.Sources[i].mLanguages[j].IsEnabled())
  220. {
  221. LocalizationManager.SetLanguageAndCode(LocalizationManager.Sources[i].mLanguages[j].Name, LocalizationManager.Sources[i].mLanguages[j].Code, false, false);
  222. return;
  223. }
  224. }
  225. }
  226. i++;
  227. }
  228. }
  229. public static bool HasLanguage(string Language, bool AllowDiscartingRegion = true, bool Initialize = true, bool SkipDisabled = true)
  230. {
  231. if (Initialize)
  232. {
  233. LocalizationManager.InitializeIfNeeded();
  234. }
  235. int i = 0;
  236. int count = LocalizationManager.Sources.Count;
  237. while (i < count)
  238. {
  239. if (LocalizationManager.Sources[i].GetLanguageIndex(Language, false, SkipDisabled) >= 0)
  240. {
  241. return true;
  242. }
  243. i++;
  244. }
  245. if (AllowDiscartingRegion)
  246. {
  247. int j = 0;
  248. int count2 = LocalizationManager.Sources.Count;
  249. while (j < count2)
  250. {
  251. if (LocalizationManager.Sources[j].GetLanguageIndex(Language, true, SkipDisabled) >= 0)
  252. {
  253. return true;
  254. }
  255. j++;
  256. }
  257. }
  258. return false;
  259. }
  260. public static string GetSupportedLanguage(string Language)
  261. {
  262. string languageCode = GoogleLanguages.GetLanguageCode(Language, false);
  263. if (!string.IsNullOrEmpty(languageCode))
  264. {
  265. int i = 0;
  266. int count = LocalizationManager.Sources.Count;
  267. while (i < count)
  268. {
  269. int languageIndexFromCode = LocalizationManager.Sources[i].GetLanguageIndexFromCode(languageCode, true);
  270. if (languageIndexFromCode >= 0)
  271. {
  272. return LocalizationManager.Sources[i].mLanguages[languageIndexFromCode].Name;
  273. }
  274. i++;
  275. }
  276. int j = 0;
  277. int count2 = LocalizationManager.Sources.Count;
  278. while (j < count2)
  279. {
  280. int languageIndexFromCode2 = LocalizationManager.Sources[j].GetLanguageIndexFromCode(languageCode, false);
  281. if (languageIndexFromCode2 >= 0)
  282. {
  283. return LocalizationManager.Sources[j].mLanguages[languageIndexFromCode2].Name;
  284. }
  285. j++;
  286. }
  287. }
  288. int k = 0;
  289. int count3 = LocalizationManager.Sources.Count;
  290. while (k < count3)
  291. {
  292. int languageIndex = LocalizationManager.Sources[k].GetLanguageIndex(Language, false, true);
  293. if (languageIndex >= 0)
  294. {
  295. return LocalizationManager.Sources[k].mLanguages[languageIndex].Name;
  296. }
  297. k++;
  298. }
  299. int l = 0;
  300. int count4 = LocalizationManager.Sources.Count;
  301. while (l < count4)
  302. {
  303. int languageIndex2 = LocalizationManager.Sources[l].GetLanguageIndex(Language, true, true);
  304. if (languageIndex2 >= 0)
  305. {
  306. return LocalizationManager.Sources[l].mLanguages[languageIndex2].Name;
  307. }
  308. l++;
  309. }
  310. return string.Empty;
  311. }
  312. public static string GetLanguageCode(string Language)
  313. {
  314. if (LocalizationManager.Sources.Count == 0)
  315. {
  316. LocalizationManager.UpdateSources();
  317. }
  318. int i = 0;
  319. int count = LocalizationManager.Sources.Count;
  320. while (i < count)
  321. {
  322. int languageIndex = LocalizationManager.Sources[i].GetLanguageIndex(Language, true, true);
  323. if (languageIndex >= 0)
  324. {
  325. return LocalizationManager.Sources[i].mLanguages[languageIndex].Code;
  326. }
  327. i++;
  328. }
  329. return string.Empty;
  330. }
  331. public static string GetLanguageFromCode(string Code, bool exactMatch = true)
  332. {
  333. if (LocalizationManager.Sources.Count == 0)
  334. {
  335. LocalizationManager.UpdateSources();
  336. }
  337. int i = 0;
  338. int count = LocalizationManager.Sources.Count;
  339. while (i < count)
  340. {
  341. int languageIndexFromCode = LocalizationManager.Sources[i].GetLanguageIndexFromCode(Code, exactMatch);
  342. if (languageIndexFromCode >= 0)
  343. {
  344. return LocalizationManager.Sources[i].mLanguages[languageIndexFromCode].Name;
  345. }
  346. i++;
  347. }
  348. return string.Empty;
  349. }
  350. public static List<string> GetAllLanguages(bool SkipDisabled = true)
  351. {
  352. if (LocalizationManager.Sources.Count == 0)
  353. {
  354. LocalizationManager.UpdateSources();
  355. }
  356. List<string> Languages = new List<string>();
  357. int i = 0;
  358. int count = LocalizationManager.Sources.Count;
  359. while (i < count)
  360. {
  361. Languages.AddRange(from x in LocalizationManager.Sources[i].GetLanguages(SkipDisabled)
  362. where !Languages.Contains(x)
  363. select x);
  364. i++;
  365. }
  366. return Languages;
  367. }
  368. public static List<string> GetAllLanguagesCode(bool allowRegions = true, bool SkipDisabled = true)
  369. {
  370. List<string> Languages = new List<string>();
  371. int i = 0;
  372. int count = LocalizationManager.Sources.Count;
  373. while (i < count)
  374. {
  375. Languages.AddRange(from x in LocalizationManager.Sources[i].GetLanguagesCode(allowRegions, SkipDisabled)
  376. where !Languages.Contains(x)
  377. select x);
  378. i++;
  379. }
  380. return Languages;
  381. }
  382. public static bool IsLanguageEnabled(string Language)
  383. {
  384. int i = 0;
  385. int count = LocalizationManager.Sources.Count;
  386. while (i < count)
  387. {
  388. if (!LocalizationManager.Sources[i].IsLanguageEnabled(Language))
  389. {
  390. return false;
  391. }
  392. i++;
  393. }
  394. return true;
  395. }
  396. private static void LoadCurrentLanguage()
  397. {
  398. for (int i = 0; i < LocalizationManager.Sources.Count; i++)
  399. {
  400. int languageIndex = LocalizationManager.Sources[i].GetLanguageIndex(LocalizationManager.mCurrentLanguage, true, false);
  401. LocalizationManager.Sources[i].LoadLanguage(languageIndex, true, true, true, false);
  402. }
  403. }
  404. public static void ApplyLocalizationParams(ref string translation)
  405. {
  406. LocalizationManager.ApplyLocalizationParams(ref translation, (string p) => LocalizationManager.GetLocalizationParam(p, null));
  407. }
  408. public static void ApplyLocalizationParams(ref string translation, GameObject root)
  409. {
  410. LocalizationManager.ApplyLocalizationParams(ref translation, (string p) => LocalizationManager.GetLocalizationParam(p, root));
  411. }
  412. public static void ApplyLocalizationParams(ref string translation, Dictionary<string, object> parameters)
  413. {
  414. LocalizationManager.ApplyLocalizationParams(ref translation, delegate(string p)
  415. {
  416. object result = null;
  417. if (parameters.TryGetValue(p, out result))
  418. {
  419. return result;
  420. }
  421. return null;
  422. });
  423. }
  424. public static void ApplyLocalizationParams(ref string translation, LocalizationManager._GetParam getParam)
  425. {
  426. if (translation == null)
  427. {
  428. return;
  429. }
  430. string text = null;
  431. int num = translation.Length;
  432. int num2 = 0;
  433. while (num2 >= 0 && num2 < translation.Length)
  434. {
  435. int num3 = translation.IndexOf("{[", num2);
  436. if (num3 < 0)
  437. {
  438. break;
  439. }
  440. int num4 = translation.IndexOf("]}", num3);
  441. if (num4 < 0)
  442. {
  443. break;
  444. }
  445. int num5 = translation.IndexOf("{[", num3 + 1);
  446. if (num5 > 0 && num5 < num4)
  447. {
  448. num2 = num5;
  449. }
  450. else
  451. {
  452. int num6 = (translation[num3 + 2] != '#') ? 2 : 3;
  453. string param = translation.Substring(num3 + num6, num4 - num3 - num6);
  454. string text2 = (string)getParam(param);
  455. if (text2 != null)
  456. {
  457. string text3;
  458. if (LocalizationManager.TryGetTranslation(text2, out text3, true, 0, true, false, null, null))
  459. {
  460. text2 = text3;
  461. }
  462. string oldValue = translation.Substring(num3, num4 - num3 + 2);
  463. translation = translation.Replace(oldValue, text2);
  464. int n = 0;
  465. if (int.TryParse(text2, out n))
  466. {
  467. text = GoogleLanguages.GetPluralType(LocalizationManager.CurrentLanguageCode, n).ToString();
  468. }
  469. num2 = num3 + text2.Length;
  470. }
  471. else
  472. {
  473. num2 = num4 + 2;
  474. }
  475. }
  476. }
  477. if (text != null)
  478. {
  479. string text4 = "[i2p_" + text + "]";
  480. int num7 = translation.IndexOf(text4, StringComparison.OrdinalIgnoreCase);
  481. if (num7 < 0)
  482. {
  483. num7 = 0;
  484. }
  485. else
  486. {
  487. num7 += text4.Length;
  488. }
  489. num = translation.IndexOf("[i2p_", num7 + 1, StringComparison.OrdinalIgnoreCase);
  490. if (num < 0)
  491. {
  492. num = translation.Length;
  493. }
  494. translation = translation.Substring(num7, num - num7);
  495. }
  496. }
  497. internal static string GetLocalizationParam(string ParamName, GameObject root)
  498. {
  499. if (root)
  500. {
  501. MonoBehaviour[] components = root.GetComponents<MonoBehaviour>();
  502. int i = 0;
  503. int num = components.Length;
  504. while (i < num)
  505. {
  506. ILocalizationParamsManager localizationParamsManager = components[i] as ILocalizationParamsManager;
  507. if (localizationParamsManager != null && components[i].enabled)
  508. {
  509. string parameterValue = localizationParamsManager.GetParameterValue(ParamName);
  510. if (parameterValue != null)
  511. {
  512. return parameterValue;
  513. }
  514. }
  515. i++;
  516. }
  517. }
  518. int j = 0;
  519. int count = LocalizationManager.ParamManagers.Count;
  520. while (j < count)
  521. {
  522. string parameterValue = LocalizationManager.ParamManagers[j].GetParameterValue(ParamName);
  523. if (parameterValue != null)
  524. {
  525. return parameterValue;
  526. }
  527. j++;
  528. }
  529. return null;
  530. }
  531. private static string GetPluralType(MatchCollection matches, string langCode, LocalizationManager._GetParam getParam)
  532. {
  533. int i = 0;
  534. int count = matches.Count;
  535. while (i < count)
  536. {
  537. Match match = matches[i];
  538. string value = match.Groups[match.Groups.Count - 1].Value;
  539. string text = (string)getParam(value);
  540. if (text != null)
  541. {
  542. int n = 0;
  543. if (int.TryParse(text, out n))
  544. {
  545. return GoogleLanguages.GetPluralType(langCode, n).ToString();
  546. }
  547. }
  548. i++;
  549. }
  550. return null;
  551. }
  552. public static string ApplyRTLfix(string line)
  553. {
  554. return LocalizationManager.ApplyRTLfix(line, 0, true);
  555. }
  556. public static string ApplyRTLfix(string line, int maxCharacters, bool ignoreNumbers)
  557. {
  558. if (string.IsNullOrEmpty(line))
  559. {
  560. return line;
  561. }
  562. char c = line[0];
  563. if (c == '!' || c == '.' || c == '?')
  564. {
  565. line = line.Substring(1) + c;
  566. }
  567. int num = -1;
  568. int num2 = 0;
  569. int num3 = 40000;
  570. num2 = 0;
  571. List<string> list = new List<string>();
  572. while (I2Utils.FindNextTag(line, num2, out num, out num2))
  573. {
  574. string str = "@@" + (char)(num3 + list.Count) + "@@";
  575. list.Add(line.Substring(num, num2 - num + 1));
  576. line = line.Substring(0, num) + str + line.Substring(num2 + 1);
  577. num2 = num + 5;
  578. }
  579. line = line.Replace("\r\n", "\n");
  580. line = I2Utils.SplitLine(line, maxCharacters);
  581. line = RTLFixer.Fix(line, true, !ignoreNumbers);
  582. for (int i = 0; i < list.Count; i++)
  583. {
  584. int length = line.Length;
  585. for (int j = 0; j < length; j++)
  586. {
  587. if (line[j] == '@' && line[j + 1] == '@' && (int)line[j + 2] >= num3 && line[j + 3] == '@' && line[j + 4] == '@')
  588. {
  589. int num4 = (int)line[j + 2] - num3;
  590. if (num4 % 2 == 0)
  591. {
  592. num4++;
  593. }
  594. else
  595. {
  596. num4--;
  597. }
  598. if (num4 >= list.Count)
  599. {
  600. num4 = list.Count - 1;
  601. }
  602. line = line.Substring(0, j) + list[num4] + line.Substring(j + 5);
  603. break;
  604. }
  605. }
  606. }
  607. return line;
  608. }
  609. public static string FixRTL_IfNeeded(string text, int maxCharacters = 0, bool ignoreNumber = false)
  610. {
  611. if (LocalizationManager.IsRight2Left)
  612. {
  613. return LocalizationManager.ApplyRTLfix(text, maxCharacters, ignoreNumber);
  614. }
  615. return text;
  616. }
  617. public static bool IsRTL(string Code)
  618. {
  619. return Array.IndexOf<string>(LocalizationManager.LanguagesRTL, Code) >= 0;
  620. }
  621. public static bool UpdateSources()
  622. {
  623. LocalizationManager.UnregisterDeletededSources();
  624. LocalizationManager.RegisterSourceInResources();
  625. LocalizationManager.RegisterSceneSources();
  626. return LocalizationManager.Sources.Count > 0;
  627. }
  628. private static void UnregisterDeletededSources()
  629. {
  630. for (int i = LocalizationManager.Sources.Count - 1; i >= 0; i--)
  631. {
  632. if (LocalizationManager.Sources[i] == null)
  633. {
  634. LocalizationManager.RemoveSource(LocalizationManager.Sources[i]);
  635. }
  636. }
  637. }
  638. private static void RegisterSceneSources()
  639. {
  640. LanguageSource[] array = (LanguageSource[])Resources.FindObjectsOfTypeAll(typeof(LanguageSource));
  641. int i = 0;
  642. int num = array.Length;
  643. while (i < num)
  644. {
  645. if (!LocalizationManager.Sources.Contains(array[i]))
  646. {
  647. LocalizationManager.AddSource(array[i]);
  648. }
  649. i++;
  650. }
  651. }
  652. private static void RegisterSourceInResources()
  653. {
  654. foreach (string name in LocalizationManager.GlobalSources)
  655. {
  656. GameObject asset = ResourceManager.pInstance.GetAsset<GameObject>(name);
  657. LanguageSource languageSource = (!asset) ? null : asset.GetComponent<LanguageSource>();
  658. if (languageSource && !LocalizationManager.Sources.Contains(languageSource))
  659. {
  660. languageSource.mIsGlobalSource = true;
  661. LocalizationManager.AddSource(languageSource);
  662. }
  663. }
  664. }
  665. internal static void AddSource(LanguageSource Source)
  666. {
  667. if (LocalizationManager.Sources.Contains(Source))
  668. {
  669. return;
  670. }
  671. LocalizationManager.Sources.Add(Source);
  672. if (Source.HasGoogleSpreadsheet() && Source.GoogleUpdateFrequency != LanguageSource.eGoogleUpdateFrequency.Never)
  673. {
  674. Source.Import_Google_FromCache();
  675. bool justCheck = false;
  676. if (Source.GoogleUpdateDelay > 0f)
  677. {
  678. CoroutineManager.Start(LocalizationManager.Delayed_Import_Google(Source, Source.GoogleUpdateDelay, justCheck));
  679. }
  680. else
  681. {
  682. Source.Import_Google(false, justCheck);
  683. }
  684. }
  685. for (int i = 0; i < Source.mLanguages.Count<LanguageData>(); i++)
  686. {
  687. Source.mLanguages[i].SetLoaded(true);
  688. }
  689. if (Source.mDictionary.Count == 0)
  690. {
  691. Source.UpdateDictionary(true);
  692. }
  693. }
  694. private static IEnumerator Delayed_Import_Google(LanguageSource source, float delay, bool justCheck)
  695. {
  696. yield return new WaitForSeconds(delay);
  697. source.Import_Google(false, justCheck);
  698. yield break;
  699. }
  700. internal static void RemoveSource(LanguageSource Source)
  701. {
  702. LocalizationManager.Sources.Remove(Source);
  703. }
  704. public static bool IsGlobalSource(string SourceName)
  705. {
  706. return Array.IndexOf<string>(LocalizationManager.GlobalSources, SourceName) >= 0;
  707. }
  708. public static LanguageSource GetSourceContaining(string term, bool fallbackToFirst = true)
  709. {
  710. if (!string.IsNullOrEmpty(term))
  711. {
  712. int i = 0;
  713. int count = LocalizationManager.Sources.Count;
  714. while (i < count)
  715. {
  716. if (LocalizationManager.Sources[i].GetTermData(term, false) != null)
  717. {
  718. return LocalizationManager.Sources[i];
  719. }
  720. i++;
  721. }
  722. }
  723. return (!fallbackToFirst || LocalizationManager.Sources.Count <= 0) ? null : LocalizationManager.Sources[0];
  724. }
  725. public static UnityEngine.Object FindAsset(string value)
  726. {
  727. int i = 0;
  728. int count = LocalizationManager.Sources.Count;
  729. while (i < count)
  730. {
  731. UnityEngine.Object @object = LocalizationManager.Sources[i].FindAsset(value);
  732. if (@object)
  733. {
  734. return @object;
  735. }
  736. i++;
  737. }
  738. return null;
  739. }
  740. public static string GetCurrentDeviceLanguage()
  741. {
  742. if (string.IsNullOrEmpty(LocalizationManager.mCurrentDeviceLanguage))
  743. {
  744. LocalizationManager.DetectDeviceLanguage();
  745. }
  746. return LocalizationManager.mCurrentDeviceLanguage;
  747. }
  748. private static void DetectDeviceLanguage()
  749. {
  750. LocalizationManager.mCurrentDeviceLanguage = Application.systemLanguage.ToString();
  751. if (LocalizationManager.mCurrentDeviceLanguage == "ChineseSimplified")
  752. {
  753. LocalizationManager.mCurrentDeviceLanguage = "Chinese (Simplified)";
  754. }
  755. if (LocalizationManager.mCurrentDeviceLanguage == "ChineseTraditional")
  756. {
  757. LocalizationManager.mCurrentDeviceLanguage = "Chinese (Traditional)";
  758. }
  759. }
  760. public static void RegisterTarget(ILocalizeTargetDescriptor desc)
  761. {
  762. if (LocalizationManager.mLocalizeTargets.FindIndex((ILocalizeTargetDescriptor x) => x.Name == desc.Name) != -1)
  763. {
  764. return;
  765. }
  766. for (int i = 0; i < LocalizationManager.mLocalizeTargets.Count; i++)
  767. {
  768. if (LocalizationManager.mLocalizeTargets[i].Priority > desc.Priority)
  769. {
  770. LocalizationManager.mLocalizeTargets.Insert(i, desc);
  771. return;
  772. }
  773. }
  774. LocalizationManager.mLocalizeTargets.Add(desc);
  775. }
  776. public static event LocalizationManager.OnLocalizeCallback OnLocalizeEvent;
  777. public static string GetTranslation(string Term, bool FixForRTL = true, int maxLineLengthForRTL = 0, bool ignoreRTLnumbers = true, bool applyParameters = false, GameObject localParametersRoot = null, string overrideLanguage = null)
  778. {
  779. string result = null;
  780. LocalizationManager.TryGetTranslation(Term, out result, FixForRTL, maxLineLengthForRTL, ignoreRTLnumbers, applyParameters, localParametersRoot, overrideLanguage);
  781. return result;
  782. }
  783. public static string GetTermTranslation(string Term, bool FixForRTL = true, int maxLineLengthForRTL = 0, bool ignoreRTLnumbers = true, bool applyParameters = false, GameObject localParametersRoot = null, string overrideLanguage = null)
  784. {
  785. return LocalizationManager.GetTranslation(Term, FixForRTL, maxLineLengthForRTL, ignoreRTLnumbers, applyParameters, localParametersRoot, overrideLanguage);
  786. }
  787. public static bool TryGetTranslation(string Term, out string Translation, bool FixForRTL = true, int maxLineLengthForRTL = 0, bool ignoreRTLnumbers = true, bool applyParameters = false, GameObject localParametersRoot = null, string overrideLanguage = null)
  788. {
  789. Translation = null;
  790. if (string.IsNullOrEmpty(Term))
  791. {
  792. return false;
  793. }
  794. LocalizationManager.InitializeIfNeeded();
  795. int i = 0;
  796. int count = LocalizationManager.Sources.Count;
  797. while (i < count)
  798. {
  799. if (LocalizationManager.Sources[i].TryGetTranslation(Term, out Translation, overrideLanguage, null, false, false))
  800. {
  801. if (applyParameters)
  802. {
  803. LocalizationManager.ApplyLocalizationParams(ref Translation, localParametersRoot);
  804. }
  805. if (LocalizationManager.IsRight2Left && FixForRTL)
  806. {
  807. Translation = LocalizationManager.ApplyRTLfix(Translation, maxLineLengthForRTL, ignoreRTLnumbers);
  808. }
  809. return true;
  810. }
  811. i++;
  812. }
  813. return false;
  814. }
  815. public static string GetAppName(string languageCode)
  816. {
  817. if (!string.IsNullOrEmpty(languageCode))
  818. {
  819. for (int i = 0; i < LocalizationManager.Sources.Count; i++)
  820. {
  821. if (!string.IsNullOrEmpty(LocalizationManager.Sources[i].mTerm_AppName))
  822. {
  823. int languageIndexFromCode = LocalizationManager.Sources[i].GetLanguageIndexFromCode(languageCode, false);
  824. if (languageIndexFromCode >= 0)
  825. {
  826. TermData termData = LocalizationManager.Sources[i].GetTermData(LocalizationManager.Sources[i].mTerm_AppName, false);
  827. if (termData != null)
  828. {
  829. string translation = termData.GetTranslation(languageIndexFromCode, null);
  830. if (!string.IsNullOrEmpty(translation))
  831. {
  832. return translation;
  833. }
  834. }
  835. }
  836. }
  837. }
  838. }
  839. return Application.productName;
  840. }
  841. public static void LocalizeAll(bool Force = false)
  842. {
  843. LocalizationManager.LoadCurrentLanguage();
  844. if (!Application.isPlaying)
  845. {
  846. LocalizationManager.DoLocalizeAll(Force);
  847. return;
  848. }
  849. LocalizationManager.mLocalizeIsScheduledWithForcedValue = (LocalizationManager.mLocalizeIsScheduledWithForcedValue || Force);
  850. if (LocalizationManager.mLocalizeIsScheduled)
  851. {
  852. return;
  853. }
  854. CoroutineManager.Start(LocalizationManager.Coroutine_LocalizeAll());
  855. }
  856. private static IEnumerator Coroutine_LocalizeAll()
  857. {
  858. LocalizationManager.mLocalizeIsScheduled = true;
  859. yield return null;
  860. LocalizationManager.mLocalizeIsScheduled = false;
  861. bool force = LocalizationManager.mLocalizeIsScheduledWithForcedValue;
  862. LocalizationManager.mLocalizeIsScheduledWithForcedValue = false;
  863. LocalizationManager.DoLocalizeAll(force);
  864. yield break;
  865. }
  866. private static void DoLocalizeAll(bool Force = false)
  867. {
  868. Localize[] array = (Localize[])Resources.FindObjectsOfTypeAll(typeof(Localize));
  869. int i = 0;
  870. int num = array.Length;
  871. while (i < num)
  872. {
  873. Localize localize = array[i];
  874. localize.OnLocalize(Force);
  875. i++;
  876. }
  877. if (LocalizationManager.OnLocalizeEvent != null)
  878. {
  879. LocalizationManager.OnLocalizeEvent();
  880. }
  881. ResourceManager.pInstance.CleanResourceCache();
  882. }
  883. public static List<string> GetCategories()
  884. {
  885. List<string> list = new List<string>();
  886. int i = 0;
  887. int count = LocalizationManager.Sources.Count;
  888. while (i < count)
  889. {
  890. LocalizationManager.Sources[i].GetCategories(false, list);
  891. i++;
  892. }
  893. return list;
  894. }
  895. public static List<string> GetTermsList(string Category = null)
  896. {
  897. if (LocalizationManager.Sources.Count == 0)
  898. {
  899. LocalizationManager.UpdateSources();
  900. }
  901. if (LocalizationManager.Sources.Count == 1)
  902. {
  903. return LocalizationManager.Sources[0].GetTermsList(Category);
  904. }
  905. HashSet<string> hashSet = new HashSet<string>();
  906. int i = 0;
  907. int count = LocalizationManager.Sources.Count;
  908. while (i < count)
  909. {
  910. hashSet.UnionWith(LocalizationManager.Sources[i].GetTermsList(Category));
  911. i++;
  912. }
  913. return new List<string>(hashSet);
  914. }
  915. public static TermData GetTermData(string term)
  916. {
  917. LocalizationManager.InitializeIfNeeded();
  918. int i = 0;
  919. int count = LocalizationManager.Sources.Count;
  920. while (i < count)
  921. {
  922. TermData termData = LocalizationManager.Sources[i].GetTermData(term, false);
  923. if (termData != null)
  924. {
  925. return termData;
  926. }
  927. i++;
  928. }
  929. return null;
  930. }
  931. private static string mCurrentLanguage;
  932. private static string mLanguageCode;
  933. private static CultureInfo mCurrentCulture;
  934. private static bool mChangeCultureInfo = false;
  935. public static bool IsRight2Left = false;
  936. public static bool HasJoinedWords = false;
  937. public static List<ILocalizationParamsManager> ParamManagers = new List<ILocalizationParamsManager>();
  938. private static string[] LanguagesRTL = new string[]
  939. {
  940. "ar-DZ",
  941. "ar",
  942. "ar-BH",
  943. "ar-EG",
  944. "ar-IQ",
  945. "ar-JO",
  946. "ar-KW",
  947. "ar-LB",
  948. "ar-LY",
  949. "ar-MA",
  950. "ar-OM",
  951. "ar-QA",
  952. "ar-SA",
  953. "ar-SY",
  954. "ar-TN",
  955. "ar-AE",
  956. "ar-YE",
  957. "he",
  958. "ur",
  959. "ji"
  960. };
  961. public static List<LanguageSource> Sources = new List<LanguageSource>();
  962. public static string[] GlobalSources = new string[]
  963. {
  964. "I2Languages"
  965. };
  966. private static string mCurrentDeviceLanguage;
  967. public static List<ILocalizeTargetDescriptor> mLocalizeTargets = new List<ILocalizeTargetDescriptor>();
  968. private static bool mLocalizeIsScheduled = false;
  969. private static bool mLocalizeIsScheduledWithForcedValue = false;
  970. public static bool HighlightLocalizedTargets = false;
  971. public delegate object _GetParam(string param);
  972. public delegate void OnLocalizeCallback();
  973. }
  974. }