unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, MMSystem, IdHTTP, StrUtils, IdBaseComponent,
IdComponent, IdTCPConnection, IdTCPClient;
type
TForm1 = class(TForm)
mSource: TMemo;
mDest: TMemo;
btnTranslate: TButton;
ComboBox1: TComboBox;
ComboBox2: TComboBox;
Label1: TLabel;
CheckBox1: TCheckBox;
procedure mDestKeyPress(Sender: TObject; var Key: Char);
procedure btnTranslateClick(Sender: TObject);
private
{ Private declarations }
function Translate_Google(
text, myLang, toLang, FileName: string; Play: boolean): string;
public
{ Public declarations }
end;
var Form1: TForm1;
type
TConvertChars = array[#128..#255] of Char;
const Koi_WinChars: TConvertChars = (
#128, #129, #130, #131, #132, #133, #134, #135,
#136, #137, #138, #139, #140, #141, #142, #143,
#144, #145, #146, #147, #148, #149, #150, #151,
#152, #153, #218, #155, #176, #157, #183, #159,
#160, #161, #162, #184, #186, #165, #166, #191,
#168, #169, #170, #171, #172, #173, #174, #175,
#156, #177, #178, #168, #170, #181, #182, #175,
#184, #185, #186, #187, #188, #189, #190, #185,
#254, #224, #225, #246, #228, #229, #244, #227,
#245, #232, #233, #234, #235, #236, #237, #238,
#239, #255, #240, #241, #242, #243, #230, #226,
#252, #251, #231, #248, #253, #249, #247, #250,
#222, #192, #193, #214, #196, #197, #212, #195,
#213, #200, #201, #202, #203, #204, #205, #206,
#207, #223, #208, #209, #210, #211, #198, #194,
#220, #219, #199, #216, #221, #217, #215, #218);
function Koi_WinConvert(const st: string): string;
implementation
{$R *.dfm}
function Koi_WinConvert(const st: string): string;
var
i: integer;
begin
result:= st;
for i:= 1 to length(st) do
if st[i] > #127 then
result[i]:= Koi_WinChars[st[i]];
end;
var
gl: boolean;
FTimer: cardinal;
procedure RemoteTimeProc(
uID, uMsg: UINT; dwUser, dw1, dw2: DWORD); stdcall;
begin
gl:= false;
end;
procedure TForm1.mDestKeyPress(Sender: TObject; var Key: Char);
begin
if Key = #13 then
begin
if gl then
begin
gl:= false;
mDest.Perform(WM_CHAR, 8, 0);
btnTranslate.OnClick(nil);
Key:= #0;
timeKillEvent(FTimer)
end
else
begin
gl:= true;
FTimer:= timeSetEvent(500, 0, @RemoteTimeProc,
DWORD(Self), TIME_PERIODIC);
end;
end
else
begin
gl:= false;
timeKillEvent(FTimer);
end;
end;
procedure TForm1.btnTranslateClick(Sender: TObject);
begin
mDest.Lines.Clear;
mDest.Refresh;
mDest.Text:= Translate_Google(
mSource.Text, 'en', 'ru', 'D:\1.mp3', false);
end;
function TForm1.Translate_Google(
text, myLang, toLang, FileName: string;
Play: boolean): string;
{ Основной модуль запроса перевода }
var
idHTTP1: TidHTTP;
data: TStringList;
Response: TStringStream;
s: string;
k, n: integer;
ms: TMemoryStream;
begin
data:= TStringList.Create;
idHTTP1:= TIdHTTP.Create(nil);
// меняем перевод строки на пробел и убираем троеточие
text:= StringReplace(StringReplace(StringReplace(
text, #13#10, ' ', [rfReplaceAll]), '...', '',
[rfReplaceAll]), '.', ',', [rfReplaceAll]);
// запрос http://translate.google.ru/#en/ru/trestle%20table
data.Add('sl=en');
data.Add('tl=ru');
data.Add('js=n');
data.add('prev=_t');
data.Add('hl=ru');
data.Add('ie=UTF-8');
data.add('eotf=1');
data.Add('text=' + text);
// data.Add('file=#submit');
data.Add('client=x');
Response:= TStringStream.Create('');
// showmessage(data.Text);
try
Idhttp1.Post('https://translate.google.ru/translate_a/t', data, Response);
except
ShowMessage('Ошибка подключения.' + #13#10 +
'Проверьте соединение. ');
end;
// парсер
s:= Response.DataString;
k:= Pos('trans":"', s);
n:= PosEx('","', s, k);
s:= Copy(s, k + 8, n - k - 8);
// Вывод данных с перекодировкой
result:= Koi_WinConvert(s);
// error #10054 #10060
// Озвучка
// Получение файла озвучки перевода
ms:= TMemoryStream.Create;
// Параметры запроса озвучки
s:= Koi_WinConvert(s);
//s:= Memo1.Text;
data.Clear;
data.Add('ie=UTF-8');
data.Add('q=' + s);
data.Add('tl=ru');
// data.Add('prev=input');
// Отсылаем запрос
idHTTP1.Post('https://translate.google.com/translate_tts', data, ms);
// сохраняем MP3
ms.SaveToFile(FileName);
data.Free;
idHTTP1.Free;
ms.Free;
// стоп на всякий случай
mciSendString(PChar('stop ' + FileName), nil, 0, 0);
// воспроизводим перевод
play:= CheckBox1.Checked;
if Play then mciSendString(PChar('play ' + FileName), nil, 0, 0);
end;
end.
Комментарии