在 Delphi 中使用微软全文翻译的小例子
2021-04-29 23:46
1671 查看
在 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 资源管理器套件
- Delphi 的链式代码
- Delphi 正则表达式语法(10): 选项
- Delphi XE2 之 FireMonkey 入门(34) - 控件基础: TFmxObject: 克隆对象
- Delphi 正则表达式之TPerlRegEx 类的属性与方法(1): 查找
- Delphi 中的自动释放策略
- XP 之后, Delphi 动注册表不方便了...逼出来一个办法:
- Delphi 正则表达式之TPerlRegEx 类的属性与方法(7): Split 函数
- Delphi中获取Unix时间戳(秒、毫秒)
- 论文学习心得 DELPHI: A Cryptographic Inference Service for Neural Networks
- Delphi编译工程包含自己的dcu库报错could not compile used unit ‘XLDCU’
- Delphi编译过程中报错Incompatible types: ´ pwideChar ´ and ´ pansiChar ´,类型不匹配
- 增强Delphi的TStatusBar控件
- delphi十六进制字符串hex转byte数组
- Delphi Byte与数据类型之间的转换
- Delphi的(xor)转成java(xor),以及加密解密
- RadStudio 10.3.3 Rio (Delphi & C++ Builder)及TMS TAdvStringGrid控件安装方法
- delphi 判断点在多边形内
- Delphi中如何调用VC++创建的动态链接库
- delphi 给字符指针分配内存