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;
// www.wmaraclari.com
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;
// www.wmaraclari.com
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
// www.wmaraclari.com
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;
{..............................................................................}
// www.wmaraclari.com
{..............................................................................}
End.
|