asp提供在线文章翻译的功能

有时候想为我们的网页提供多语言支持,如果一种语言用一张网页来做实在太麻烦了,幸好Google提供了语言工具功能,下面介绍如何利用它来实现网页多种语言之间的转换。

注:转载请注明出处

lan.htm

 1<form>
 2<select name="lan">
 3<option value="en|de">英语 翻译成 德语</option>
 4<option value="en|es">英语 翻译成 西班牙语</option>
 5<option value="en|fr">英语 翻译成 法语</option>
 6<option value="en|it">英语 翻译成 意大利语</option>
 7<option value="en|pt">英语 翻译成 葡萄牙语</option>
 8<option value="en|ja">英语 翻译成 日语 BETA</option>
 9<option value="en|ko">英语 翻译成 朝鲜语 BETA</option>
10<option value="en|zh-CN">英语 翻译成 中文(简体) BETA</option>
11<option value="de|en">德语 翻译成 英语</option>
12<option value="de|fr">德语 翻译成 法语</option>
13<option value="es|en">西班牙语 翻译成 英语</option>
14<option value="fr|en">法语 翻译成 英语</option>
15<option value="fr|de">法语 翻译成 德语</option>
16<option value="it|en">意大利语 翻译成 英语</option>
17<option value="pt|en">葡萄牙语 翻译成 英语</option>
18<option value="ja|en">日语 翻译成 英语 BETA</option>
19<option value="ko|en">朝鲜语 翻译成 英语 BETA</option>
20<option value="zh-CN|en">中文(简体) 翻译成 英语 BETA</option>
21<input name="Button1" onclick="javascript:window.open('translate.asp?urls='+document.location+'&amp;lan='+lan.value,'_self','')" style="FONT-SIZE: 12px" type="button" value="Go-&gt;"/>
22</select>
23</form>

lan.htm中的内容用来选择要翻译的语言包括原来的语言和要翻译成的语言。我们只需要将lan.htm中的内容拷到提供多语言翻译的页面中就可以了。

translate.asp

1<html>
2<head>
3<title>在线翻译</title>
4<meta content="text/html; charset=utf-8" http-equiv="Content-Type"/>
5</head>
6<body>   

'on error resume next
' 如果网速很慢的话,可以调整以下时间。单位秒
Server.ScriptTimeout = 999999
'========================================================
'字符编码函数
'========================================================
Function BytesToBstr(body,code)
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 =code
BytesToBstr = objstream.ReadText
objstream.Close
set objstream = nothing
End Function

'取行字符串在另一字符串中的出现位置
Function Newstring(wstr,strng)
Newstring=Instr(lcase(wstr),lcase(strng))
if Newstring&lt;=0 then Newstring=Len(wstr)
End Function
'替换字符串函数
function ReplaceStr(ori,str1,str2)
ReplaceStr=replace(ori,str1,str2)
end function
'=====================================================
function ReadXml(url,code,start,ends)
set oSend=createobject("Microsoft.XMLHTTP")
SourceCode = oSend.open ("GET",url,false)
oSend.send()
ReadXml=BytesToBstr(oSend.responseBody,code )
if(start="" or ends="") then
else
start=Newstring(ReadXml,start)
ReadXml=mid(ReadXml,start)
ends=Newstring(ReadXml,ends)
ReadXml=left(ReadXml,ends-1)
end if
end function
dim urlpage,lan
urlpage=request("urls")
lan=request("lan")

1<form action="translate.asp" method="post">
2<input name="urls" size="150" type="text" value="```
3=urlpage
4```"/>
5<input name="lan" type="hidden" value="```
6=lan
7```"/>
8<input type="submit" value="submit"/>
9</form>   

dim transURL
transURL=" http://216.239.39.104/translate_c?hl=zh-CN&amp;ie=UTF-8&amp;oe=UTF-8&amp;langpair="&amp;server.URLEncode(lan)&amp;"&amp;u="&amp;urlpage&amp;"&amp;prev=/language_tools "
if(len(urlpage)&gt;3) then
getcont=ReadXml(transURL,"gb2312","","")
response.Write(getcont)
end if

1</body>
2</html>

translate.asp实现翻译功能,这是利用google的语言工具实现的。

注意,因为提供多语言支持,所以translate.asp文件所用的编码是支持所有字符的"utf-8"

事例: http://www.tot.name/3.htm

Published At
Categories with Web编程
Tagged with
comments powered by Disqus