TranslationJob_GET.cs 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using UnityEngine;
  5. namespace I2.Loc
  6. {
  7. public class TranslationJob_GET : TranslationJob_WWW
  8. {
  9. public TranslationJob_GET(Dictionary<string, TranslationQuery> requests, Action<Dictionary<string, TranslationQuery>, string> OnTranslationReady)
  10. {
  11. this._requests = requests;
  12. this._OnTranslationReady = OnTranslationReady;
  13. this.mQueries = GoogleTranslation.ConvertTranslationRequest(requests, true);
  14. this.GetState();
  15. }
  16. private void ExecuteNextQuery()
  17. {
  18. if (this.mQueries.Count == 0)
  19. {
  20. this.mJobState = TranslationJob.eJobState.Succeeded;
  21. return;
  22. }
  23. int index = this.mQueries.Count - 1;
  24. string arg = this.mQueries[index];
  25. this.mQueries.RemoveAt(index);
  26. string url = string.Format("{0}?action=Translate&list={1}", LocalizationManager.GetWebServiceURL(null), arg);
  27. this.www = new WWW(url);
  28. }
  29. public override TranslationJob.eJobState GetState()
  30. {
  31. if (this.www != null && this.www.isDone)
  32. {
  33. this.ProcessResult(this.www.bytes, this.www.error);
  34. this.www.Dispose();
  35. this.www = null;
  36. }
  37. if (this.www == null)
  38. {
  39. this.ExecuteNextQuery();
  40. }
  41. return this.mJobState;
  42. }
  43. public void ProcessResult(byte[] bytes, string errorMsg)
  44. {
  45. if (string.IsNullOrEmpty(errorMsg))
  46. {
  47. string @string = Encoding.UTF8.GetString(bytes, 0, bytes.Length);
  48. errorMsg = GoogleTranslation.ParseTranslationResult(@string, this._requests);
  49. if (string.IsNullOrEmpty(errorMsg))
  50. {
  51. if (this._OnTranslationReady != null)
  52. {
  53. this._OnTranslationReady(this._requests, null);
  54. }
  55. return;
  56. }
  57. }
  58. this.mJobState = TranslationJob.eJobState.Failed;
  59. this.mErrorMessage = errorMsg;
  60. }
  61. private Dictionary<string, TranslationQuery> _requests;
  62. private Action<Dictionary<string, TranslationQuery>, string> _OnTranslationReady;
  63. private List<string> mQueries;
  64. public string mErrorMessage;
  65. }
  66. }