Saturday, June 24, 2006

Using Google Translate from Delphi

This is how to translate a text unsing Google Translate from Delphi:
For sending data to Google will use http GET method.


function translate(ss,lng:string):string;
var s:widestring;
a,b:integer;
http:tidhttp;
begin
http:=tidhttp.Create;
s:=http.Get('http://translate.google.com/translate_t?text='
+httpencode(ss)+'&langpair='+lng);
a:=posex('›',s,pos('‹textarea',s));
b:=posex('‹/textarea›',s,a);
result:=copy(s,a+1,b-a-1);
http.Free;
end;


where ss is the text to be translated and lng is the string that tell
Google from and to what language to translate.
Here is some definiton of lng:


lng:array[0..17]of string=('zh-CN%7Cen',
'en%7Czh-CN',
'en%7Cfr',
'en%7Cde',
'en%7Cit',
'en%7Cja',
'en%7Cko',
'en%7Cpt',
'en%7Ces',
'fr%7Cen',
'fr%7Cde',
'de%7Cen',
'de%7Cfr',
'it%7Cen',
'ja%7Cen',
'ko%7Cen',
'pt%7Cen',
'es%7Cen');

3 comments:

Anonymous said...

Hello. Send please all code in celovec@inbox.lv.

Many thanks.

Anonymous said...

What sort of code sample is this? - no explanation on how to use the code or even how it works

asafadd said...

Great code, needs very little explanation, but, alas, no longer works.

I will try to fix it, but I feel that it is not a good idea to make a software that depends on a hack that may not work tomorrow.