一个多语言网站的解放方案(asp+xml)
新建两个存储语言项的xml文件,放到Languages目录下:
zh-cn.xml
english.xml
公用函数(方法)Functions.asp,放到Comm目录下:
Demo:
代码:
zh-cn.xml
代码:
<?xml version="1.0" encoding="utf-8"?>
<Languages>
<aboutus>关于我们</aboutus>
<product>产品展示</product>
</Languages>
代码:
<?xml version="1.0" encoding="utf-8"?>
<Languages>
<aboutus>About us</aboutus>
<product>Products</product>
</Languages>
代码:
<%
'-------------------
'函数名:getLang();
'参数Lang:当前语言;
'参数Litem:设定的语言项;
'示例:Call getLang("zh-cn","aboutus");
'Author:Huerreson,huerreson@msn.com
'-------------------
Function getLang(ByVal Lang,Litem)
Dim xmlDom
If objCheck("Microsoft.XMLDOM") then
Set xmlDom = Server.CreateObject("Microsoft.XMLDOM")
elseif objCheck("Microsoft.XMLDOM") then
Set xmlDom = Server.CreateObject("MSXML2.DOMDocument")
else
Response.Write("服务器不支持'Microsoft.XMLDOM'及'MSXML2.DOMDocument',无法使用本功能")
End if
xmlDom.async = false
xmlDom.load(Server.MapPath("Languages/"& Lang &".xml"))
Response.Write(xmlDom.documentElement.selectSingleNode(Litem).text)
Set xmlDoc = Nothing
Set xmlDom = Nothing
End Function
'-------------------
'函数名:objCheck();
'参数ObjName:对象名称;
'示例:Call objCheck("Microsoft.XMLDOM");
'Author:Huerreson,huerreson@msn.com
'-------------------
Function objCheck(ObjName)
on error resume next
objCheck = false
Set Obj = Server.CreateObject (ObjName)
If -2147221005 <> Err then
objCheck = true
end if
Set Obj = nothing
End Function
%>
代码:
<!--
==多语言版本解决方案==
Author:Huerrseon,huerreson@msn.com
-->
<%@LANGUAGE="VBSCRIPT" CODEPAGE="65001"%>
<!--#include file="Comm/functions.asp" -->
<style type="text/css">
#NavMenu{border-bottom:1px dashed #000;}
</style>
<%
Dim LangQuery
LangQuery = lCase(Request.QueryString("lang"))
If Not (IsNull(LangQuery) or LangQuery = "") Then
LangQuery = LangQuery
else
LangQuery = "default"
End if
%>
<div id="NavMenu"><%Call getLang(LangQuery,"aboutus")%></div>
<div id="SwitchLangBar">语言切换<a href="?lang=zh-cn">简体中文</a>--<a href="?lang=english">Englsih</a></div>
申明
非源创博文中的内容均收集自网上,若有侵权之处,请及时联络,我会在第一时间内删除.再次说声抱歉!!!
博文欢迎转载,但请给出原文连接。