This repository has been archived by the owner on May 12, 2022. It is now read-only.
/
GoogleTranslationModule.cs
148 lines (125 loc) · 3.79 KB
/
GoogleTranslationModule.cs
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SimpleJSON;
using UnityEngine;
namespace CM3D2.AutoTranslate.Plugin
{
internal class GoogleTranslationModule : TranslationModule
{
public override string Section => "Google";
private string _targetLanguage = "en";
public override bool Init()
{
// Do Nothing
StartCoroutine(Test());
return true;
}
private IEnumerator Test()
{
var dat = new TranslationData()
{
Id = 0,
ProcessedText = "Hallo Welt"
};
var cd = CreateCoroutineEx(TranslateGoogle(dat.ProcessedText, "de", "en", dat));
yield return cd.coroutine;
try
{
cd.Check();
if (dat.State == TranslationState.Finished)
{
Logger.Log("Google seems OK", Level.Debug);
}
else
{
Logger.Log("There seems to be a problem with Google!", Level.Warn);
}
}
catch (Exception e)
{
Logger.Log("There seems to be a problem with Google!", Level.Warn);
Logger.Log(e);
}
}
protected override void LoadConfig(CoreUtil.SectionLoader section)
{
section.LoadValue("TargetLanguage", ref _targetLanguage);
}
public override IEnumerator Translate(TranslationData data)
{
var cd = CreateCoroutineEx(TranslateGoogle(data.ProcessedText, "ja", _targetLanguage, data));
yield return cd.coroutine;
if (cd.GetException() != null)
{
Logger.LogException(cd.GetException(), Level.Warn);
data.State = TranslationState.Failed;
}
}
public override void DeInit()
{
// Do Nothing
}
private static string ExtractTranslationFromGoogleString(string input)
{
var data = JSON.Parse(input);
var lineBuilder = new StringBuilder(input.Length);
foreach (JSONNode entry in data.AsArray[0].AsArray)
{
var token = entry.AsArray[0].ToString();
if (lineBuilder.Length != 0) lineBuilder.Append(" ");
lineBuilder.Append(token.Substring(1, token.Length - 2));
}
var text = lineBuilder.ToString();
var builder = new StringBuilder(text.Length);
for (var i = 0; i < text.Length; ++i)
{
if (text[i] == '\\')
{
// skip this and next token
if (text[i + 1] != '"')
i++;
continue;
}
if (text[i] == '"' && text[i - 1] != '\\')
break;
builder.Append(text[i]);
}
return builder.ToString();
}
private IEnumerator TranslateGoogle(string text, string fromCulture, string toCulture, TranslationData translation)
{
fromCulture = fromCulture.ToLower();
toCulture = toCulture.ToLower();
translation.ProcessedText = text;
translation.State = TranslationState.Failed;
// normalize the culture in case something like en-us was passed
// retrieve only en since Google doesn't support sub-locales
string[] tokens = fromCulture.Split('-');
if (tokens.Length > 1)
fromCulture = tokens[0];
// normalize ToCulture
tokens = toCulture.Split('-');
if (tokens.Length > 1)
toCulture = tokens[0];
//string url =
// $@"http://translate.google.com/translate_a/t?client=j&text={WWW.EscapeURL(text)}&hl=en&sl={fromCulture}&tl={toCulture}";
string url = $@"https://translate.googleapis.com/translate_a/single?client=gtx&sl={fromCulture}&tl={toCulture}&dt=t&q={WWW.EscapeURL(text)}";
var headers = new Dictionary<string, string> { { "User-Agent", "Mozilla/5.0" }, { "Accept-Charset", "UTF-8" } };
var www = new WWW(url, null, headers);
yield return www;
if (www.error != null)
{
Logger.LogError(www.error);
yield break;
}
var result = ExtractTranslationFromGoogleString(www.text);
result = result.Replace("\\n", "");
Logger.Log($"Got Translation from google: {result}", Level.Debug);
translation.Translation = result;
translation.State = TranslationState.Finished;
}
}
}