-
Notifications
You must be signed in to change notification settings - Fork 0
/
Translator.cs
220 lines (198 loc) · 9.48 KB
/
Translator.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
namespace I18nAutoTranslation
{
internal class Translator
{
#region Properties
/// <summary>
/// Gets the supported languages.
/// </summary>
public static IEnumerable<string> Languages
{
get
{
Translator.EnsureInitialized();
return Translator._languageModeMap.Keys.OrderBy(p => p);
}
}
/// <summary>
/// Gets the time taken to perform the translation.
/// </summary>
public TimeSpan TranslationTime
{
get;
private set;
}
/// <summary>
/// Gets the url used to speak the translation.
/// </summary>
/// <value>The url used to speak the translation.</value>
public string TranslationSpeechUrl
{
get;
private set;
}
/// <summary>
/// Gets the error.
/// </summary>
public Exception Error
{
get;
private set;
}
#endregion
#region Public methods
/// <summary>
/// Translates the specified source text.
/// </summary>
/// <param name="sourceText">The source text.</param>
/// <param name="sourceLanguage">The source language.</param>
/// <param name="targetLanguage">The target language.</param>
/// <returns>The translation.</returns>
public string Translate
(string sourceText,
string sourceLanguage,
string targetLanguage)
{
// Initialize
this.Error = null;
this.TranslationSpeechUrl = null;
this.TranslationTime = TimeSpan.Zero;
DateTime tmStart = DateTime.Now;
string translation = string.Empty;
try
{
// Download translation
string translationFromGoogle = "";
//Using secret translate.googleapis.com API that is internally used by the Google Translate extension for Chrome and requires no authentication
string url = string.Format("https://translate.googleapis.com/translate_a/single?client=gtx&sl={0}&tl={1}&dt=t&q={2}",
Translator.LanguageEnumToIdentifier(sourceLanguage),
Translator.LanguageEnumToIdentifier(targetLanguage),
HttpUtility.UrlEncode(sourceText));
using (WebClient wc = new WebClient())
{
wc.Headers.Add("user-agent", "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36");
wc.Encoding = System.Text.Encoding.UTF8;
translationFromGoogle = wc.DownloadString(url);
}
// Get translated text
var json = JsonConvert.DeserializeObject(translationFromGoogle);
translation = ((((json as JArray).First() as JArray).First() as JArray).First() as JValue).Value.ToString();
// And translation speech URL
//Using secret translate.googleapis.com API that is internally used by the Google Translate extension for Chrome and requires no authentication
this.TranslationSpeechUrl = string.Format("https://translate.googleapis.com/translate_tts?ie=UTF-8&q={0}&tl={1}&total=1&idx=0&textlen={2}&client=gtx",
HttpUtility.UrlEncode(translation), Translator.LanguageEnumToIdentifier(targetLanguage), translation.Length);
}
catch (Exception ex)
{
this.Error = ex;
}
// Return result
this.TranslationTime = DateTime.Now - tmStart;
return translation;
}
#endregion
#region Private methods
/// <summary>
/// Converts a language to its identifier.
/// </summary>
/// <param name="language">The language."</param>
/// <returns>The identifier or <see cref="string.Empty"/> if none.</returns>
private static string LanguageEnumToIdentifier
(string language)
{
string mode = string.Empty;
Translator.EnsureInitialized();
Translator._languageModeMap.TryGetValue(language, out mode);
return mode;
}
/// <summary>
/// Ensures the translator has been initialized.
/// </summary>
private static void EnsureInitialized()
{
if (Translator._languageModeMap == null)
{
Translator._languageModeMap = new Dictionary<string, string>();
Translator._languageModeMap.Add("Afrikaans", "af");
Translator._languageModeMap.Add("Albanian", "sq");
Translator._languageModeMap.Add("Arabic", "ar");
Translator._languageModeMap.Add("Armenian", "hy");
Translator._languageModeMap.Add("Azerbaijani", "az");
Translator._languageModeMap.Add("Basque", "eu");
Translator._languageModeMap.Add("Belarusian", "be");
Translator._languageModeMap.Add("Bengali", "bn");
Translator._languageModeMap.Add("Bulgarian", "bg");
Translator._languageModeMap.Add("Catalan", "ca");
Translator._languageModeMap.Add("Chinese", "zh-CN");
Translator._languageModeMap.Add("Croatian", "hr");
Translator._languageModeMap.Add("Czech", "cs");
Translator._languageModeMap.Add("Danish", "da");
Translator._languageModeMap.Add("Dutch", "nl");
Translator._languageModeMap.Add("English", "en");
Translator._languageModeMap.Add("Esperanto", "eo");
Translator._languageModeMap.Add("Estonian", "et");
Translator._languageModeMap.Add("Filipino", "tl");
Translator._languageModeMap.Add("Finnish", "fi");
Translator._languageModeMap.Add("French", "fr");
Translator._languageModeMap.Add("Galician", "gl");
Translator._languageModeMap.Add("German", "de");
Translator._languageModeMap.Add("Georgian", "ka");
Translator._languageModeMap.Add("Greek", "el");
Translator._languageModeMap.Add("Haitian Creole", "ht");
Translator._languageModeMap.Add("Hebrew", "iw");
Translator._languageModeMap.Add("Hindi", "hi");
Translator._languageModeMap.Add("Hungarian", "hu");
Translator._languageModeMap.Add("Icelandic", "is");
Translator._languageModeMap.Add("Indonesian", "id");
Translator._languageModeMap.Add("Irish", "ga");
Translator._languageModeMap.Add("Italian", "it");
Translator._languageModeMap.Add("Japanese", "ja");
Translator._languageModeMap.Add("Korean", "ko");
Translator._languageModeMap.Add("Lao", "lo");
Translator._languageModeMap.Add("Latin", "la");
Translator._languageModeMap.Add("Latvian", "lv");
Translator._languageModeMap.Add("Lithuanian", "lt");
Translator._languageModeMap.Add("Macedonian", "mk");
Translator._languageModeMap.Add("Malay", "ms");
Translator._languageModeMap.Add("Maltese", "mt");
Translator._languageModeMap.Add("Norwegian", "no");
Translator._languageModeMap.Add("Persian", "fa");
Translator._languageModeMap.Add("Polish", "pl");
Translator._languageModeMap.Add("Portuguese", "pt");
Translator._languageModeMap.Add("Romanian", "ro");
Translator._languageModeMap.Add("Russian", "ru");
Translator._languageModeMap.Add("Serbian", "sr");
Translator._languageModeMap.Add("Slovak", "sk");
Translator._languageModeMap.Add("Slovenian", "sl");
Translator._languageModeMap.Add("Spanish", "es");
Translator._languageModeMap.Add("Swahili", "sw");
Translator._languageModeMap.Add("Swedish", "sv");
Translator._languageModeMap.Add("Tamil", "ta");
Translator._languageModeMap.Add("Telugu", "te");
Translator._languageModeMap.Add("Thai", "th");
Translator._languageModeMap.Add("Turkish", "tr");
Translator._languageModeMap.Add("Ukrainian", "uk");
Translator._languageModeMap.Add("Urdu", "ur");
Translator._languageModeMap.Add("Vietnamese", "vi");
Translator._languageModeMap.Add("Welsh", "cy");
Translator._languageModeMap.Add("Yiddish", "yi");
}
}
#endregion
#region Fields
/// <summary>
/// The language to translation mode map.
/// </summary>
private static Dictionary<string, string> _languageModeMap;
#endregion
}
}