GoogleTranslation_Post.cs 5.8 KB

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