ASP Google的translate API代碼
更新時(shí)間:2008年12月10日 12:36:02 作者:
上篇文字PHP使用Google的translate API寫了以后,突然想做個(gè)ASP的類,以致asp用戶使用。遂寫起,發(fā)現(xiàn)asp忘了不少。如主題吧,思路和C#的這篇是完全一樣的,我只是把它兌成ASP的實(shí)現(xiàn)方式而已。
直接看代碼:
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()'發(fā)送
If objXML.readystate<>4 then
exit function
End If
getHTTPPage=BytesToBstr(objXML.responseBody)
set objXML=nothing'關(guān)閉
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"
'轉(zhuǎn)換原來默認(rèn)的UTF-8編碼轉(zhuǎn)換成GB2312編碼,否則直接用XMLHTTP調(diào)用有中文字符的網(wǎng)頁得到的將是亂碼
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
然后使用的時(shí)候:
Dim Obj
Set Obj = new GoogleTranslator
Obj.Opt="zh-CN|en"
response.write(Obj.GetText("我們"))
然后就可以了,需要注意的是因?yàn)镚oogle的任何產(chǎn)品都是UTF-8格式的,這個(gè)asp文件保存為UTF-8格式,并在開頭加上:
<%@ LANGUAGE=VBScript CodePage=65001%>
<%Option Explicit
'... 開抄我上面的代碼
就可以了。
復(fù)制代碼 代碼如下:
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()'發(fā)送
If objXML.readystate<>4 then
exit function
End If
getHTTPPage=BytesToBstr(objXML.responseBody)
set objXML=nothing'關(guān)閉
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"
'轉(zhuǎn)換原來默認(rèn)的UTF-8編碼轉(zhuǎn)換成GB2312編碼,否則直接用XMLHTTP調(diào)用有中文字符的網(wǎng)頁得到的將是亂碼
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
然后使用的時(shí)候:
復(fù)制代碼 代碼如下:
Dim Obj
Set Obj = new GoogleTranslator
Obj.Opt="zh-CN|en"
response.write(Obj.GetText("我們"))
然后就可以了,需要注意的是因?yàn)镚oogle的任何產(chǎn)品都是UTF-8格式的,這個(gè)asp文件保存為UTF-8格式,并在開頭加上:
<%@ LANGUAGE=VBScript CodePage=65001%>
<%Option Explicit
'... 開抄我上面的代碼
就可以了。
相關(guān)文章
實(shí)現(xiàn)純真IP txt轉(zhuǎn)mdb數(shù)據(jù)庫的方法
實(shí)現(xiàn)純真IP txt轉(zhuǎn)mdb數(shù)據(jù)庫的方法...2007-07-07Session.TimeOut的最大取值是1440,超出會(huì)報(bào)錯(cuò)
Session.TimeOut的取值以分鐘為單位,最大取值是1440分鐘,也就是24個(gè)小時(shí),如果給它賦一個(gè)更大的值2009-06-06非常不錯(cuò)的ASP+Access數(shù)據(jù)庫的終極安全大法18則
非常不錯(cuò)的ASP+Access數(shù)據(jù)庫的終極安全大法18則...2007-06-06