Delphi: Google Translate ile cümle çevirisi - Delphi / Kylix Webmaster bilgi bankası, knowledge base Webmaster Araçları

AnaSayfa > Delphi / Kylix > Delphi: Google Translate ile cümle çevirisi
Kategori: Delphi / Kylix
Gönderen: Admin
Tarih: 2010-01-20
Puan: 5   Puan:5 | Katılımcı:28 | Katılımcı : 28
Okunma: 8348
  
 
1 puan 2 puan 3 puan 4 puan 5 puan 6 puan 7 puan 8 puan 9 puan 10 puan


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.



Yorumlar
Yazan: merthasan (22 Şubat 2010 Pazartesi)+30   
süper bir site harika elinize saglik
Yorum Ekleme Aparatı
Yorum Eklemek için lütfen sol menuden giris yapınız..
Toplam 1 yorum listelendi.