LocalizationManager.cs 28 KB

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