ASP Google's translate API code

Source: Internet
Author: User

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.

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.