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^ := Sp^
Else
If Sp^ = ' ' Then
Rp^ := '+'
Else
Begin
FormatBuf(Rp^, 3, '%%%.2x', 6, [Ord(Sp^)]);
Inc(Rp, 2);
End;
Inc(Rp);
Inc(Sp);
End;
SetLength(Result, Rp - PChar(Result));
End;
{................................................. .............................}
{................................................. .............................}
Function PosEx(Const SubStr, S : AnsiString; Offset : Cardinal) : Integer;
Var
i : PChar;
Begin
If (offset <1> length(s)) Then
Begin
Result := 0;
Exit
End;
i := StrPos(@s[offset],@substr[1]);
If i = Nil Then
PosEx := 0
Else
PosEx := Succ(i-PChar(s));
End;
{................................................. .............................}
{................................................. .............................}
Function TranslateText(Const ATextIn : AnsiString;
Var ATextOut : AnsiString;
Const ALanguagePair : Integer) : Boolean;
Var
SendTxt : AnsiString;
Response : TStringList;
s : AnsiString;
a,b : integer;
Begin
Result := False;
Response := TStringList.Create;
Try
SendTxt := 'http://translate.google.com/translate_t?text=' +
HTTPEncode(ATextIn) +
'&langpair=' +
cLanguagePairList[ALanguagePair];
If Not HttpGetText(SendTxt,Response) Then Exit;
s := Response.Text;
a := PosEx('>',s,Pos('id=suggestion>',s));
b := PosEx('</textarea>',s,a);
ATextOut := Copy(s,a+1,b-a-1);
Result := True;
Finally
Response.Free;
End;
End;
{................................................. .............................}
{................................................. .............................}
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
Powered by vBulletin® Version 4.2.5 Copyright © 2024 vBulletin Solutions Inc. All rights reserved.