-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathTranslatorGoogle.cs
50 lines (42 loc) · 1.52 KB
/
TranslatorGoogle.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
namespace VPet.Plugin.AutoMTL
{
class TranslatorGoogle : TranslatorBase
{
public static new string providerName = "Google Translate";
public static new string providerId = "google";
public static new Dictionary<string, string> providedLanguages => JsonConvert.DeserializeObject<Dictionary<string, string>>(Constants.googleLangJSON);
public TranslatorGoogle(string srcLang, string dstLang, string cacheBase) : base(srcLang,dstLang,cacheBase)
{
}
// https://stackoverflow.com/questions/50963296/c-sharp-google-translate-without-api-and-with-unicode
// https://github.com/dmytrovoytko/SublimeText-Translate/blob/master/Translator.py
public override string TranslateString(string input)
{
try {
string url = String.Format("https://translate.googleapis.com/translate_a/single?client=gtx&sl={0}&tl={1}&dt=t&q={2}", srcLang, dstLang, input);
WebClient webClient = new WebClient();
webClient.Encoding = System.Text.Encoding.UTF8;
string result = webClient.DownloadString(url);
JArray json = (JArray)JsonConvert.DeserializeObject(result);
string output = "";
foreach (JArray item in json[0])
output += item[0].Value<string>();
if (output == "")
return null;
return output;
} catch (Exception exc) {
string error = exc.ToString();
//MessageBox.Show(error);
return null;
}
}
}
}