GoogleTranslation.cs 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. using UnityEngine;
  2. using System;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using System.Text;
  6. using System.Text.RegularExpressions;
  7. using System.Linq;
  8. namespace I2.Loc
  9. {
  10. using TranslationDictionary = Dictionary<string, TranslationQuery>;
  11. public static partial class GoogleTranslation
  12. {
  13. public delegate void fnOnTranslated(string Translation, string Error);
  14. public static bool CanTranslate ()
  15. {
  16. return (LocalizationManager.Sources.Count > 0 &&
  17. !string.IsNullOrEmpty (LocalizationManager.GetWebServiceURL()));
  18. }
  19. // LanguageCodeFrom can be "auto"
  20. // After the translation is returned from Google, it will call OnTranslationReady(TranslationResult, ErrorMsg)
  21. // TranslationResult will be null if translation failed
  22. public static void Translate( string text, string LanguageCodeFrom, string LanguageCodeTo, fnOnTranslated OnTranslationReady )
  23. {
  24. LocalizationManager.InitializeIfNeeded();
  25. if (!GoogleTranslation.CanTranslate())
  26. {
  27. OnTranslationReady(null, "WebService is not set correctly or needs to be reinstalled");
  28. return;
  29. }
  30. //LanguageCodeTo = GoogleLanguages.GetGoogleLanguageCode(LanguageCodeTo);
  31. if (LanguageCodeTo==LanguageCodeFrom)
  32. {
  33. OnTranslationReady(text, null);
  34. return;
  35. }
  36. TranslationDictionary queries = new TranslationDictionary();
  37. // Unsupported language
  38. if (string.IsNullOrEmpty(LanguageCodeTo))
  39. {
  40. OnTranslationReady(string.Empty, null);
  41. return;
  42. }
  43. CreateQueries(text, LanguageCodeFrom, LanguageCodeTo, queries); // can split plurals and specializations into several queries
  44. Translate(queries, (results,error)=>
  45. {
  46. if (!string.IsNullOrEmpty(error) || results.Count==0)
  47. {
  48. OnTranslationReady(null, error);
  49. return;
  50. }
  51. string result = RebuildTranslation( text, queries, LanguageCodeTo); // gets the result from google and rebuilds the text from multiple queries if its is plurals
  52. OnTranslationReady( result, null );
  53. });
  54. }
  55. // Query google for the translation and waits until google returns
  56. // On some Unity versions (e.g. 2017.1f1) unity doesn't handle well waiting for WWW in the main thread, so this call can fail
  57. // In those cases, its advisable to use the Async version (GoogleTranslation.Translate(....))
  58. public static string ForceTranslate ( string text, string LanguageCodeFrom, string LanguageCodeTo )
  59. {
  60. TranslationDictionary dict = new TranslationDictionary();
  61. AddQuery(text, LanguageCodeFrom, LanguageCodeTo, dict);
  62. var job = new TranslationJob_Main(dict, null);
  63. while (true)
  64. {
  65. var state = job.GetState();
  66. if (state == TranslationJob.eJobState.Running)
  67. continue;
  68. if (state == TranslationJob.eJobState.Failed)
  69. return null;
  70. //TranslationJob.eJobState.Succeeded
  71. return GetQueryResult( text, "", dict);
  72. }
  73. }
  74. }
  75. }