XE3随笔19:实例 - 借用 Google 实现全文翻译
调用 Google 翻译的地址格式:
http://ajax.googleapis.com/ajax/services/language/translate?v=1.0&q=" + 原始文本 + "&langpair=" + 原语言 + "%7C" + 目标语言
返回的数据格式如下, 可以用 responseData.translatedText 简单获取:
{
"responseData" : {"translatedText" : "返回的文本"},
"responseDetails" : null,
"responseStatus" : 200
}
本例效果图:

代码文件:
unit Unit1; interface uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls; type
TForm1 = class(TForm)
Memo1: TMemo;
Memo2: TMemo;
Button1: TButton;
Button2: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
end; var
Form1: TForm1; implementation {$R *.dfm} uses MsXML, SuperObject; //字符串到 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; //翻译函数
function Translate(str, RequestLanguage, ResultLanguage: string): string;
const
BaseUrl = 'http://ajax.googleapis.com/ajax/services/language/translate?v=1.0&q=';
var
Url: string;
jo: ISuperObject;
req: IXMLHTTPRequest;
begin
Url := BaseUrl + ToUTF8Encode(str) + '&langpair=' + RequestLanguage + '%7C' + ResultLanguage;
req := CoXMLHTTP.Create;
req.open('Get', Url, False, EmptyParam, EmptyParam);
req.send(EmptyParam);
jo := SO(req.responseText);
Result := jo.Format('%responseData.translatedText%');
end; //英译汉
procedure TForm1.Button1Click(Sender: TObject);
begin
Memo2.Text := Translate(Memo1.Text, 'en', 'zh-cn');
end; //汉译英
procedure TForm1.Button2Click(Sender: TObject);
begin
Memo1.Text := Translate(Memo2.Text, 'zh-cn', 'en');
end; end.
窗体文件:
object Form1: TForm1
Left =
Top =
Caption = 'Form1'
ClientHeight =
ClientWidth =
Color = clBtnFace
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Font.Height = -
Font.Name = 'Tahoma'
Font.Style = []
OldCreateOrder = False
PixelsPerInch =
TextHeight =
object Memo1: TMemo
Left =
Top =
Width =
Height =
Align = alLeft
Lines.Strings = (
'Memo1')
TabOrder =
ExplicitHeight =
end
object Button1: TButton
Left =
Top =
Width =
Height =
Caption = #33521#25991' -> '#20013#25991
TabOrder =
OnClick = Button1Click
end
object Memo2: TMemo
Left =
Top =
Width =
Height =
Align = alRight
Lines.Strings = (
'Memo2')
TabOrder =
ExplicitLeft =
ExplicitTop = -
ExplicitHeight =
end
object Button2: TButton
Left =
Top =
Width =
Height =
Caption = #33521#25991' <- '#20013#25991
TabOrder =
OnClick = Button2Click
end
end
XE3随笔19:实例 - 借用 Google 实现全文翻译的更多相关文章
- 用JSON 和 Google 实现全文翻译
unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms ...
- Python爬虫教程-16-破解js加密实例(有道在线翻译)
python爬虫教程-16-破解js加密实例(有道在线翻译) 在爬虫爬取网站的时候,经常遇到一些反爬虫技术,比如: 加cookie,身份验证UserAgent 图形验证,还有很难破解的滑动验证 js签 ...
- GDPR全文翻译(一)
General Data Protection Regulation <一般数据保护法案>全文翻译(一) 编者按 2016年4月14日,欧洲议会投票通过了商讨四年的<一般数据保护法案 ...
- the evolution of Lua 全文翻译
终于赶在春节前将论文全文翻译完,以后有时间将前面三章重新翻译一次,因为刚开始的时候没打算全文翻译的..第一次每天花25分钟完成这么长的一篇翻译,证明滴水可以穿石,哈哈哈 中文地址:Lua的演进 祝各位 ...
- 关闭google默认打开翻译提醒
关闭google默认打开翻译提醒 在header中添加以下代码: <meta name="google" content="notranslate" /& ...
- XE3随笔18:实例 - 解析 Google 关键字搜索排名
同上例类似, 通过 'http://clients1.google.cn/complete/search?&q=' + "关键字" 可以获取 Google 的关键字搜索排名 ...
- XE3随笔17:实例 - 模拟 Google 搜索
本例测试效果图: 代码文件: unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics ...
- 借用Google API在线生成网站二维码地址方法
二维码其实很早就出现了,在国外很多年前就已经在应用了,国内这两年才开始异常的火爆,智能手机的发展,以及微博.微信等移动应用带动了二维码的普及.那么,如果为网址在线生成二维码呢?下面我们就来介绍一下Go ...
- Android实例-调用GOOGLE的TTS实现文字转语音(XE7+小米2)(无图)
注意:在手机上必须选安装文字转语音引擎“google Text To Speech”地址:http://www.shouji56.com/soft/GoogleWenZiZhuanYuYinYinQi ...
随机推荐
- sudo,linux 新建账号,并开通ssh登录
新建账号需要root账号或sudo权限,sudo配置保存在/etc/sudoers文件. sudoers的配置格式一般为: root ALL=(ALL:ALL) ALL %sudo ALL=(ALL: ...
- BitLocker 加密工具挂起和恢复命令行(windows7)
如果你的硬盘使用BitLocker加密了,但是有时候需要高效率的硬盘做某些事情,可以暂时挂起加密,命令行如下方便做个bat. 挂起: manage-bde -protectors -disable C ...
- 页面可见生Page Visibility
Page Visibility 会在浏览器的 document 对象上添加两个属性 hidden 和 visibilityState .如果当前的标签被激活了,那么 document.hidden 的 ...
- final的使用
final对基本类型,限定常量. final对对象的引用,不可引用其他对象. final的字段,必须在定义时或者构造器内完成初始化.构造内才完成初始化的Blank Final(空白final). cl ...
- C# winform 动态调用WebService
封装的通用方法: using System; using System.Collections.Generic; using System.Text; using System.Xml; using ...
- ATL开发 ActiveX控件的 inf文件模板
ATL开发 ActiveX控件的 inf文件模板
- 考查SQLite 3索引对整数排序的性能影响
做个实验,想了解SQLite3索引对整数排序的性能影响. 用这个测试表,考查绿色那列: id name date 自增型主键 字符串型,随机生成 整数型 随机生成,范围0到54354354 1 bMz ...
- WAMPP安装后mysql无法启动
上午 10:23:42 [mysql] This may be due to a blocked port, missing dependencies, 上午 10:23:42 [mysql] imp ...
- love easily fade
Ubuntu addict and loving it , the answerer 's intro attracted me.When I first read a book on linux ...
- SQL中判断一串字符中是否有特定的字符
),) SET @s='1,2,3,4,5,6,7,8,9,10' 一:SET @sql='select col='''+ replace(@s,',',''' union all select '' ...