在 Delphi 中使用微软全文翻译的小例子


本文整理自网络,侵删。

 刚刚从博客园看到有朋友用 C# 做了这么个东西, 我用 Delphi 尝试了一下.

需要先去申请一个 AppID: http://www.bing.com/toolbox/bingdeveloper/
使用帮助在: http://msdn.microsoft.com/en-us/library/dd576287.aspx

uses MsXML;{函数}function Translate(AAppID: string; AText: string; InLanguage: string='en'; OutLanguage: string='zh-CHS'): string;const  BaseUrl = 'http://api.microsofttranslator.com/V2/http.svc/Translate?appId=%s&text=%s&from=%s&to=%s';var  Url: string;  req: IXMLHTTPRequest;begin  Url := Format(BaseUrl, [AAppID, AText, InLanguage, OutLanguage]);  req := CoXMLHTTP.Create;  req.open('Get', Url, False, EmptyParam, EmptyParam);  req.send(EmptyParam);  Result := req.responseText;  Result := Copy(Result, 68+1, Length(Result)-68-9); //去掉前后的标签end;{调用测试}procedure TForm1.Button1Click(Sender: TObject);const  myAppId = '65FCA293BDB85C98D16A567C3FECE22272B6****'; //这是我申请的 AppID, 隐藏了后四位begin  Memo2.Text := Translate(myAppId, Memo1.Text);end;


效果图:



使用 Indy:

uses IdHTTP;function Translate2(AAppID: string; AText: string; InLanguage: string='en'; OutLanguage: string='zh-CHS'): string;const  BaseUrl = 'http://api.microsofttranslator.com/V2/http.svc/Translate?appId=%s&text=%s&from=%s&to=%s';var  Url: string;  stream: TStringStream;  idHttpObj: TIdHTTP;begin  stream := TStringStream.Create;  idHttpObj := TIdHTTP.Create(nil);  Url := Format(BaseUrl, [AAppID, Trim(AText), InLanguage, OutLanguage]);  idHttpObj.Get(Url, stream);  Result := stream.DataString;  Result := Copy(Result, 68+1, Length(Result)-68-9); //去掉前后的标签  idHttpObj.Free;  stream.Free;end;

相关阅读 >>

Delphi 捕捉异常 try except语句 和 try finally语句用法以及区别

Delphi webservices传base64字串

Delphi程序只允许运行一个实例的三种方法

Delphi tms web core 通过url 传递参数

idhttp使用时内存猛增,如何解决

winapi 字符及字符串函数(13): lstrcmp、lstrcmpi - 对比串

Delphi eof 判断文件指针是否移动到了文件未尾

Delphi安装*.pas扩展名的控件

Delphi 数字摇号器

Delphi实现解析百度搜索结果link?url=

更多相关阅读请进入《Delphi》频道 >>



打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...