RealTimeTranslation.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. using UnityEngine;
  2. using System.Collections.Generic;
  3. namespace I2.Loc
  4. {
  5. public class RealTimeTranslation : MonoBehaviour
  6. {
  7. string OriginalText = "This is an example showing how to use the google translator to translate chat messages within the game.\nIt also supports multiline translations.",
  8. TranslatedText = string.Empty;
  9. bool IsTranslating = false;
  10. public void OnGUI()
  11. {
  12. GUILayout.Label("Translate:");
  13. OriginalText = GUILayout.TextArea(OriginalText, GUILayout.Width(Screen.width));
  14. GUILayout.Space(10);
  15. GUILayout.BeginHorizontal();
  16. if (GUILayout.Button("English -> Español", GUILayout.Height(100))) StartTranslating("en", "es");
  17. if (GUILayout.Button("Español -> English", GUILayout.Height(100))) StartTranslating("es", "en");
  18. GUILayout.EndHorizontal();
  19. GUILayout.Space(10);
  20. GUILayout.BeginHorizontal();
  21. GUILayout.TextArea("Multiple Translation with 1 call:\n'This is an example' -> en,zh\n'Hola' -> en");
  22. if (GUILayout.Button("Multi Translate", GUILayout.ExpandHeight(true))) ExampleMultiTranslations_Async();
  23. GUILayout.EndHorizontal();
  24. GUILayout.TextArea(TranslatedText, GUILayout.Width(Screen.width));
  25. GUILayout.Space(10);
  26. if (IsTranslating)
  27. {
  28. GUILayout.Label("Contacting Google....");
  29. }
  30. }
  31. public void StartTranslating(string fromCode, string toCode)
  32. {
  33. IsTranslating = true;
  34. // fromCode could be "auto" to autodetect the language
  35. GoogleTranslation.Translate(OriginalText, fromCode, toCode, OnTranslationReady);
  36. // can also use the ForceTranslate version: (it will block the main thread until the translation is returned)
  37. //var translation = GoogleTranslation.ForceTranslate(OriginalText, fromCode, toCode);
  38. //Debug.Log(translation);
  39. }
  40. void OnTranslationReady(string Translation, string errorMsg)
  41. {
  42. IsTranslating = false;
  43. if (errorMsg != null)
  44. Debug.LogError(errorMsg);
  45. else
  46. TranslatedText = Translation;
  47. }
  48. public void ExampleMultiTranslations_Blocking()
  49. {
  50. // This shows how to ask for many translations
  51. var dict = new System.Collections.Generic.Dictionary<string, TranslationQuery>();
  52. GoogleTranslation.AddQuery("This is an example", "en", "es", dict);
  53. GoogleTranslation.AddQuery("This is an example", "auto", "zh", dict);
  54. GoogleTranslation.AddQuery("Hola", "es", "en", dict);
  55. if (!GoogleTranslation.ForceTranslate(dict))
  56. return;
  57. Debug.Log(GoogleTranslation.GetQueryResult("This is an example", "en", dict));
  58. Debug.Log(GoogleTranslation.GetQueryResult("This is an example", "zh", dict));
  59. Debug.Log(GoogleTranslation.GetQueryResult("This is an example", "", dict)); // This returns ANY translation of that text (in this case, the first one 'en')
  60. Debug.Log(dict["Hola"].Results[0]); // example of getting the translation directly from the Results
  61. }
  62. public void ExampleMultiTranslations_Async()
  63. {
  64. IsTranslating = true;
  65. // This shows how to ask for many translations
  66. var dict = new Dictionary<string, TranslationQuery>();
  67. GoogleTranslation.AddQuery("This is an example", "en", "es", dict);
  68. GoogleTranslation.AddQuery("This is an example", "auto", "zh", dict);
  69. GoogleTranslation.AddQuery("Hola", "es", "en", dict);
  70. GoogleTranslation.Translate(dict, OnMultitranslationReady);
  71. }
  72. void OnMultitranslationReady(Dictionary<string, TranslationQuery> dict, string errorMsg)
  73. {
  74. if (!string.IsNullOrEmpty(errorMsg))
  75. {
  76. Debug.LogError(errorMsg);
  77. return;
  78. }
  79. IsTranslating = false;
  80. TranslatedText = "";
  81. TranslatedText += GoogleTranslation.GetQueryResult("This is an example", "es", dict) + "\n";
  82. TranslatedText += GoogleTranslation.GetQueryResult("This is an example", "zh", dict) + "\n";
  83. TranslatedText += GoogleTranslation.GetQueryResult("This is an example", "", dict) + "\n"; // This returns ANY translation of that text (in this case, the first one 'en')
  84. TranslatedText += dict["Hola"].Results[0]; // example of getting the translation directly from the Results
  85. }
  86. #region This methods are used in the publisher's Unity Tests
  87. public bool IsWaitingForTranslation()
  88. {
  89. return IsTranslating;
  90. }
  91. public string GetTranslatedText()
  92. {
  93. return TranslatedText;
  94. }
  95. public void SetOriginalText( string text )
  96. {
  97. OriginalText = text;
  98. }
  99. #endregion
  100. }
  101. }