ASP Google的translate API代码

所属分类: 网络编程 / ASP编程 阅读数: 725
收藏 0 赞 0 分享
直接看代码:
复制代码 代码如下:

Class GoogleTranslator
sub Class_Initialize()
RURI="http://translate.google.com/translate_t?langpair={0}&text={1}"
End Sub
Private Opt_ '
Property Get Opt
Opt=Opt_
End Property
Property Let Opt(Opt_s)
Opt_=Opt_s
End Property
Private RURI
Function AnalyzeChild(patrn,texts,IPos)
Dim regEx, Match, Matches
Set regEx = New RegExp
regEx.IgnoreCase = true
regEx.Global = True
regEx.Pattern = patrn
regEx.Multiline = True
Dim RetStr
Set Matches = regEx.Execute(texts)
If(Matches.Count > 0)Then RetStr= Matches(0).SubMatches(IPos)
AnalyzeChild=RetStr
Set regEx =Nothing
End Function
Function getHTTPPage(url)
dim objXML
set objXML=server.createobject("MSXML2.XMLHTTP")'定义
objXML.open "GET",url,false'打开
objXML.send()'发送
If objXML.readystate<>4 then
exit function
End If
getHTTPPage=BytesToBstr(objXML.responseBody)
set objXML=nothing'关闭
if err.number<>0 then err.Clear
End Function
Function BytesToBstr(body)
dim objstream
set objstream = Server.CreateObject("adodb.stream")
objstream.Type = 1
objstream.Mode =3
objstream.Open
objstream.Write body
objstream.Position = 0
objstream.Type = 2
objstream.Charset = "utf-8"
'转换原来默认的UTF-8编码转换成GB2312编码,否则直接用XMLHTTP调用有中文字符的网页得到的将是乱码
BytesToBstr = objstream.ReadText
objstream.Close
set objstream = nothing
End Function
Public Function GetText(str)
If(isempty(str)) Then Exit Function
Dim newUrl,Rs
newUrl=Replace(Replace(RURI,"{0}",Server.URLEncode(Opt)),"{1}",Server.URLEncode(str))
Rs=getHTTPPage(newUrl)
GetText = AnalyzeChild("(<div id=result_box dir=""ltr"">)([?:\s\S]*?)(</div>)",Rs,1)
End Function
Sub class_Terminate
End Sub
End Class


然后使用的时候:
复制代码 代码如下:

Dim Obj
Set Obj = new GoogleTranslator
Obj.Opt="zh-CN|en"
response.write(Obj.GetText("我们"))

然后就可以了,需要注意的是因为Google的任何产品都是UTF-8格式的,这个asp文件保存为UTF-8格式,并在开头加上:
<%@ LANGUAGE=VBScript CodePage=65001%>
<%Option Explicit
'... 开抄我上面的代码
就可以了。
更多精彩内容其他人还在看

IIS访问ASP页面时报错The requested resource is in use.的解决办法

IIS访问ASP页面时报错The requested resource is in use.的解决办法
收藏 0 赞 0 分享

错误类型:Provider (0x80004005)未指定的错误 的一个处理方法

一般情况下asp可以正常运行,但只要连接数据库就提示,Microsoft JET Database Engine 错误'80004005'
收藏 0 赞 0 分享

关于“未指定的错误”的问题 的比较正解的解决方法

关于“未指定的错误”的问题 的比较正解的解决方法
收藏 0 赞 0 分享

关于asp+access的安全问题分析

关于asp+access的安全问题分析
收藏 0 赞 0 分享

把RS.GetRows看得更清楚

把RS.GetRows看得更清楚
收藏 0 赞 0 分享

ASP面向对象编程探讨及比较

ASP面向对象编程探讨及比较
收藏 0 赞 0 分享

ASP错误处理

ASP错误处理
收藏 0 赞 0 分享

web文件管理器的后续开发

web文件管理器的后续开发
收藏 0 赞 0 分享

上一篇,下一篇过程代码

上一篇,下一篇过程代码
收藏 0 赞 0 分享

一小偷类!!有兴趣的可以看看

一小偷类!!有兴趣的可以看看
收藏 0 赞 0 分享
查看更多