asp+|xml|网站|多语言
新建两个存储语言项的xml文件,放到Languages目录下:
zh-cn.xml
以下是引用片段:
<?xml version="1.0" encoding="utf-8"?>
<Languages>
<aboutus>关于我们</aboutus>
<product>产品展示</product>
</Languages>english.xml
以下是引用片段:
<?xml version="1.0" encoding="utf-8"?>
<Languages>
<aboutus>About us</aboutus>
<product>Products</product>
</Languages>公用函数(方法)Functions.asp,放到Comm目录下:
以下是引用片段:
<%
’-------------------
’函数名: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
%>asp+xml实现多语言网站应用实例:
以下是引用片段:
<!--
==多语言版本解决方案==
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>