最新更新 sitemap 网站制作设计本站搜索
网页设计
国外网站 韩国网站 个人主页 手提袋设计 CSS 网页特效 平面设计 网站设计 Flash CMS技巧 服装网站 php教程 photoshop 画册 服务器选用 数据库 Office
虚拟主机 域名注册 云主机 网页设计 客服QQ:8208442
当前位置:首页 > 编程开发 > asp教程

ASP使用Google的translate API

日期:06-20    来源:网页设计秀    作者:cnwebshow.com

上篇文字php使用Google的translate API写了以后,突然想做个asp的类,以致asp用户使用。遂写起,发现asp忘了不少。如主题吧,思路和C#的这篇是完全一样的,我只是把它兑成ASP的实现方式而已。wU9中国设计秀

  直接看代码:wU9中国设计秀
  wU9中国设计秀
Class GoogleTranslatorwU9中国设计秀

    sub Class_Initialize()wU9中国设计秀
        RURI="http://translate.google.com/translate_t?langpair={0}&text={1}"wU9中国设计秀
    End SubwU9中国设计秀

    PRivate Opt_    '    wU9中国设计秀
    Property Get OptwU9中国设计秀
        Opt=Opt_wU9中国设计秀
    End PropertywU9中国设计秀
    Property Let Opt(Opt_s)wU9中国设计秀
        Opt_=Opt_swU9中国设计秀
    End PropertywU9中国设计秀
    Private RURIwU9中国设计秀

    Function AnalyzeChild(patrn,texts,ipos)wU9中国设计秀
        Dim regEx, Match, Matches     wU9中国设计秀
        Set regEx = New RegExp       wU9中国设计秀
        regEx.IgnoreCase = true       wU9中国设计秀
        regEx.Global = True           wU9中国设计秀
        regEx.Pattern = patrn      wU9中国设计秀
        regEx.Multiline = True wU9中国设计秀

        Dim RetStrwU9中国设计秀
        Set Matches = regEx.Execute(texts)      wU9中国设计秀
        If(Matches.Count > 0)Then RetStr= Matches(0).SubMatches(IPos)wU9中国设计秀
        wU9中国设计秀
        AnalyzeChild=RetStrwU9中国设计秀
        Set regEx =Nothing wU9中国设计秀
    End FunctionwU9中国设计秀
    http://bizhi.knowsky.comwU9中国设计秀
    Function getHTTPPage(url)wU9中国设计秀
        dim objxmlwU9中国设计秀
        set objXML=server.createobject("MSXML2.xmlhttp")'定义wU9中国设计秀
        objXML.open "GET",url,false'打开wU9中国设计秀
        objXML.send()'发送wU9中国设计秀
        If objXML.readystate<>4 then wU9中国设计秀
            exit functionwU9中国设计秀
        End IfwU9中国设计秀
            getHTTPPage=BytesToBstr(objXML.responseBody)wU9中国设计秀
        set objXML=nothing'关闭wU9中国设计秀
        if err.number<>0 then err.Clear wU9中国设计秀
    End FunctionwU9中国设计秀

    Function BytesToBstr(body)wU9中国设计秀
        dim objstreamwU9中国设计秀
        set objstream = Server.CreateObject("adodb.stream")wU9中国设计秀
            objstream.Type = 1wU9中国设计秀
            objstream.Mode =3wU9中国设计秀
            objstream.OpenwU9中国设计秀
            objstream.Write bodywU9中国设计秀
            objstream.Position = 0wU9中国设计秀
            objstream.Type = 2wU9中国设计秀
            objstream.Charset = "utf-8"  wU9中国设计秀
            '转换原来默认的UTF-8编码转换成GB2312编码,否则直接用XMLHTTP调用有中文字符的网页得到的将是乱码wU9中国设计秀
            BytesToBstr = objstream.ReadText wU9中国设计秀
        objstream.ClosewU9中国设计秀
        set objstream = nothingwU9中国设计秀
    End FunctionwU9中国设计秀
    Public Function GetText(str)wU9中国设计秀
        If(isempty(str)) Then Exit Function wU9中国设计秀
        Dim newUrl,RswU9中国设计秀
        newUrl=Replace(Replace(RURI,"{0}",Server.URLEncode(Opt)),"{1}",Server.URLEncode(str))wU9中国设计秀
        Rs=getHTTPPage(newUrl)wU9中国设计秀
        GetText = AnalyzeChild("(<div id=result_box dir=""ltr"">)([?:sS]*?)(</div>)",Rs,1)wU9中国设计秀
    End Function wU9中国设计秀
    Sub class_TerminatewU9中国设计秀

    End Sub wU9中国设计秀
End ClasswU9中国设计秀

然后使用的时候:wU9中国设计秀

Dim ObjwU9中国设计秀
Set Obj = new GoogleTranslatorwU9中国设计秀
Obj.Opt="zh-CN|en"wU9中国设计秀
response.write(Obj.GetText("我们"))wU9中国设计秀

然后就可以了,需要注意的是因为Google的任何产品都是UTF-8格式的,这个asp文件保存为UTF-8格式,并在开头加上:wU9中国设计秀

<%@ LANGUAGE=VBScript CodePage=65001%>wU9中国设计秀
<%Option ExplicitwU9中国设计秀
'... 开抄我上面的代码wU9中国设计秀

就可以了。wU9中国设计秀

本文引用地址:/bc/article_46234.html
网站地图 | 关于我们 | 联系我们 | 网站建设 | 广告服务 | 版权声明 | 免责声明