RealTimeTranslation.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. using System;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. namespace I2.Loc
  5. {
  6. public class RealTimeTranslation : MonoBehaviour
  7. {
  8. public void OnGUI()
  9. {
  10. GUILayout.Label("Translate:", new GUILayoutOption[0]);
  11. this.OriginalText = GUILayout.TextArea(this.OriginalText, new GUILayoutOption[]
  12. {
  13. GUILayout.Width((float)Screen.width)
  14. });
  15. GUILayout.Space(10f);
  16. GUILayout.BeginHorizontal(new GUILayoutOption[0]);
  17. if (GUILayout.Button("English -> Español", new GUILayoutOption[]
  18. {
  19. GUILayout.Height(100f)
  20. }))
  21. {
  22. this.StartTranslating("en", "es");
  23. }
  24. if (GUILayout.Button("Español -> English", new GUILayoutOption[]
  25. {
  26. GUILayout.Height(100f)
  27. }))
  28. {
  29. this.StartTranslating("es", "en");
  30. }
  31. GUILayout.EndHorizontal();
  32. GUILayout.Space(10f);
  33. GUILayout.BeginHorizontal(new GUILayoutOption[0]);
  34. GUILayout.TextArea("Multiple Translation with 1 call:\n'This is an example' -> en,zh\n'Hola' -> en", new GUILayoutOption[0]);
  35. if (GUILayout.Button("Multi Translate", new GUILayoutOption[]
  36. {
  37. GUILayout.ExpandHeight(true)
  38. }))
  39. {
  40. this.ExampleMultiTranslations_Async();
  41. }
  42. GUILayout.EndHorizontal();
  43. GUILayout.TextArea(this.TranslatedText, new GUILayoutOption[]
  44. {
  45. GUILayout.Width((float)Screen.width)
  46. });
  47. GUILayout.Space(10f);
  48. if (this.IsTranslating)
  49. {
  50. GUILayout.Label("Contacting Google....", new GUILayoutOption[0]);
  51. }
  52. }
  53. public void StartTranslating(string fromCode, string toCode)
  54. {
  55. this.IsTranslating = true;
  56. GoogleTranslation.Translate(this.OriginalText, fromCode, toCode, new Action<string, string>(this.OnTranslationReady));
  57. }
  58. private void OnTranslationReady(string Translation, string errorMsg)
  59. {
  60. this.IsTranslating = false;
  61. if (errorMsg != null)
  62. {
  63. Debug.LogError(errorMsg);
  64. }
  65. else
  66. {
  67. this.TranslatedText = Translation;
  68. }
  69. }
  70. public void ExampleMultiTranslations_Blocking()
  71. {
  72. Dictionary<string, TranslationQuery> dictionary = new Dictionary<string, TranslationQuery>();
  73. GoogleTranslation.AddQuery("This is an example", "en", "es", dictionary);
  74. GoogleTranslation.AddQuery("This is an example", "auto", "zh", dictionary);
  75. GoogleTranslation.AddQuery("Hola", "es", "en", dictionary);
  76. if (!GoogleTranslation.ForceTranslate(dictionary, true))
  77. {
  78. return;
  79. }
  80. Debug.Log(GoogleTranslation.GetQueryResult("This is an example", "en", dictionary));
  81. Debug.Log(GoogleTranslation.GetQueryResult("This is an example", "zh", dictionary));
  82. Debug.Log(GoogleTranslation.GetQueryResult("This is an example", string.Empty, dictionary));
  83. Debug.Log(dictionary["Hola"].Results[0]);
  84. }
  85. public void ExampleMultiTranslations_Async()
  86. {
  87. this.IsTranslating = true;
  88. Dictionary<string, TranslationQuery> dictionary = new Dictionary<string, TranslationQuery>();
  89. GoogleTranslation.AddQuery("This is an example", "en", "es", dictionary);
  90. GoogleTranslation.AddQuery("This is an example", "auto", "zh", dictionary);
  91. GoogleTranslation.AddQuery("Hola", "es", "en", dictionary);
  92. GoogleTranslation.Translate(dictionary, new Action<Dictionary<string, TranslationQuery>, string>(this.OnMultitranslationReady), true);
  93. }
  94. private void OnMultitranslationReady(Dictionary<string, TranslationQuery> dict, string errorMsg)
  95. {
  96. if (!string.IsNullOrEmpty(errorMsg))
  97. {
  98. Debug.LogError(errorMsg);
  99. return;
  100. }
  101. this.IsTranslating = false;
  102. this.TranslatedText = string.Empty;
  103. this.TranslatedText = this.TranslatedText + GoogleTranslation.GetQueryResult("This is an example", "es", dict) + "\n";
  104. this.TranslatedText = this.TranslatedText + GoogleTranslation.GetQueryResult("This is an example", "zh", dict) + "\n";
  105. this.TranslatedText = this.TranslatedText + GoogleTranslation.GetQueryResult("This is an example", string.Empty, dict) + "\n";
  106. this.TranslatedText += dict["Hola"].Results[0];
  107. }
  108. public bool IsWaitingForTranslation()
  109. {
  110. return this.IsTranslating;
  111. }
  112. public string GetTranslatedText()
  113. {
  114. return this.TranslatedText;
  115. }
  116. public void SetOriginalText(string text)
  117. {
  118. this.OriginalText = text;
  119. }
  120. private 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.";
  121. private string TranslatedText = string.Empty;
  122. private bool IsTranslating;
  123. }
  124. }