summaryrefslogtreecommitdiff
path: root/Thronefall_1_57/Thronefall/I2.Loc/RealTimeTranslation.cs
blob: d0bee68723d4d11955663898deabefbb05649af1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
using System.Collections.Generic;
using UnityEngine;

namespace I2.Loc;

public class RealTimeTranslation : MonoBehaviour
{
	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;

	public void OnGUI()
	{
		GUILayout.Label("Translate:");
		OriginalText = GUILayout.TextArea(OriginalText, GUILayout.Width(Screen.width));
		GUILayout.Space(10f);
		GUILayout.BeginHorizontal();
		if (GUILayout.Button("English -> Español", GUILayout.Height(100f)))
		{
			StartTranslating("en", "es");
		}
		if (GUILayout.Button("Español -> English", GUILayout.Height(100f)))
		{
			StartTranslating("es", "en");
		}
		GUILayout.EndHorizontal();
		GUILayout.Space(10f);
		GUILayout.BeginHorizontal();
		GUILayout.TextArea("Multiple Translation with 1 call:\n'This is an example' -> en,zh\n'Hola' -> en");
		if (GUILayout.Button("Multi Translate", GUILayout.ExpandHeight(expand: true)))
		{
			ExampleMultiTranslations_Async();
		}
		GUILayout.EndHorizontal();
		GUILayout.TextArea(TranslatedText, GUILayout.Width(Screen.width));
		GUILayout.Space(10f);
		if (IsTranslating)
		{
			GUILayout.Label("Contacting Google....");
		}
	}

	public void StartTranslating(string fromCode, string toCode)
	{
		IsTranslating = true;
		GoogleTranslation.Translate(OriginalText, fromCode, toCode, OnTranslationReady);
	}

	private void OnTranslationReady(string Translation, string errorMsg)
	{
		IsTranslating = false;
		if (errorMsg != null)
		{
			Debug.LogError(errorMsg);
		}
		else
		{
			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))
		{
			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", "", dictionary));
			Debug.Log(dictionary["Hola"].Results[0]);
		}
	}

	public void ExampleMultiTranslations_Async()
	{
		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, OnMultitranslationReady);
	}

	private void OnMultitranslationReady(Dictionary<string, TranslationQuery> dict, string errorMsg)
	{
		if (!string.IsNullOrEmpty(errorMsg))
		{
			Debug.LogError(errorMsg);
			return;
		}
		IsTranslating = false;
		TranslatedText = "";
		TranslatedText = TranslatedText + GoogleTranslation.GetQueryResult("This is an example", "es", dict) + "\n";
		TranslatedText = TranslatedText + GoogleTranslation.GetQueryResult("This is an example", "zh", dict) + "\n";
		TranslatedText = TranslatedText + GoogleTranslation.GetQueryResult("This is an example", "", dict) + "\n";
		TranslatedText += dict["Hola"].Results[0];
	}

	public bool IsWaitingForTranslation()
	{
		return IsTranslating;
	}

	public string GetTranslatedText()
	{
		return TranslatedText;
	}

	public void SetOriginalText(string text)
	{
		OriginalText = text;
	}
}