网络编程
位置:首页>> 网络编程>> JavaScript>> 利用ajax制作在线翻译聊天室

利用ajax制作在线翻译聊天室

作者:baula84 来源:蓝色理想 发布时间:2007-12-28 21:44:00 

标签:翻译,聊天室,ajax,xmlhttp

前几天同学要我帮他做个国际聊天室,要求能够将聊天的内容自动翻译成多国语言.本来想用worldlink的翻译服务,但是用ajax很难获得结果,后来偶想到用google的翻译服务了,用ajax获取翻译结果.

相关文章:Google发布网页在线翻译小工具

演示页面: www.goodeye.com.cn/login.aspx 大家用个123就可以登陆了

核心代码

function send(){
    if(document.getElementById("isTrans").checked){
        ajaxsend();
    
    }else{
        text=document.getElementById("content").value;
        send2DataBase(text);
    }
}
function ajaxsend(){
        text=encodeURI(document.getElementById("content").value);
        var langpair=document.getElementById("langpair").value;
        var hl=document.getElementById("hl").value;
        var file;
    file="123.asp?text="+ text + "&langpair=" + langpair + "&hl=" + hl;
    var objXmlHttp=null;
    if(window.XMLHttpRequest){
        objXmlHttp=new XMLHttpRequest();  
    }else if(window.ActiveXObject){
        objXmlHttp = new ActiveXObject("Microsoft.XMLHTTP");  
        
    }else
    {
        return;
    }
    objXmlHttp.onreadystatechange=function(){
        if(objXmlHttp.readystate==4){
            var strValue=objXmlHttp.responseText;
            if (strValue.length!=0){
            
                try{
                if(strValue.indexOf("<div id=result_box dir=ltr>")>0){
                    var msg=strValue.indexOf("<div id=result_box dir=ltr>")
                    var msg2=strValue.indexOf("</div></td></tr><tr><td id=submitcell>")
                    var result=strValue.substring(msg,msg2);
                    result=result.replace("<div id=result_box dir=ltr>","");
                    
        
                    send2DataBase(result)
                }
                
                }catch(e){
                    alert("翻译服务器设置发生改变!");
                
                }
                
                
                
                
                
            }
            else{
            }
        }
    }
    objXmlHttp.open ('GET', file, true);   
    objXmlHttp.send ('');   
}
  function send2DataBase(txtContent){
   if (txtContent == "") return;
   var user_to = document.getElementById("userlist").value;  //聊天对象
   var textcolor = document.getElementById("textcolor").value;  //颜色
   var expression = document.getElementById("expression").value;  //表情
   var isPublic = !(document.getElementById("isSecret").checked);  //是否密谈   
   
   //调用服务器端方法发送消息
   ChatRoom.SendMsg(txtContent, user_to, textcolor, expression, isPublic);
   
   //更新聊天内容显示
   var div = document.getElementById("chatcontent");
   div.innerHTML = ChatRoom.GetNewMsgString().value + div.innerHTML;
   
   //清空输入框
   document.getElementById("content").value = "";   
  }

123.asp源文件

<%@ CODEPAGE=65001 %>
<% Response.CodePage=65001%>
<% Response.Charset="UTF-8" %>
<%
dim text,langpair,hl
text=trim(request("text"))
langpair=request("langpair")
hl=request("hl")
if text="" and langpair="" and hl="" then
    response.write "don't break my heart!"
end if
dim htmls
text=server.Urlencode(text)
dim strUrl:strUrl="http://translate.google.com/translate_t?text="+text+"&langpair="+langpair+"&hl="+hl+"&oe=UTF8"
htmls=getHTTPPage(strUrl)
response.write htmls
function getHTTPPage(url)
dim Http
set Http=server.createobject("MSXML2.XMLHTTP")
Http.open "GET",url,false
Http.send()
if Http.readystate<>4 then
exit function
end if
getHTTPPage=bytesToBSTR(Http.responseBody,"UTF-8")
set http=nothing
if err.number<>0 then err.Clear
end function
Function BytesToBstr(body,Cset)
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 = Cset
BytesToBstr = objstream.ReadText
objstream.Close
set objstream = nothing
End Function
%>



0
投稿

猜你喜欢

手机版 网络编程 asp之家 www.aspxhome.com