Use Google Translate C # version.

Source: Internet
Author: User

Work needs, the translation of a program into a multi-lingual version, very sad one of the languages is Turkish. The translation of the domestic open API is not supported. Microsoft is in trouble. Find a C # version on the web, fix the change, make it work. Pro test is effective.

Development tools: VS2013

Need to reference multiple cells

using system.web; using System.Net; using System.Text.RegularExpressions; using System.IO; using Msscriptcontrol;

Multiple reference packages are required.

Reference--Add reference to the-->com--> type library, note that the program environment must be X86 after referencing the library. Change the project properties to affect the small   JS parsing package
Reference--Add reference--Select recent project-->newtonsoft.json.dll     Json Parsing library. The DLL can be downloaded online

To save the JS file on the net, I write the global variable directly

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); > c g[d++] = C: (2048 > c? g[d++] = C >> 6 | 192: (55296 = = (C & 64 && F + 1 < a.length && 56320 = (a.charcodeat (f + 1) & 64512)? (c = 65536 + ((C & 1023) <<) + (A.charcodeat (++f) & 1023), g[d++] = c >> | | 240,g[d++] = c &GT;&G T & 63 | ): g[d++] = c >> 12 | 224, g[d++] = C >> 6 & 63 | ), g[d++] = C & 63 |  }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" > Pending Translated text </param>///<param Name= "fromlanguage" > Automatic detection:auto</param>//<param name= "Tola Nguage "> Chinese: ZH-CN, English:en</param>///<returns> translated text </returns> public string Googletra Nslate (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 () + ")";//In the returned HTML is matched Tkk js code var TKK = Executescript (Tkkstr, TKKSTR);//execute TKK code, get 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
Results Example//resulttext = ' [[' I Love you ', ' I Loved You ', Null,null,1],[null,null,null, ' Wǒài nǐ ']],nu ll, "ZH-CN", null,null,[["I Love You", null,[["I Loved You", 1000,true,false]],//[[0,3]], "I Love You", 0,0]],1,n ull,[["ZH-CN"],null,[1],["ZH-CN"]],null,null,null,null,null,[["I", "Love", "You"] []] ' string resulttext = Convert.tostr ING (tempresult[0][0][0]); The first element of the array json, the first array inside 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, your own Chrome browser F12 view tracking changes to the following two lines, the same execution success. 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 "; using (var WebResponse = (HttpWebResponse) webrequest.getresponse ()) {using (var reader = new St Reamreader (WebResponse.GetResponseStream (), Encoding.UTF8)) {html = reader. ReadToEnd (); Reader. Close (); WebResponse.Close (); }} return HTML; }//<summary>//Execute JS//</summary>//<param name= "Sexpre Ssion "> Parameter body </param>//<param name=" SCode ">javascript Code for Strings </param>///<returns> </returns> private String Executescript (String SexpressioN, String sCode) {Msscriptcontrol.scriptcontrol ScriptControl = new MSSCRIPTCONTROL.SCRIPTC Ontrol (); 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, subsequent preparations are extended to Delphi. The required knowledge is as follows:

Idhttp Get:http Protocol,

Superobject:json Library,

Tperlregex: Regular Expressions

Comobje:js

Httpapp:httpencode, kanji transcoding, send translation requests when used.

Use Google Translate C # version.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.