View Code :
Copy code The Code is as follows: 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") 'Definition
Objxml. Open "get", URL, false' open
Objxml. Send () 'send
If objxml. readystate <> 4 then
Exit Function
End if
Gethttppage = bytestobstr (objxml. responsebody)
Set objxml = nothing 'disabled
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"
'Convert the original default UTF-8 encoding to gb2312 encoding, otherwise the web page with Chinese characters will be garbled directly by calling 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] *?) (</Div>) ", RS, 1)
End Function
Sub class_terminate
End sub
End Class
And then use:Copy codeThe Code is as follows: dim OBJ
Set OBJ = new googletranslator
OBJ. Opt = "ZH-CN | En"
Response. Write (obj. gettext ("we "))
Then you can, it should be noted that any product of Google is in UTF-8 format, this ASP file is saved as UTF-8 format, and add at the beginning:
<% @ Language = VBScript codePage = 65001%>
<% Option explicit
'... Open the code above
You can.