Address format for calling Google translation:
Http://ajax.googleapis.com/ajax/services/language/translate? V = 1.0 & Q = "+ original text +" & langpair = "+ original language +" % 7C "+ target language
The returned data format is as follows, which can be obtained simply using responsedata. translatedtext:
{"Responsedata": {"translatedtext": "Returned text"}, "responsedetails": NULL, "responsestatus": 200}
In this example:
Code File:
Unit unit1; interfaceuses windows, messages, sysutils, variants, classes, graphics, controls, forms, dialogs, stdctrls; Type tform1 = Class (tform) memo1: tmemo; memo2: tmemo; button1: tbutton; button2: tbutton; Procedure button1click (Sender: tobject); Procedure button2click (Sender: tobject); end; var form1: tform1; implementation {$ R *. DFM} uses MSXML, superobject; // string-to-utf8 encoded function, used for Google address Function Toutf8encode (STR: string): string; var B: byte; begin for B in bytesof (utf8encode (STR) do result: = format ('% S %. 2x ', [result,' % ', B]); end; // translation function translate (STR, requestlanguage, resultlanguage: string): string; const baseurl = 'HTTP: // ajax.googleapis.com/ajax/services/language/translate? V = 1.0 & Q = '; var URL: string; JO: isuperobject; Req: ixmlhttprequest; begin URL: = baseurl + toutf8encode (STR) + '& langpair =' + requestlanguage + '% 7c' + resultlanguage; Req: = coxmlhttp. create; req. open ('get', URL, false, emptyparam, emptyparam); req. send (emptyparam); JO: = So (req. responsetext); Result: = Jo. format ('% responsedata. translatedtext % '); end; // procedure tform1.button1click (Sender: tobject); begin memo2.text: = translate (memo1.text, 'en', 'zh-cn'); end; // procedure tform1.button2click (Sender: tobject); begin memo1.text: = translate (memo2.text, 'zh-cn', 'en'); end.
Form file:
Object form1: tform1 left = 0 Top = 0 caption = 'form1 'clientheight = 139 clientwidth = 459 color = clbtnface font. charset = default_charset font. color = clwindowtext font. height =-11 font. name = 'tahoma 'font. style = [] oldcreateorder = false pixelsperinch = 96 textheight = 13 object memo1: tmemo left = 0 Top = 0 width = 185 Height = 139 align = alleft lines. strings = ('memo1') taborder = 0 explicitheight = 202 end object button1: tbutton left = 191 Top = 24 width = 75 Height = 25 caption = #33521 #25991 '->' #20013 #25991 taborder = 1 onclick = button1click end object memo2: tmemo left = 274 Top = 0 width = 185 Height = 139 align = alright lines. strings = ('memo2') taborder = 2 explicitleft = 312 explicittop =-8 explicitheight = 202 end object button2: tbutton left = 191 Top = 72 width = 75 Height = 25 caption = #33521 #25991 '