GoogleTranslation.cs 3.1 KB

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