TranslationJob_POST.cs 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using UnityEngine;
  5. namespace I2.Loc
  6. {
  7. public class TranslationJob_POST : TranslationJob_WWW
  8. {
  9. public TranslationJob_POST(Dictionary<string, TranslationQuery> requests, Action<Dictionary<string, TranslationQuery>, string> OnTranslationReady)
  10. {
  11. this._requests = requests;
  12. this._OnTranslationReady = OnTranslationReady;
  13. List<string> list = GoogleTranslation.ConvertTranslationRequest(requests, false);
  14. WWWForm wwwform = new WWWForm();
  15. wwwform.AddField("action", "Translate");
  16. wwwform.AddField("list", list[0]);
  17. this.www = new WWW(LocalizationManager.GetWebServiceURL(null), wwwform);
  18. }
  19. public override TranslationJob.eJobState GetState()
  20. {
  21. if (this.www != null && this.www.isDone)
  22. {
  23. this.ProcessResult(this.www.bytes, this.www.error);
  24. this.www.Dispose();
  25. this.www = null;
  26. }
  27. return this.mJobState;
  28. }
  29. public void ProcessResult(byte[] bytes, string errorMsg)
  30. {
  31. if (!string.IsNullOrEmpty(errorMsg))
  32. {
  33. this.mJobState = TranslationJob.eJobState.Failed;
  34. }
  35. else
  36. {
  37. string @string = Encoding.UTF8.GetString(bytes, 0, bytes.Length);
  38. errorMsg = GoogleTranslation.ParseTranslationResult(@string, this._requests);
  39. if (this._OnTranslationReady != null)
  40. {
  41. this._OnTranslationReady(this._requests, errorMsg);
  42. }
  43. this.mJobState = TranslationJob.eJobState.Succeeded;
  44. }
  45. }
  46. private Dictionary<string, TranslationQuery> _requests;
  47. private Action<Dictionary<string, TranslationQuery>, string> _OnTranslationReady;
  48. }
  49. }