ASP Google's Translate API code _ Application Tips

Source: Internet
Author: User
Tags gettext urlencode
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.
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.