Look directly at the code:
Copy Code code 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 ' Off
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 directly using the XMLHTTP to invoke the page with Chinese characters will be garbled
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
And then use the time:
Copy Code code as follows:
Dim OBJ
Set OBJ = new Googletranslator
obj.opt= "Zh-cn|en"
Response.Write (Obj.gettext ("we"))
Then it's okay to note that because any Google product is UTF-8 format, the ASP file is saved in the UTF-8 format and added at the beginning:
<%@ Language=vbscript codepage=65001%>
<%option Explicit
'... Copy my Code.
It's OK.