GoogleTranslation_Post.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. using UnityEngine;
  2. using System;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using System.Text;
  6. using System.Linq;
  7. using UnityEngine.Networking;
  8. namespace I2.Loc
  9. {
  10. using TranslationDictionary = Dictionary<string, TranslationQuery>;
  11. public static partial class GoogleTranslation
  12. {
  13. static List<UnityWebRequest> mCurrentTranslations = new List<UnityWebRequest>();
  14. static List<TranslationJob> mTranslationJobs = new List<TranslationJob>();
  15. public delegate void fnOnTranslationReady(TranslationDictionary dict, string error);
  16. #region Multiple Translations
  17. public static void Translate( TranslationDictionary requests, fnOnTranslationReady OnTranslationReady, bool usePOST = true )
  18. {
  19. //WWW www = GetTranslationWWW( requests, usePOST );
  20. //I2.Loc.CoroutineManager.Start(WaitForTranslation(www, OnTranslationReady, requests));
  21. AddTranslationJob( new TranslationJob_Main(requests, OnTranslationReady) );
  22. }
  23. public static bool ForceTranslate(TranslationDictionary requests, bool usePOST = true)
  24. {
  25. var job = new TranslationJob_Main(requests, null);
  26. while (true)
  27. {
  28. var state = job.GetState();
  29. if (state == TranslationJob.eJobState.Running)
  30. continue;
  31. if (state == TranslationJob.eJobState.Failed)
  32. return false;
  33. //TranslationJob.eJobState.Succeeded
  34. return true;
  35. }
  36. }
  37. public static List<string> ConvertTranslationRequest(TranslationDictionary requests, bool encodeGET)
  38. {
  39. List<string> results = new List<string>();
  40. var sb = new StringBuilder();
  41. foreach (var kvp in requests)
  42. {
  43. var request = kvp.Value;
  44. if (sb.Length > 0)
  45. sb.Append("<I2Loc>");
  46. sb.Append(GoogleLanguages.GetGoogleLanguageCode(request.LanguageCode));
  47. sb.Append(":");
  48. for (int i = 0; i < request.TargetLanguagesCode.Length; ++i)
  49. {
  50. if (i != 0) sb.Append(",");
  51. sb.Append(GoogleLanguages.GetGoogleLanguageCode(request.TargetLanguagesCode[i]));
  52. }
  53. sb.Append("=");
  54. var text = (TitleCase(request.Text) == request.Text) ? request.Text.ToLowerInvariant() : request.Text;
  55. if (!encodeGET)
  56. {
  57. sb.Append(text);
  58. }
  59. else
  60. {
  61. sb.Append(Uri.EscapeDataString(text));
  62. if (sb.Length > 4000)
  63. {
  64. results.Add(sb.ToString());
  65. sb.Length = 0;
  66. }
  67. }
  68. }
  69. results.Add(sb.ToString());
  70. return results;
  71. }
  72. static void AddTranslationJob( TranslationJob job )
  73. {
  74. mTranslationJobs.Add(job);
  75. if (mTranslationJobs.Count==1)
  76. {
  77. I2.Loc.CoroutineManager.Start(WaitForTranslations());
  78. }
  79. }
  80. static IEnumerator WaitForTranslations()
  81. {
  82. while (mTranslationJobs.Count > 0)
  83. {
  84. var jobs = mTranslationJobs.ToArray();
  85. foreach (var job in jobs)
  86. {
  87. if (job.GetState() != TranslationJob.eJobState.Running)
  88. mTranslationJobs.Remove(job);
  89. }
  90. yield return null;
  91. }
  92. }
  93. public static string ParseTranslationResult( string html, TranslationDictionary requests )
  94. {
  95. //Debug.Log(html);
  96. // Handle google restricting the webservice to run
  97. if (html.StartsWith("<!DOCTYPE html>") || html.StartsWith("<HTML>"))
  98. {
  99. if (html.Contains("The script completed but did not return anything"))
  100. return "The current Google WebService is not supported.\nPlease, delete the WebService from the Google Drive and Install the latest version.";
  101. else
  102. if (html.Contains("Service invoked too many times in a short time"))
  103. return ""; // ignore and try again
  104. else
  105. return "There was a problem contacting the WebService. Please try again later\n" + html;
  106. }
  107. string[] texts = html.Split (new string[]{"<I2Loc>"}, StringSplitOptions.None);
  108. string[] splitter = new string[]{"<i2>"};
  109. int i = 0;
  110. var Keys = requests.Keys.ToArray();
  111. foreach (var text in Keys)
  112. {
  113. var temp = FindQueryFromOrigText(text, requests);
  114. var fullText = texts[i++];
  115. if (temp.Tags != null)
  116. {
  117. //for (int j = 0, jmax = temp.Tags.Length; j < jmax; ++j)
  118. for (int j = temp.Tags.Length-1; j>=0; --j)
  119. {
  120. fullText = fullText.Replace(GetGoogleNoTranslateTag(j), temp.Tags[j]);
  121. //fullText = fullText.Replace( /*"{[" + j + "]}"*/ ((char)(0x2600+j)).ToString(), temp.Tags[j]);
  122. }
  123. }
  124. temp.Results = fullText.Split (splitter, StringSplitOptions.None);
  125. // Google has problem translating this "This Is An Example" but not this "this is an example"
  126. if (TitleCase(text)==text)
  127. {
  128. for (int j=0; j<temp.Results.Length; ++j)
  129. temp.Results[j] = TitleCase(temp.Results[j]);
  130. }
  131. requests[temp.OrigText] = temp;
  132. }
  133. return null;
  134. }
  135. public static bool IsTranslating()
  136. {
  137. return mCurrentTranslations.Count>0 || mTranslationJobs.Count > 0;
  138. }
  139. public static void CancelCurrentGoogleTranslations()
  140. {
  141. mCurrentTranslations.Clear ();
  142. foreach (var job in mTranslationJobs)
  143. {
  144. job.Dispose();
  145. }
  146. mTranslationJobs.Clear();
  147. }
  148. #endregion
  149. }
  150. }