Call the Google translate address format:
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 easily obtained with Responsedata.translatedtext:
{" ResponseData": {"Translatedtext": "Returned Text"}, "responsedetails": null, "ResponseStatus": 200}
This example:
Code files:
UnitUnit1;InterfaceusesWindows, Messages, sysutils, variants, Classes, Graphics, Controls, Forms, Dialogs, Stdctrls;typeTForm1 =class(Tform) Memo1:tmemo; Memo2:tmemo; Button1:tbutton; Button2:tbutton;procedureButton1Click (Sender:tobject);procedureButton2click (Sender:tobject);End;varForm1:tform1;Implementation{$R *.DFM}usesMsXML, Superobject;//String to UTF8 encoded function for Google addressfunctionToutf8encode (str:string):string;varB:byte;begin forBinchBytesof (Utf8encode (str)) DoResult: = Format ('%s%s%.2x ', [Result,'% ', b]);End;//Translation functionfunctionTranslate (str, requestlanguage, Resultlanguage:string):string;ConstBASEURL =' http://ajax.googleapis.com/ajax/services/language/translate?v=1.0&q= ';varUrl:string; Jo:isuperobject; Req:ixmlhttprequest;beginURL: = 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;//English to Chinese translationprocedureTform1.button1click (Sender:tobject);beginMemo2.text: = Translate (Memo1.text,' en ',' ZH-CN ');End;//EnglishprocedureTform1.button2click (Sender:tobject);beginMemo1.text: = Translate (Memo2.text,' ZH-CN ',' en ');End;End.
Form file:
ObjectForm1:tform1 left =0Top =0Caption =' Form1 'ClientHeight =139ClientWidth =459Color = Clbtnface Font.charset = Default_charset Font.Color = Clwindowtext Font.height =- OneFont.Name =' Tahoma 'Font.style = [] Oldcreateorder = False PixelsPerInch = theTextHeight = -ObjectMemo1:tmemo left =0Top =0Width =185Height =139Align = Alleft Lines.strings = (' Memo1 ') TabOrder =0Explicitheight =202End ObjectButton1:tbutton left =191Top = -Width = theHeight = -Caption =#33521#25991' and '#20013#25991TabOrder =1OnClick = Button1ClickEnd ObjectMemo2:tmemo left =274Top =0Width =185Height =139Align = Alright lines.strings = (' Memo2 ') TabOrder =2Explicitleft =312Explicittop =-8Explicitheight =202 EndObjectButton2:tbutton left =191Top = theWidth = theHeight = -Caption =#33521#25991' <-'#20013#25991TabOrder =3OnClick = Button2clickEndEnd
JSON Superobject (17): example-using Google for full-text translation