GoogleTranslation.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Globalization;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Text.RegularExpressions;
  8. using UnityEngine;
  9. namespace I2.Loc
  10. {
  11. public static class GoogleTranslation
  12. {
  13. public static bool CanTranslate()
  14. {
  15. return LocalizationManager.Sources.Count > 0 && !string.IsNullOrEmpty(LocalizationManager.GetWebServiceURL(null));
  16. }
  17. public static void Translate(string text, string LanguageCodeFrom, string LanguageCodeTo, Action<string, string> OnTranslationReady)
  18. {
  19. LocalizationManager.InitializeIfNeeded();
  20. if (!GoogleTranslation.CanTranslate())
  21. {
  22. OnTranslationReady(null, "WebService is not set correctly or needs to be reinstalled");
  23. return;
  24. }
  25. if (LanguageCodeTo == LanguageCodeFrom)
  26. {
  27. OnTranslationReady(text, null);
  28. return;
  29. }
  30. Dictionary<string, TranslationQuery> queries = new Dictionary<string, TranslationQuery>();
  31. if (string.IsNullOrEmpty(LanguageCodeTo))
  32. {
  33. OnTranslationReady(string.Empty, null);
  34. return;
  35. }
  36. GoogleTranslation.CreateQueries(text, LanguageCodeFrom, LanguageCodeTo, queries);
  37. GoogleTranslation.Translate(queries, delegate(Dictionary<string, TranslationQuery> results, string error)
  38. {
  39. if (!string.IsNullOrEmpty(error) || results.Count == 0)
  40. {
  41. OnTranslationReady(null, error);
  42. return;
  43. }
  44. string arg = GoogleTranslation.RebuildTranslation(text, queries, LanguageCodeTo);
  45. OnTranslationReady(arg, null);
  46. }, true);
  47. }
  48. public static string ForceTranslate(string text, string LanguageCodeFrom, string LanguageCodeTo)
  49. {
  50. Dictionary<string, TranslationQuery> dictionary = new Dictionary<string, TranslationQuery>();
  51. GoogleTranslation.AddQuery(text, LanguageCodeFrom, LanguageCodeTo, dictionary);
  52. TranslationJob_Main translationJob_Main = new TranslationJob_Main(dictionary, null);
  53. TranslationJob.eJobState state;
  54. do
  55. {
  56. state = translationJob_Main.GetState();
  57. }
  58. while (state == TranslationJob.eJobState.Running);
  59. if (state == TranslationJob.eJobState.Failed)
  60. {
  61. return null;
  62. }
  63. return GoogleTranslation.GetQueryResult(text, string.Empty, dictionary);
  64. }
  65. public static void Translate(Dictionary<string, TranslationQuery> requests, Action<Dictionary<string, TranslationQuery>, string> OnTranslationReady, bool usePOST = true)
  66. {
  67. GoogleTranslation.AddTranslationJob(new TranslationJob_Main(requests, OnTranslationReady));
  68. }
  69. public static bool ForceTranslate(Dictionary<string, TranslationQuery> requests, bool usePOST = true)
  70. {
  71. TranslationJob_Main translationJob_Main = new TranslationJob_Main(requests, null);
  72. TranslationJob.eJobState state;
  73. do
  74. {
  75. state = translationJob_Main.GetState();
  76. }
  77. while (state == TranslationJob.eJobState.Running);
  78. return state != TranslationJob.eJobState.Failed;
  79. }
  80. public static List<string> ConvertTranslationRequest(Dictionary<string, TranslationQuery> requests, bool encodeGET)
  81. {
  82. List<string> list = new List<string>();
  83. StringBuilder stringBuilder = new StringBuilder();
  84. foreach (KeyValuePair<string, TranslationQuery> keyValuePair in requests)
  85. {
  86. TranslationQuery value = keyValuePair.Value;
  87. if (stringBuilder.Length > 0)
  88. {
  89. stringBuilder.Append("<I2Loc>");
  90. }
  91. stringBuilder.Append(GoogleLanguages.GetGoogleLanguageCode(value.LanguageCode));
  92. stringBuilder.Append(":");
  93. for (int i = 0; i < value.TargetLanguagesCode.Length; i++)
  94. {
  95. if (i != 0)
  96. {
  97. stringBuilder.Append(",");
  98. }
  99. stringBuilder.Append(GoogleLanguages.GetGoogleLanguageCode(value.TargetLanguagesCode[i]));
  100. }
  101. stringBuilder.Append("=");
  102. string text = (!(GoogleTranslation.TitleCase(value.Text) == value.Text)) ? value.Text : value.Text.ToLowerInvariant();
  103. if (!encodeGET)
  104. {
  105. stringBuilder.Append(text);
  106. }
  107. else
  108. {
  109. stringBuilder.Append(Uri.EscapeDataString(text));
  110. if (stringBuilder.Length > 4000)
  111. {
  112. list.Add(stringBuilder.ToString());
  113. stringBuilder.Length = 0;
  114. }
  115. }
  116. }
  117. list.Add(stringBuilder.ToString());
  118. return list;
  119. }
  120. private static void AddTranslationJob(TranslationJob job)
  121. {
  122. GoogleTranslation.mTranslationJobs.Add(job);
  123. if (GoogleTranslation.mTranslationJobs.Count == 1)
  124. {
  125. CoroutineManager.Start(GoogleTranslation.WaitForTranslations());
  126. }
  127. }
  128. private static IEnumerator WaitForTranslations()
  129. {
  130. while (GoogleTranslation.mTranslationJobs.Count > 0)
  131. {
  132. TranslationJob[] jobs = GoogleTranslation.mTranslationJobs.ToArray();
  133. foreach (TranslationJob translationJob in jobs)
  134. {
  135. if (translationJob.GetState() != TranslationJob.eJobState.Running)
  136. {
  137. GoogleTranslation.mTranslationJobs.Remove(translationJob);
  138. }
  139. }
  140. yield return null;
  141. }
  142. yield break;
  143. }
  144. public static string ParseTranslationResult(string html, Dictionary<string, TranslationQuery> requests)
  145. {
  146. if (!html.StartsWith("<!DOCTYPE html>") && !html.StartsWith("<HTML>"))
  147. {
  148. string[] array = html.Split(new string[]
  149. {
  150. "<I2Loc>"
  151. }, StringSplitOptions.None);
  152. string[] separator = new string[]
  153. {
  154. "<i2>"
  155. };
  156. int num = 0;
  157. string[] array2 = requests.Keys.ToArray<string>();
  158. foreach (string text in array2)
  159. {
  160. TranslationQuery value = GoogleTranslation.FindQueryFromOrigText(text, requests);
  161. string text2 = array[num++];
  162. if (value.Tags != null)
  163. {
  164. int j = 0;
  165. int num2 = value.Tags.Length;
  166. while (j < num2)
  167. {
  168. text2 = text2.Replace(((char)(9728 + j)).ToString(), value.Tags[j]);
  169. j++;
  170. }
  171. }
  172. value.Results = text2.Split(separator, StringSplitOptions.None);
  173. if (GoogleTranslation.TitleCase(text) == text)
  174. {
  175. for (int k = 0; k < value.Results.Length; k++)
  176. {
  177. value.Results[k] = GoogleTranslation.TitleCase(value.Results[k]);
  178. }
  179. }
  180. requests[value.OrigText] = value;
  181. }
  182. return null;
  183. }
  184. if (html.Contains("The script completed but did not return anything"))
  185. {
  186. return "The current Google WebService is not supported.\nPlease, delete the WebService from the Google Drive and Install the latest version.";
  187. }
  188. if (html.Contains("Service invoked too many times in a short time"))
  189. {
  190. return string.Empty;
  191. }
  192. return "There was a problem contacting the WebService. Please try again later\n" + html;
  193. }
  194. public static bool IsTranslating()
  195. {
  196. return GoogleTranslation.mCurrentTranslations.Count > 0 || GoogleTranslation.mTranslationJobs.Count > 0;
  197. }
  198. public static void CancelCurrentGoogleTranslations()
  199. {
  200. GoogleTranslation.mCurrentTranslations.Clear();
  201. foreach (TranslationJob translationJob in GoogleTranslation.mTranslationJobs)
  202. {
  203. translationJob.Dispose();
  204. }
  205. GoogleTranslation.mTranslationJobs.Clear();
  206. }
  207. public static void CreateQueries(string text, string LanguageCodeFrom, string LanguageCodeTo, Dictionary<string, TranslationQuery> dict)
  208. {
  209. if (!text.Contains("[i2s_"))
  210. {
  211. GoogleTranslation.CreateQueries_Plurals(text, LanguageCodeFrom, LanguageCodeTo, dict);
  212. return;
  213. }
  214. Dictionary<string, string> specializations = SpecializationManager.GetSpecializations(text, null);
  215. foreach (KeyValuePair<string, string> keyValuePair in specializations)
  216. {
  217. GoogleTranslation.CreateQueries_Plurals(keyValuePair.Value, LanguageCodeFrom, LanguageCodeTo, dict);
  218. }
  219. }
  220. private static void CreateQueries_Plurals(string text, string LanguageCodeFrom, string LanguageCodeTo, Dictionary<string, TranslationQuery> dict)
  221. {
  222. bool flag = text.Contains("{[#");
  223. bool flag2 = text.Contains("[i2p_");
  224. if (!GoogleTranslation.HasParameters(text) || (!flag && !flag2))
  225. {
  226. GoogleTranslation.AddQuery(text, LanguageCodeFrom, LanguageCodeTo, dict);
  227. return;
  228. }
  229. bool forceTag = flag;
  230. for (ePluralType ePluralType = ePluralType.Zero; ePluralType <= ePluralType.Plural; ePluralType++)
  231. {
  232. string pluralType = ePluralType.ToString();
  233. if (GoogleLanguages.LanguageHasPluralType(LanguageCodeTo, pluralType))
  234. {
  235. string text2 = GoogleTranslation.GetPluralText(text, pluralType);
  236. int pluralTestNumber = GoogleLanguages.GetPluralTestNumber(LanguageCodeTo, ePluralType);
  237. string pluralParameter = GoogleTranslation.GetPluralParameter(text2, forceTag);
  238. if (!string.IsNullOrEmpty(pluralParameter))
  239. {
  240. text2 = text2.Replace(pluralParameter, pluralTestNumber.ToString());
  241. }
  242. GoogleTranslation.AddQuery(text2, LanguageCodeFrom, LanguageCodeTo, dict);
  243. }
  244. }
  245. }
  246. public static void AddQuery(string text, string LanguageCodeFrom, string LanguageCodeTo, Dictionary<string, TranslationQuery> dict)
  247. {
  248. if (string.IsNullOrEmpty(text))
  249. {
  250. return;
  251. }
  252. if (!dict.ContainsKey(text))
  253. {
  254. TranslationQuery value = new TranslationQuery
  255. {
  256. OrigText = text,
  257. LanguageCode = LanguageCodeFrom,
  258. TargetLanguagesCode = new string[]
  259. {
  260. LanguageCodeTo
  261. }
  262. };
  263. value.Text = text;
  264. GoogleTranslation.ParseNonTranslatableElements(ref value);
  265. dict[text] = value;
  266. }
  267. else
  268. {
  269. TranslationQuery value2 = dict[text];
  270. if (Array.IndexOf<string>(value2.TargetLanguagesCode, LanguageCodeTo) < 0)
  271. {
  272. value2.TargetLanguagesCode = value2.TargetLanguagesCode.Concat(new string[]
  273. {
  274. LanguageCodeTo
  275. }).Distinct<string>().ToArray<string>();
  276. }
  277. dict[text] = value2;
  278. }
  279. }
  280. private static string GetTranslation(string text, string LanguageCodeTo, Dictionary<string, TranslationQuery> dict)
  281. {
  282. if (!dict.ContainsKey(text))
  283. {
  284. return null;
  285. }
  286. TranslationQuery translationQuery = dict[text];
  287. int num = Array.IndexOf<string>(translationQuery.TargetLanguagesCode, LanguageCodeTo);
  288. if (num < 0)
  289. {
  290. return string.Empty;
  291. }
  292. if (translationQuery.Results == null)
  293. {
  294. return string.Empty;
  295. }
  296. return translationQuery.Results[num];
  297. }
  298. private static TranslationQuery FindQueryFromOrigText(string origText, Dictionary<string, TranslationQuery> dict)
  299. {
  300. foreach (KeyValuePair<string, TranslationQuery> keyValuePair in dict)
  301. {
  302. if (keyValuePair.Value.OrigText == origText)
  303. {
  304. return keyValuePair.Value;
  305. }
  306. }
  307. return default(TranslationQuery);
  308. }
  309. public static bool HasParameters(string text)
  310. {
  311. int num = text.IndexOf("{[");
  312. return num >= 0 && text.IndexOf("]}", num) > 0;
  313. }
  314. public static string GetPluralParameter(string text, bool forceTag)
  315. {
  316. int num = text.IndexOf("{[#");
  317. if (num < 0)
  318. {
  319. if (forceTag)
  320. {
  321. return null;
  322. }
  323. num = text.IndexOf("{[");
  324. }
  325. if (num < 0)
  326. {
  327. return null;
  328. }
  329. int num2 = text.IndexOf("]}", num + 2);
  330. if (num2 < 0)
  331. {
  332. return null;
  333. }
  334. return text.Substring(num, num2 - num + 2);
  335. }
  336. public static string GetPluralText(string text, string pluralType)
  337. {
  338. pluralType = "[i2p_" + pluralType + "]";
  339. int num = text.IndexOf(pluralType);
  340. if (num >= 0)
  341. {
  342. num += pluralType.Length;
  343. int num2 = text.IndexOf("[i2p_", num);
  344. if (num2 < 0)
  345. {
  346. num2 = text.Length;
  347. }
  348. return text.Substring(num, num2 - num);
  349. }
  350. num = text.IndexOf("[i2p_");
  351. if (num < 0)
  352. {
  353. return text;
  354. }
  355. if (num > 0)
  356. {
  357. return text.Substring(0, num);
  358. }
  359. num = text.IndexOf("]");
  360. if (num < 0)
  361. {
  362. return text;
  363. }
  364. num++;
  365. int num3 = text.IndexOf("[i2p_", num);
  366. if (num3 < 0)
  367. {
  368. num3 = text.Length;
  369. }
  370. return text.Substring(num, num3 - num);
  371. }
  372. private static int FindClosingTag(string tag, MatchCollection matches, int startIndex)
  373. {
  374. int i = startIndex;
  375. int count = matches.Count;
  376. while (i < count)
  377. {
  378. string captureMatch = GoogleTranslation.GetCaptureMatch(matches[i]);
  379. if (captureMatch[0] == '/' && tag.StartsWith(captureMatch.Substring(1)))
  380. {
  381. return i;
  382. }
  383. i++;
  384. }
  385. return -1;
  386. }
  387. private static string GetCaptureMatch(Match match)
  388. {
  389. for (int i = match.Groups.Count - 1; i >= 0; i--)
  390. {
  391. if (match.Groups[i].Success)
  392. {
  393. return match.Groups[i].ToString();
  394. }
  395. }
  396. return match.ToString();
  397. }
  398. private static void ParseNonTranslatableElements(ref TranslationQuery query)
  399. {
  400. MatchCollection matchCollection = Regex.Matches(query.Text, "\\{\\[(.*?)]}|\\[(.*?)]|\\<(.*?)>");
  401. if (matchCollection == null || matchCollection.Count == 0)
  402. {
  403. return;
  404. }
  405. string text = query.Text;
  406. List<string> list = new List<string>();
  407. int i = 0;
  408. int count = matchCollection.Count;
  409. while (i < count)
  410. {
  411. string captureMatch = GoogleTranslation.GetCaptureMatch(matchCollection[i]);
  412. int num = GoogleTranslation.FindClosingTag(captureMatch, matchCollection, i);
  413. if (num < 0)
  414. {
  415. string text2 = matchCollection[i].ToString();
  416. if (text2.StartsWith("{[") && text2.EndsWith("]}"))
  417. {
  418. text = text.Replace(text2, ((char)(9728 + list.Count)).ToString());
  419. list.Add(text2);
  420. }
  421. }
  422. else if (captureMatch == "i2nt")
  423. {
  424. string text3 = query.Text.Substring(matchCollection[i].Index, matchCollection[num].Index - matchCollection[i].Index + matchCollection[num].Length);
  425. text = text.Replace(text3, ((char)(9728 + list.Count)).ToString());
  426. list.Add(text3);
  427. }
  428. else
  429. {
  430. string text4 = matchCollection[i].ToString();
  431. text = text.Replace(text4, ((char)(9728 + list.Count)).ToString());
  432. list.Add(text4);
  433. string text5 = matchCollection[num].ToString();
  434. text = text.Replace(text5, ((char)(9728 + list.Count)).ToString());
  435. list.Add(text5);
  436. }
  437. i++;
  438. }
  439. query.Text = text;
  440. query.Tags = list.ToArray();
  441. }
  442. public static string GetQueryResult(string text, string LanguageCodeTo, Dictionary<string, TranslationQuery> dict)
  443. {
  444. if (!dict.ContainsKey(text))
  445. {
  446. return null;
  447. }
  448. TranslationQuery translationQuery = dict[text];
  449. if (translationQuery.Results == null || translationQuery.Results.Length < 0)
  450. {
  451. return null;
  452. }
  453. if (string.IsNullOrEmpty(LanguageCodeTo))
  454. {
  455. return translationQuery.Results[0];
  456. }
  457. int num = Array.IndexOf<string>(translationQuery.TargetLanguagesCode, LanguageCodeTo);
  458. if (num < 0)
  459. {
  460. return null;
  461. }
  462. return translationQuery.Results[num];
  463. }
  464. public static string RebuildTranslation(string text, Dictionary<string, TranslationQuery> dict, string LanguageCodeTo)
  465. {
  466. if (!text.Contains("[i2s_"))
  467. {
  468. return GoogleTranslation.RebuildTranslation_Plural(text, dict, LanguageCodeTo);
  469. }
  470. Dictionary<string, string> specializations = SpecializationManager.GetSpecializations(text, null);
  471. Dictionary<string, string> dictionary = new Dictionary<string, string>();
  472. foreach (KeyValuePair<string, string> keyValuePair in specializations)
  473. {
  474. dictionary[keyValuePair.Key] = GoogleTranslation.RebuildTranslation_Plural(keyValuePair.Value, dict, LanguageCodeTo);
  475. }
  476. return SpecializationManager.SetSpecializedText(dictionary);
  477. }
  478. private static string RebuildTranslation_Plural(string text, Dictionary<string, TranslationQuery> dict, string LanguageCodeTo)
  479. {
  480. bool flag = text.Contains("{[#");
  481. bool flag2 = text.Contains("[i2p_");
  482. if (!GoogleTranslation.HasParameters(text) || (!flag && !flag2))
  483. {
  484. return GoogleTranslation.GetTranslation(text, LanguageCodeTo, dict);
  485. }
  486. StringBuilder stringBuilder = new StringBuilder();
  487. string b = null;
  488. bool forceTag = flag;
  489. for (ePluralType ePluralType = ePluralType.Plural; ePluralType >= ePluralType.Zero; ePluralType--)
  490. {
  491. string text2 = ePluralType.ToString();
  492. if (GoogleLanguages.LanguageHasPluralType(LanguageCodeTo, text2))
  493. {
  494. string text3 = GoogleTranslation.GetPluralText(text, text2);
  495. int pluralTestNumber = GoogleLanguages.GetPluralTestNumber(LanguageCodeTo, ePluralType);
  496. string pluralParameter = GoogleTranslation.GetPluralParameter(text3, forceTag);
  497. if (!string.IsNullOrEmpty(pluralParameter))
  498. {
  499. text3 = text3.Replace(pluralParameter, pluralTestNumber.ToString());
  500. }
  501. string text4 = GoogleTranslation.GetTranslation(text3, LanguageCodeTo, dict);
  502. if (!string.IsNullOrEmpty(pluralParameter))
  503. {
  504. text4 = text4.Replace(pluralTestNumber.ToString(), pluralParameter);
  505. }
  506. if (ePluralType == ePluralType.Plural)
  507. {
  508. b = text4;
  509. }
  510. else
  511. {
  512. if (text4 == b)
  513. {
  514. goto IL_117;
  515. }
  516. stringBuilder.AppendFormat("[i2p_{0}]", text2);
  517. }
  518. stringBuilder.Append(text4);
  519. }
  520. IL_117:;
  521. }
  522. return stringBuilder.ToString();
  523. }
  524. public static string UppercaseFirst(string s)
  525. {
  526. if (string.IsNullOrEmpty(s))
  527. {
  528. return string.Empty;
  529. }
  530. char[] array = s.ToLower().ToCharArray();
  531. array[0] = char.ToUpper(array[0]);
  532. return new string(array);
  533. }
  534. public static string TitleCase(string s)
  535. {
  536. if (string.IsNullOrEmpty(s))
  537. {
  538. return string.Empty;
  539. }
  540. return CultureInfo.CurrentCulture.TextInfo.ToTitleCase(s);
  541. }
  542. private static List<WWW> mCurrentTranslations = new List<WWW>();
  543. private static List<TranslationJob> mTranslationJobs = new List<TranslationJob>();
  544. }
  545. }