ASP Google的translate API代码

来源:asp之家 时间:2011-04-03 11:16:00 

直接看代码:

代码如下:


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 
'... 开抄我上面的代码就可以了。

标签:ASP,Google,translate,API
0
投稿

猜你喜欢

  • ASP Session对象的集合以及属性方法事件

    2009-02-02 09:26:00
  • 百度、谷歌和雅虎的近日LOGO

    2008-05-19 12:11:00
  • 根据表中数据生成insert语句的存储过程

    2008-11-10 12:13:00
  • 带你深入了解SQL Server 2008的独到之处

    2009-01-07 14:20:00
  • JavaScript 实现 Konami Code

    2009-05-26 12:44:00
  • Window.ShowModalDialog使用手册

    2008-02-24 14:42:00
  • js实现input+select组合 抛砖引玉

    2010-08-02 12:34:00
  • Asp 防止网页频繁刷新一法

    2008-04-07 13:03:00
  • WEB手绘稿常用元素 之 箭头

    2009-05-08 12:33:00
  • SQL Server 2005恢复Master库

    2011-05-16 13:11:00
  • asp自带的内存缓存 application

    2011-03-09 11:18:00
  • 用ASP对网页进行限制性的访问

    2008-07-03 13:02:00
  • 典型的三行二列居中高度自适应css布局

    2008-02-22 16:02:00
  • window.location.hash的应用及浏览器的支持测试

    2009-07-07 11:52:00
  • Oracle 用户权限管理方法

    2009-08-15 10:54:00
  • ASP处理多关键词查询实例代码

    2008-11-21 17:36:00
  • 解读MySQL的InnoDB引擎日志工作原理

    2011-01-04 19:59:00
  • X/HTML5 v.s. XHTML2(I)

    2008-06-17 18:00:00
  • 9个Web设计中常见的可用性错误[译]

    2009-03-11 20:25:00
  • 交互设计师心得——核心竞争力

    2010-01-19 13:45:00
  • asp之家 网络编程 m.aspxhome.com