123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136 |
- 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<string, string>(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<string, TranslationQuery> dictionary = new Dictionary<string, TranslationQuery>();
- 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<string, TranslationQuery> dictionary = new Dictionary<string, TranslationQuery>();
- 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<Dictionary<string, TranslationQuery>, string>(this.OnMultitranslationReady), true);
- }
- private void OnMultitranslationReady(Dictionary<string, TranslationQuery> 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;
- }
- }
|