LocalizationManager.cs 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067
  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. 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. [DebuggerBrowsable(DebuggerBrowsableState.Never)]
  777. public static event LocalizationManager.OnLocalizeCallback OnLocalizeEvent;
  778. public static string GetTranslation(string Term, bool FixForRTL = true, int maxLineLengthForRTL = 0, bool ignoreRTLnumbers = true, bool applyParameters = false, GameObject localParametersRoot = null, string overrideLanguage = null)
  779. {
  780. string result = null;
  781. LocalizationManager.TryGetTranslation(Term, out result, FixForRTL, maxLineLengthForRTL, ignoreRTLnumbers, applyParameters, localParametersRoot, overrideLanguage);
  782. return result;
  783. }
  784. public static string GetTermTranslation(string Term, bool FixForRTL = true, int maxLineLengthForRTL = 0, bool ignoreRTLnumbers = true, bool applyParameters = false, GameObject localParametersRoot = null, string overrideLanguage = null)
  785. {
  786. return LocalizationManager.GetTranslation(Term, FixForRTL, maxLineLengthForRTL, ignoreRTLnumbers, applyParameters, localParametersRoot, overrideLanguage);
  787. }
  788. 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)
  789. {
  790. Translation = null;
  791. if (string.IsNullOrEmpty(Term))
  792. {
  793. return false;
  794. }
  795. LocalizationManager.InitializeIfNeeded();
  796. int i = 0;
  797. int count = LocalizationManager.Sources.Count;
  798. while (i < count)
  799. {
  800. if (LocalizationManager.Sources[i].TryGetTranslation(Term, out Translation, overrideLanguage, null, false, false))
  801. {
  802. if (applyParameters)
  803. {
  804. LocalizationManager.ApplyLocalizationParams(ref Translation, localParametersRoot);
  805. }
  806. if (LocalizationManager.IsRight2Left && FixForRTL)
  807. {
  808. Translation = LocalizationManager.ApplyRTLfix(Translation, maxLineLengthForRTL, ignoreRTLnumbers);
  809. }
  810. return true;
  811. }
  812. i++;
  813. }
  814. return false;
  815. }
  816. public static string GetAppName(string languageCode)
  817. {
  818. if (!string.IsNullOrEmpty(languageCode))
  819. {
  820. for (int i = 0; i < LocalizationManager.Sources.Count; i++)
  821. {
  822. if (!string.IsNullOrEmpty(LocalizationManager.Sources[i].mTerm_AppName))
  823. {
  824. int languageIndexFromCode = LocalizationManager.Sources[i].GetLanguageIndexFromCode(languageCode, false);
  825. if (languageIndexFromCode >= 0)
  826. {
  827. TermData termData = LocalizationManager.Sources[i].GetTermData(LocalizationManager.Sources[i].mTerm_AppName, false);
  828. if (termData != null)
  829. {
  830. string translation = termData.GetTranslation(languageIndexFromCode, null);
  831. if (!string.IsNullOrEmpty(translation))
  832. {
  833. return translation;
  834. }
  835. }
  836. }
  837. }
  838. }
  839. }
  840. return Application.productName;
  841. }
  842. public static void LocalizeAll(bool Force = false)
  843. {
  844. LocalizationManager.LoadCurrentLanguage();
  845. if (!Application.isPlaying)
  846. {
  847. LocalizationManager.DoLocalizeAll(Force);
  848. return;
  849. }
  850. LocalizationManager.mLocalizeIsScheduledWithForcedValue = (LocalizationManager.mLocalizeIsScheduledWithForcedValue || Force);
  851. if (LocalizationManager.mLocalizeIsScheduled)
  852. {
  853. return;
  854. }
  855. CoroutineManager.Start(LocalizationManager.Coroutine_LocalizeAll());
  856. }
  857. private static IEnumerator Coroutine_LocalizeAll()
  858. {
  859. LocalizationManager.mLocalizeIsScheduled = true;
  860. yield return null;
  861. LocalizationManager.mLocalizeIsScheduled = false;
  862. bool force = LocalizationManager.mLocalizeIsScheduledWithForcedValue;
  863. LocalizationManager.mLocalizeIsScheduledWithForcedValue = false;
  864. LocalizationManager.DoLocalizeAll(force);
  865. yield break;
  866. }
  867. private static void DoLocalizeAll(bool Force = false)
  868. {
  869. Localize[] array = (Localize[])Resources.FindObjectsOfTypeAll(typeof(Localize));
  870. int i = 0;
  871. int num = array.Length;
  872. while (i < num)
  873. {
  874. Localize localize = array[i];
  875. localize.OnLocalize(Force);
  876. i++;
  877. }
  878. if (LocalizationManager.OnLocalizeEvent != null)
  879. {
  880. LocalizationManager.OnLocalizeEvent();
  881. }
  882. ResourceManager.pInstance.CleanResourceCache();
  883. }
  884. public static List<string> GetCategories()
  885. {
  886. List<string> list = new List<string>();
  887. int i = 0;
  888. int count = LocalizationManager.Sources.Count;
  889. while (i < count)
  890. {
  891. LocalizationManager.Sources[i].GetCategories(false, list);
  892. i++;
  893. }
  894. return list;
  895. }
  896. public static List<string> GetTermsList(string Category = null)
  897. {
  898. if (LocalizationManager.Sources.Count == 0)
  899. {
  900. LocalizationManager.UpdateSources();
  901. }
  902. if (LocalizationManager.Sources.Count == 1)
  903. {
  904. return LocalizationManager.Sources[0].GetTermsList(Category);
  905. }
  906. HashSet<string> hashSet = new HashSet<string>();
  907. int i = 0;
  908. int count = LocalizationManager.Sources.Count;
  909. while (i < count)
  910. {
  911. hashSet.UnionWith(LocalizationManager.Sources[i].GetTermsList(Category));
  912. i++;
  913. }
  914. return new List<string>(hashSet);
  915. }
  916. public static TermData GetTermData(string term)
  917. {
  918. LocalizationManager.InitializeIfNeeded();
  919. int i = 0;
  920. int count = LocalizationManager.Sources.Count;
  921. while (i < count)
  922. {
  923. TermData termData = LocalizationManager.Sources[i].GetTermData(term, false);
  924. if (termData != null)
  925. {
  926. return termData;
  927. }
  928. i++;
  929. }
  930. return null;
  931. }
  932. public static bool ContainsTerm(string term)
  933. {
  934. if (LocalizationManager.Sources.Count == 0)
  935. {
  936. LocalizationManager.UpdateSources();
  937. }
  938. int i = 0;
  939. int count = LocalizationManager.Sources.Count;
  940. while (i < count)
  941. {
  942. if (LocalizationManager.Sources[i].ContainsTerm(term))
  943. {
  944. return true;
  945. }
  946. i++;
  947. }
  948. return false;
  949. }
  950. private static string mCurrentLanguage;
  951. private static string mLanguageCode;
  952. private static CultureInfo mCurrentCulture;
  953. private static bool mChangeCultureInfo = false;
  954. public static bool IsRight2Left = false;
  955. public static bool HasJoinedWords = false;
  956. public static List<ILocalizationParamsManager> ParamManagers = new List<ILocalizationParamsManager>();
  957. private static string[] LanguagesRTL = new string[]
  958. {
  959. "ar-DZ",
  960. "ar",
  961. "ar-BH",
  962. "ar-EG",
  963. "ar-IQ",
  964. "ar-JO",
  965. "ar-KW",
  966. "ar-LB",
  967. "ar-LY",
  968. "ar-MA",
  969. "ar-OM",
  970. "ar-QA",
  971. "ar-SA",
  972. "ar-SY",
  973. "ar-TN",
  974. "ar-AE",
  975. "ar-YE",
  976. "he",
  977. "ur",
  978. "ji"
  979. };
  980. public static List<LanguageSource> Sources = new List<LanguageSource>();
  981. public static string[] GlobalSources = new string[]
  982. {
  983. "I2Languages"
  984. };
  985. private static string mCurrentDeviceLanguage;
  986. public static List<ILocalizeTargetDescriptor> mLocalizeTargets = new List<ILocalizeTargetDescriptor>();
  987. private static bool mLocalizeIsScheduled = false;
  988. private static bool mLocalizeIsScheduledWithForcedValue = false;
  989. public static bool HighlightLocalizedTargets = false;
  990. public delegate object _GetParam(string param);
  991. public delegate void OnLocalizeCallback();
  992. }
  993. }