一个多语言网站的解放方案(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
%>
Demo:
代码:
<!--
==多语言版本解决方案==
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>
posted @ 2008-04-30 08:50  Athrun  阅读(437)  评论(0编辑  收藏  举报