ASP Google的translate API代码_应用技巧

直接看代码:

复制代码 代码如下:

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()'发送
If objXML.readystate<>4 then
exit function
End If
getHTTPPage=BytesToBstr(objXML.responseBody)
set objXML=nothing'关闭
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"
'转换原来默认的UTF-8编码转换成GB2312编码,否则直接用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\S]*?)(</div>)",Rs,1)
End Function
Sub class_Terminate
End Sub
End Class

然后使用的时候:

复制代码 代码如下:

Dim Obj
Set Obj = new GoogleTranslator
Obj.Opt="zh-CN|en"
response.write(Obj.GetText("我们"))

然后就可以了,需要注意的是因为Google的任何产品都是UTF-8格式的,这个asp文件保存为UTF-8格式,并在开头加上:
<%@ LANGUAGE=VBScript CodePage=65001%>
<%Option Explicit
'... 开抄我上面的代码
就可以了。

时间: 2024-09-20 04:06:52

ASP Google的translate API代码_应用技巧的相关文章

ASP Google的translate API代码

直接看代码: 复制代码 代码如下: 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 Propert

PHP Google的translate API代码

CODE: header("Content-Type: text/html; charset=utf-8"); class Google_API_translator{ public $opts = array("text" => "", "language_pair" => "en|it"); public $out = ""; function setOpts($opts

asp+ajax仿google搜索提示效果代码_应用技巧

对于更完整的代码可以参考,这个是支持数据库的版本.经过编辑测试.Asp+Ajax仿google搜索提示效果 数据库版需要修改的地方有 复制代码 代码如下: javascript.js var url="ajax.asp"; //后台地址 var time_delayajax=300; //搜索延迟 var time_delayupdown=100; //方向键延迟 obj_div.style.top = (xtop + 20) + "px"; //20差不多是输入框

PHP Google的translate API代码_php技巧

新建一个ANSI的PHP文件,然后创建一个类: 复制代码 代码如下: header("Content-Type: text/html; charset=utf-8"); class Google_API_translator{ public $opts = array("text" => "", "language_pair" => "en|it"); public $out = "&

ASP.NET生成Google网站地图的代码_实用技巧

复制代码 代码如下: /// <summary> /// 生成google网站地图 /// </summary> /// <returns></returns> public static boolBuildGoogleSitemap() { try { string RootDirectory = AppDomain.CurrentDomain.BaseDirectory; XmlTextWriter Writer = new XmlTextWriter(

最详细的ASP.NET微信JS-SDK支付代码_实用技巧

本文实例为大家分享了微信JS SDK支付的具体代码,供大家参考,具体内容如下 模型层实体类: public class JsEntities { /// <summary> /// 公众号id /// </summary> public string appId { get; set; } /// <summary> /// 时间戳 /// </summary> public string timeStamp { get; set; } /// <su

fso asp生成静态html的代码_应用技巧

FSO生成静态HTML文件的时候替换模板标签一直是一个很麻烦的问题,至少我是这么认为的,还要别外做一个模板,麻烦!,我今天看见有一个方法可以解决这个问题         如一个正常的index.asp页面,并且用ASP代码调出数据库中的内容,另建一个makehtml.asp的页面,加入一个textarea域,假设为name="body",将index.asp在textarea里调出来,如:  <textarea name="body"><!--#i

asp.net url分页类代码_实用技巧

复制代码 代码如下: using System; using System.Data; using System.Configuration; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.HtmlControls; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; usi

jQuery AJax调用asp.net webservers的实现代码_实用技巧

aspx页面代码 复制代码 代码如下: <html xmlns="http://www.w3.org/1999/xhtml" > <head runat="server"> <title></title> <script src="JQUERY.JS" type="text/javascript"></script> <style type=&quo