TranslationJob_WEB.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Globalization;
  4. using System.Text;
  5. using System.Text.RegularExpressions;
  6. using UnityEngine;
  7. namespace I2.Loc
  8. {
  9. public class TranslationJob_WEB : TranslationJob_WWW
  10. {
  11. public TranslationJob_WEB(Dictionary<string, TranslationQuery> requests, Action<Dictionary<string, TranslationQuery>, string> OnTranslationReady)
  12. {
  13. this._requests = requests;
  14. this._OnTranslationReady = OnTranslationReady;
  15. this.FindAllQueries();
  16. this.ExecuteNextBatch();
  17. }
  18. private void FindAllQueries()
  19. {
  20. this.mQueries = new List<KeyValuePair<string, string>>();
  21. foreach (KeyValuePair<string, TranslationQuery> keyValuePair in this._requests)
  22. {
  23. foreach (string str in keyValuePair.Value.TargetLanguagesCode)
  24. {
  25. this.mQueries.Add(new KeyValuePair<string, string>(keyValuePair.Value.OrigText, keyValuePair.Value.LanguageCode + ":" + str));
  26. }
  27. }
  28. this.mQueries.Sort((KeyValuePair<string, string> a, KeyValuePair<string, string> b) => a.Value.CompareTo(b.Value));
  29. }
  30. private void ExecuteNextBatch()
  31. {
  32. if (this.mQueries.Count == 0)
  33. {
  34. this.mJobState = TranslationJob.eJobState.Succeeded;
  35. return;
  36. }
  37. this.mCurrentBatch_Text = new List<string>();
  38. string text = null;
  39. int num = 200;
  40. StringBuilder stringBuilder = new StringBuilder();
  41. int i;
  42. for (i = 0; i < this.mQueries.Count; i++)
  43. {
  44. string key = this.mQueries[i].Key;
  45. string value = this.mQueries[i].Value;
  46. if (text == null || value == text)
  47. {
  48. if (i != 0)
  49. {
  50. stringBuilder.Append("|||");
  51. }
  52. stringBuilder.Append(key);
  53. this.mCurrentBatch_Text.Add(key);
  54. text = value;
  55. }
  56. if (stringBuilder.Length > num)
  57. {
  58. break;
  59. }
  60. }
  61. this.mQueries.RemoveRange(0, i);
  62. string[] array = text.Split(new char[]
  63. {
  64. ':'
  65. });
  66. this.mCurrentBatch_FromLanguageCode = array[0];
  67. this.mCurrentBatch_ToLanguageCode = array[1];
  68. string text2 = string.Format("http://www.google.com/translate_t?hl=en&vi=c&ie=UTF8&oe=UTF8&submit=Translate&langpair={0}|{1}&text={2}", this.mCurrentBatch_FromLanguageCode, this.mCurrentBatch_ToLanguageCode, Uri.EscapeUriString(stringBuilder.ToString()));
  69. Debug.Log(text2);
  70. this.www = new WWW(text2);
  71. }
  72. public override TranslationJob.eJobState GetState()
  73. {
  74. if (this.www != null && this.www.isDone)
  75. {
  76. this.ProcessResult(this.www.bytes, this.www.error);
  77. this.www.Dispose();
  78. this.www = null;
  79. }
  80. if (this.www == null)
  81. {
  82. this.ExecuteNextBatch();
  83. }
  84. return this.mJobState;
  85. }
  86. public void ProcessResult(byte[] bytes, string errorMsg)
  87. {
  88. if (string.IsNullOrEmpty(errorMsg))
  89. {
  90. string @string = Encoding.UTF8.GetString(bytes, 0, bytes.Length);
  91. string message = this.ParseTranslationResult(@string, "aab");
  92. Debug.Log(message);
  93. if (string.IsNullOrEmpty(errorMsg))
  94. {
  95. if (this._OnTranslationReady != null)
  96. {
  97. this._OnTranslationReady(this._requests, null);
  98. }
  99. return;
  100. }
  101. }
  102. this.mJobState = TranslationJob.eJobState.Failed;
  103. this.mErrorMessage = errorMsg;
  104. }
  105. private string ParseTranslationResult(string html, string OriginalText)
  106. {
  107. string result;
  108. try
  109. {
  110. int num = html.IndexOf("TRANSLATED_TEXT='") + "TRANSLATED_TEXT='".Length;
  111. int num2 = html.IndexOf("';var", num);
  112. string text = html.Substring(num, num2 - num);
  113. text = Regex.Replace(text, "\\\\x([a-fA-F0-9]{2})", (Match match) => char.ConvertFromUtf32(int.Parse(match.Groups[1].Value, NumberStyles.HexNumber)));
  114. text = Regex.Replace(text, "&#(\\d+);", (Match match) => char.ConvertFromUtf32(int.Parse(match.Groups[1].Value)));
  115. text = text.Replace("<br>", "\n");
  116. if (OriginalText.ToUpper() == OriginalText)
  117. {
  118. text = text.ToUpper();
  119. }
  120. else if (GoogleTranslation.UppercaseFirst(OriginalText) == OriginalText)
  121. {
  122. text = GoogleTranslation.UppercaseFirst(text);
  123. }
  124. else if (GoogleTranslation.TitleCase(OriginalText) == OriginalText)
  125. {
  126. text = GoogleTranslation.TitleCase(text);
  127. }
  128. result = text;
  129. }
  130. catch (Exception ex)
  131. {
  132. Debug.LogError(ex.Message);
  133. result = string.Empty;
  134. }
  135. return result;
  136. }
  137. private Dictionary<string, TranslationQuery> _requests;
  138. private Action<Dictionary<string, TranslationQuery>, string> _OnTranslationReady;
  139. public string mErrorMessage;
  140. private string mCurrentBatch_ToLanguageCode;
  141. private string mCurrentBatch_FromLanguageCode;
  142. private List<string> mCurrentBatch_Text;
  143. private List<KeyValuePair<string, string>> mQueries;
  144. }
  145. }