delphi利用bing翻译API做的多国语言翻译软件
以下是参照万一老师的例子,做的多国语言翻译软件,可以自动判断输入的语言。软件中用到了http连接技术、json技术和xml解析技术、转化UTF8字符串。软件中还有一些问题,例如可以得到bing支持的各种语言代号,但是怎么转化为中文描述的语言名称等,请大家给予帮助。
直接上代码:
unit Unit7;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs,msxml, StdCtrls, ExtCtrls,IniFiles,XMLDoc, xmldom, XMLIntf, msxmldom;
type
TMainForm = class(TForm)
Memo1: TMemo;
Splitter1: TSplitter;
Memo2: TMemo;
Panel1: TPanel;
Button1: TButton;
ComboBox1: TComboBox;
Label1: TLabel;
XMLDocument1: TXMLDocument;
procedure Memo1KeyPress(Sender: TObject; var Key: Char);
procedure Button1Click(Sender: TObject);
procedure Memo1Change(Sender: TObject);
procedure ComboBox1Change(Sender: TObject);
procedure FormShow(Sender: TObject);
private
{ Private declarations }
langList:THashedStringList;
function Translate(AAppID: string; AText: string; InLanguage: string=\'en\'; OutLanguage: string=\'zh-CHS\'): string;
procedure initParam;
public
{ Public declarations }
end;
const
GETLANGUAGESFORTRANSLATE=\'http://api.microsofttranslator.com/v2/Http.svc/GetLanguagesForTranslate?appId=%s\';
GETLANGUAGENAMES=\'http://api.microsofttranslator.com/v2/Http.svc/GetLanguageNames?appId=%s&locale=%s&languageCodes=%s\';
DETECT=\'http://api.microsofttranslator.com/V2/http.svc/Detect?appId=%s&text=%s\';
TRANSLATESTR= \'http://api.microsofttranslator.com/V2/http.svc/Translate?appId=%s&text=%s&from=%s&to=%s\';
APPID= \'C98FDCE95A2FD7BA417F260D43763C40232E****\';
var
MainForm: TMainForm;
implementation
{$R *.dfm}
//字符串到 UTF8 编码的函数, 用于 Google 地址
function ToUTF8Encode(str: string): string;
var
b: Byte;
begin
for b in BytesOf(UTF8Encode(str)) do
Result := Format(\'%s%s%.2x\', [Result, \'%\', b]);
end;
procedure TMainForm.Button1Click(Sender: TObject);
begin
initParam;
end;
procedure TMainForm.ComboBox1Change(Sender: TObject);
begin
Button1.Enabled:= (length(trim(memo1.text))>0) and (ComboBox1.ItemIndex>=0);
end;
procedure TMainForm.FormShow(Sender: TObject);
var
I:Integer;
begin
ComboBox1.Items.Clear;
langList:=THashedStringList.Create;
with langList do
begin
add(\'zh-CHS=简体中文\');
add(\'zh-CHT=繁体中文\');
add(\'en=英语\');
add(\'ar=阿拉伯语\');
add(\'bg=保加利亚语\');
add(\'ca=加泰罗尼亚人语\');
add(\'cs=捷克语\');
add(\'da=丹麦语\');
add(\'nl=荷兰语\');
add(\'et=爱沙尼亚语\');
add(\'fi=芬兰语\');
add(\'fr=法语\');
add(\'de=德语\');
add(\'el=希腊语\');
add(\'ht=海地语\');
add(\'he=希伯来语\');
add(\'hi=北印度语\');
add(\'hu=匈牙利语\');
add(\'id=印尼语群\');
add(\'it=意大利语\');
add(\'ja=日本语\');
add(\'ko=韩语\');
add(\'lv=拉脱维亚语\');
add(\'lt=立陶宛语\');
add(\'no=挪威语\');
add(\'pl=波兰语\');
add(\'pt=葡萄牙语\');
add(\'ro=罗马尼亚语\');
add(\'ru=俄语\');
add(\'sk=斯洛伐克语\');
add(\'sl=斯洛文尼亚语\');
add(\'es=西班牙语\');
add(\'sv=瑞典语\');
add(\'th=泰文\');
add(\'tr=土耳其语\');
add(\'uk=乌克兰语\');
add(\'vi=越南语\');
end;
for I := 0 to langList.Count - 1 do
begin
ComboBox1.Items.Add(langList.ValueFromIndex[i]);
end;
ComboBox1.ItemIndex:=0;
end;
procedure TMainForm.Memo1Change(Sender: TObject);
begin
Button1.Enabled:= (length(trim(memo1.text))>0) and (ComboBox1.ItemIndex>=0);
end;
procedure TMainForm.Memo1KeyPress(Sender: TObject; var Key: Char);
begin
if key=#13 then
initParam;
end;
function TMainForm.Translate(AAppID, AText, InLanguage,
OutLanguage: string): string;
var
Url: string;
req: IXMLHTTPRequest;
begin
Url := Format(TRANSLATESTR, [AAppID, AText, InLanguage, OutLanguage]);
req := CoXMLHTTP.Create;
req.open(\'Get\', Url, False, EmptyParam, EmptyParam);
req.send(EmptyParam);
XMLDocument1.LoadFromXML( req.responseText);
Result := XMLDocument1.DocumentElement.Text; //去掉前后的标签
end;
procedure TMainForm.initParam;
var
langFrom: string;
req: IXMLHTTPRequest;
tolang: string;
URL: string;
begin
Url := Format(DETECT, [APPID, ToUTF8Encode(Memo1.Text)]);
req := CoXMLHTTP.Create;
req.open(\'Get\', URL, False, EmptyParam, EmptyParam);
req.send(EmptyParam);
langFrom := req.responseText;
XMLDocument1.LoadFromXML(langFrom);
langFrom := XMLDocument1.DocumentElement.Text;
tolang := langList.Names[ComboBox1.ItemIndex];
memo2.Text := Translate(APPID, ToUTF8Encode(Memo1.Text), langfrom, tolang);
end;end.