using System; using System.Collections.Generic; using UnityEngine; namespace I2.Loc { public class RealTimeTranslation : MonoBehaviour { public void OnGUI() { GUILayout.Label("Translate:", new GUILayoutOption[0]); this.OriginalText = GUILayout.TextArea(this.OriginalText, new GUILayoutOption[] { GUILayout.Width((float)Screen.width) }); GUILayout.Space(10f); GUILayout.BeginHorizontal(new GUILayoutOption[0]); if (GUILayout.Button("English -> Español", new GUILayoutOption[] { GUILayout.Height(100f) })) { this.StartTranslating("en", "es"); } if (GUILayout.Button("Español -> English", new GUILayoutOption[] { GUILayout.Height(100f) })) { this.StartTranslating("es", "en"); } GUILayout.EndHorizontal(); GUILayout.Space(10f); GUILayout.BeginHorizontal(new GUILayoutOption[0]); GUILayout.TextArea("Multiple Translation with 1 call:\n'This is an example' -> en,zh\n'Hola' -> en", new GUILayoutOption[0]); if (GUILayout.Button("Multi Translate", new GUILayoutOption[] { GUILayout.ExpandHeight(true) })) { this.ExampleMultiTranslations_Async(); } GUILayout.EndHorizontal(); GUILayout.TextArea(this.TranslatedText, new GUILayoutOption[] { GUILayout.Width((float)Screen.width) }); GUILayout.Space(10f); if (this.IsTranslating) { GUILayout.Label("Contacting Google....", new GUILayoutOption[0]); } } public void StartTranslating(string fromCode, string toCode) { this.IsTranslating = true; GoogleTranslation.Translate(this.OriginalText, fromCode, toCode, new Action(this.OnTranslationReady)); } private void OnTranslationReady(string Translation, string errorMsg) { this.IsTranslating = false; if (errorMsg != null) { Debug.LogError(errorMsg); } else { this.TranslatedText = Translation; } } public void ExampleMultiTranslations_Blocking() { Dictionary dictionary = new Dictionary(); GoogleTranslation.AddQuery("This is an example", "en", "es", dictionary); GoogleTranslation.AddQuery("This is an example", "auto", "zh", dictionary); GoogleTranslation.AddQuery("Hola", "es", "en", dictionary); if (!GoogleTranslation.ForceTranslate(dictionary, true)) { return; } Debug.Log(GoogleTranslation.GetQueryResult("This is an example", "en", dictionary)); Debug.Log(GoogleTranslation.GetQueryResult("This is an example", "zh", dictionary)); Debug.Log(GoogleTranslation.GetQueryResult("This is an example", string.Empty, dictionary)); Debug.Log(dictionary["Hola"].Results[0]); } public void ExampleMultiTranslations_Async() { this.IsTranslating = true; Dictionary dictionary = new Dictionary(); GoogleTranslation.AddQuery("This is an example", "en", "es", dictionary); GoogleTranslation.AddQuery("This is an example", "auto", "zh", dictionary); GoogleTranslation.AddQuery("Hola", "es", "en", dictionary); GoogleTranslation.Translate(dictionary, new Action, string>(this.OnMultitranslationReady), true); } private void OnMultitranslationReady(Dictionary dict, string errorMsg) { if (!string.IsNullOrEmpty(errorMsg)) { Debug.LogError(errorMsg); return; } this.IsTranslating = false; this.TranslatedText = string.Empty; this.TranslatedText = this.TranslatedText + GoogleTranslation.GetQueryResult("This is an example", "es", dict) + "\n"; this.TranslatedText = this.TranslatedText + GoogleTranslation.GetQueryResult("This is an example", "zh", dict) + "\n"; this.TranslatedText = this.TranslatedText + GoogleTranslation.GetQueryResult("This is an example", string.Empty, dict) + "\n"; this.TranslatedText += dict["Hola"].Results[0]; } public bool IsWaitingForTranslation() { return this.IsTranslating; } public string GetTranslatedText() { return this.TranslatedText; } public void SetOriginalText(string text) { this.OriginalText = text; } 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."; private string TranslatedText = string.Empty; private bool IsTranslating; } }