Use Google Translate C# version.

The work needs to translate a program into a multi-language version. One of the languages ​​that is very sad is Turkish. The translation of the domestic open API is not supported. Microsoft's is really troublesome. Find a c# version on the Internet, modify it, and make do with it. Pro-test is effective.

Development tools: VS2013

Need to reference multiple units

using System.Web;
using System.Net;
using System.Text.RegularExpressions;
using System.IO;
using MSScriptControl;

Multiple reference packages are required.

 

//Reference-->Add reference-->com-->Type library, note that the program environment must be X86 after referencing the library. Just change the project properties, it will not affect the js parsing package
//Reference-->Add Reference-->Select Recent Project-->Newtonsoft.Json.dll json parsing library. The DLL can be downloaded online
 

To save js files online, I directly write them as global variables

string GetTkkJS = @"
var b = function (a, b)
{
for (var d = 0; d < b.length - 2; d += 3)
{
var c = b.charAt (d + 2),
c = ""a"" <= c ? c.charCodeAt(0) - 87 : Number(c),
c = ""+"" == b.charAt(d + 1) ? a >>> c : a << c;
a = ""+"" == b.charAt(d) ? a + c & 4294967295 : a ^ c
}
return a
}


var tk = function (a,TKK) {
for (var e = TKK.split("".""), h = Number(e[0]) || 0, g = [], d = 0, f = 0; f < a.length; f++)
{
var c = a.charCodeAt(f);
128 > c ? g[d++] = c : (2048 > c ? g[d++] = c >> 6 | 192 : (55296 == (c & 64512) && f + 1 < a.length
&& 56320 == (a.charCodeAt(f + 1) & 64512) ? (c = 65536 + ((c & 1023) << 10) + (a.charCodeAt(++f) & 1023), g[d++] = c >> 18 | 240,
g[d++] = c >> 12 & 63 | 128) : g[d++] = c >> 12 | 224, g[d++] = c >> 6 & 63 | 128), g[d++] = c & 63 | 128)
}
a = h;
for (d = 0; d < g.length; d++) a += g[d], a = b(a, ""+-a^+6"");
a = b(a, ""+-3^+b+-f"");
a ^= Number(e[1]) || 0;
0 > a && (a = (a & 2147483647) + 2147483648);
a %= 1E6;
return a.toString() + ""."" + (a ^ h)
}";

  The core execution code is as follows:

 

        
/// <summary> /// Google Translate /// </summary> /// <param name="text">text to be translated</param> /// <param name="fromLanguage">Automatic detection: auto</param> /// <param name="toLanguage">Chinese: zh-CN, English: en</param> /// <returns>Translated text</returns> public string GoogleTranslate(string text, string fromLanguage, string toLanguage) { CookieContainer cc = new CookieContainer(); string GoogleTransBaseUrl = "https://translate.google.cn/"; var BaseResultHtml = GetResultHtml(GoogleTransBaseUrl, cc, ""); Regex re = new Regex(@"(?<=TKK=)(.*?)(?=\);)"); var TKKStr = re.Match(BaseResultHtml).ToString() + ")";//Regularly match the JS code of TKK in the returned HTML var TKK = ExecuteScript(TKKStr, TKKStr);//Execute the TKK code to get the TKK value var tk = ExecuteScript("tk(\"" + text + "\",\"" + TKK + "\")", GetTkkJS); string googleTransUrl = "https://translate.google.cn/translate_a/single?client=t&sl=" + fromLanguage + "&tl=" + toLanguage + "&hl=en&dt=at&dt=bd&dt=ex&dt=ld&dt=md&dt=qca&dt=rw&dt=rm&dt=ss&dt=t&ie=UTF-8&oe=UTF-8&otf=1&ssel=0&tsel=0&kc=1&tk=" + tk + "&q=" + HttpUtility.UrlEncode(text);
var ResultHtml = GetResultHtml(googleTransUrl, cc, ""); dynamic TempResult = Newtonsoft.Json.JsonConvert.DeserializeObject(ResultHtml); //Convert to json
//Result example
//ResultText = '[[["I love you","I love you",null,null,1], [null,null,null,"Wǒ ài nǐ"]],null,"zh-CN",null,null,[["I love you",null,[["I love you",1000,true,false ]],
// [[0,3]],"I love you",0,0]],1,null,[["zh-CN"],null,[1],["zh-CN" ]],null,null,null,null,null,[["I","Love","You"]]]' string ResultText = Convert.ToString(TempResult[0][0][0]); //Array json, the first content of the first array in the first array. Nested data, return ResultText; } public string GetResultHtml(string url, CookieContainer cookie, string referer) { var html = ""; var webRequest = WebRequest.Create(url) as HttpWebRequest; webRequest.Method = "GET";
/*Online program code, use chrome browser F12 to view the trace and modify it to the following two lines, the same execution is successful. 20180427 //webRequest.CookieContainer = cookie; //webRequest.Referer = referer; //webRequest.Timeout = 20000; //webRequest.Headers.Add("X-Requested-With:XMLHttpRequest"); //webRequest.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8";
*/
webRequest.Accept = "*/*"; webRequest.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36";//useragent;
using (var webResponse = (HttpWebResponse)webRequest.GetResponse()) { using (var reader = new StreamReader(webResponse.GetResponseStream(), Encoding.UTF8)) { html = reader.ReadToEnd(); reader.Close(); webResponse.Close(); } } return html; } /// <summary> /// Execute JS /// </summary> /// <param name="sExpression">Parameter body</param> /// <param name="sCode">String of JavaScript code</param> /// <returns></returns> private string ExecuteScript(string sExpression, string sCode) { MSScriptControl.ScriptControl scriptControl = new MSScriptControl.ScriptControl(); scriptControl.UseSafeSubset = true; scriptControl.Language = "JScript"; scriptControl.AddCode(sCode); try { string str = scriptControl.Eval(sExpression).ToString(); return str; } catch (Exception ex) { string str = ex.Message; } return null; }

  

With this example, the subsequent preparation is extended to Delphi. The required knowledge is as follows:

idhttp get :http协议,

superobject: json library, 

TPerlRegEx: Regular Expression

comObje:js

HTTPApp: httpEncode, Chinese character transcoding, used when sending translation requests.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324952180&siteId=291194637