PDA

View Full Version : Google translation via Delphi code :)



paul_nicholls
28-01-2009, 04:07 AM
Hi all,
After googling today I found the below link:

http://delphiptt.blogspot.com/2006/06/using-google-translate-from-delphi.html

It allows you to make a request to the google translation engine to translate some text for you and return the translated version as a string using Delphi :)

cheers,
Paul

NecroDOME
28-01-2009, 02:42 PM
Cool, now you can translate your game/story on the fly :P \o/
(however it's still computer translation, so its still doesn't feel 'human')

Joshas
28-01-2009, 06:44 PM
Could be fun to use in MMORPG.

paul_nicholls
30-01-2009, 01:32 AM
If anyone wants to use it, I have made a unit for doing the translation.

It requires the Synapse internet classes (http://www.ararat.cz/synapse/doku.php).

I had to change the search strings slightly because the translator was finding the original text instead of the translated version and returning that!!



Unit translator_unit;
{$IFDEF FPC}
{$MODE DELPHI} {$H+}
{$ENDIF}
Interface

Const
{................................................. .............................}

// cLanguagePair_ = 0;
cLanguagePair_EnglishToFrench = 2;
cLanguagePair_EnglishToGerman = 3;
cLanguagePair_EnglishToItalian = 4;
// cLanguagePair_ = 5;
// cLanguagePair_ = 6;
// cLanguagePair_ = 7;
// cLanguagePair_ = 8;
// cLanguagePair_ = 9;
// cLanguagePair_ = 10;
// cLanguagePair_ = 11;
// cLanguagePair_ = 12;
cLanguagePair_ItalianToEnglish = 13;
// cLanguagePair_ = 14;
// cLanguagePair_ = 15;
// cLanguagePair_ = 16;
// cLanguagePair_ = 17;

cLanguagePairList : Array[0..17] Of AnsiString =(
'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'
);
{................................................. .............................}

{................................................. .............................}
Function TranslateText(Const ATextIn : AnsiString;
Var ATextOut : AnsiString;
Const ALanguagePair : Integer) : Boolean;

Implementation

Uses
Classes,
httpsend,
SysUtils;

{................................................. .............................}

{................................................. .............................}
Function HTTPEncode(Const AStr : AnsiString) : AnsiString;
Const
cNoConversion = ['A'..'Z', 'a'..'z', '*', '@', '.', '_', '-'];
Var
Sp, Rp : PChar;
Begin
SetLength(Result, Length(AStr) * 3);
Sp := PChar(AStr);
Rp := PChar(Result);
While Sp^ <> #0 Do
Begin
If Sp^ in cNoConversion Then
Rp^ &#58;= Sp^
Else
If Sp^ = ' ' Then
Rp^ &#58;= '+'
Else
Begin
FormatBuf&#40;Rp^, 3, '%%%.2x', 6, &#91;Ord&#40;Sp^&#41;&#93;&#41;;
Inc&#40;Rp, 2&#41;;
End;
Inc&#40;Rp&#41;;
Inc&#40;Sp&#41;;
End;
SetLength&#40;Result, Rp - PChar&#40;Result&#41;&#41;;
End;
&#123;................................................. .............................&#125;

&#123;................................................. .............................&#125;
Function PosEx&#40;Const SubStr, S &#58; AnsiString; Offset &#58; Cardinal&#41; &#58; Integer;

Var
i &#58; PChar;
Begin
If &#40;offset <1> length&#40;s&#41;&#41; Then
Begin
Result &#58;= 0;
Exit
End;

i &#58;= StrPos&#40;@s&#91;offset&#93;,@substr&#91;1&#93;&#41;;
If i = Nil Then
PosEx &#58;= 0
Else
PosEx &#58;= Succ&#40;i-PChar&#40;s&#41;&#41;;
End;
&#123;................................................. .............................&#125;

&#123;................................................. .............................&#125;
Function TranslateText&#40;Const ATextIn &#58; AnsiString;
Var ATextOut &#58; AnsiString;
Const ALanguagePair &#58; Integer&#41; &#58; Boolean;
Var
SendTxt &#58; AnsiString;
Response &#58; TStringList;
s &#58; AnsiString;
a,b &#58; integer;
Begin
Result &#58;= False;
Response &#58;= TStringList.Create;
Try
SendTxt &#58;= 'http&#58;//translate.google.com/translate_t?text=' +
HTTPEncode&#40;ATextIn&#41; +
'&langpair=' +
cLanguagePairList&#91;ALanguagePair&#93;;

If Not HttpGetText&#40;SendTxt,Response&#41; Then Exit;
s &#58;= Response.Text;
a &#58;= PosEx&#40;'>',s,Pos&#40;'id=suggestion>',s&#41;&#41;;
b &#58;= PosEx&#40;'</textarea>',s,a&#41;;
ATextOut &#58;= Copy&#40;s,a+1,b-a-1&#41;;
Result &#58;= True;
Finally
Response.Free;
End;
End;
&#123;................................................. .............................&#125;

&#123;................................................. .............................&#125;
End.

It most likely won't work on languages that have UniCode characters that are
not ascii 8 bit in them due to it using normal strings, but it works ok for
the others :)

I have also included some constants for the language from-to pairs (a few of them, not all)

enjoy :)
cheers,
Paul