在.net 1.x 中, 实现多语言功能, by shawl.qiu
说明:
这两天看了N种实现多语言的方案, 但都N麻烦, 动不动就有些功能不支持:(
像类 System.Resources.ResXResouceWriter 和 命名空间 Microsoft.JScript... 在我的.NET Framework 居然找不到...:(
有鉴于此, 虽然功能实现了, 但还是总结总结, 希望对其他人有帮助...
主要是使用 xml 形式的 DataSet 实现...
考虑中英文目录应该是两份站点文件, 所以用 web.config 设置各站点的主要功能... 比如默认语言..Cookie 名等...
嗯, 这个功能现在被我弄得还是挺简单的, 就不多说明了...
目录
1. web.config
2. default.aspx
3. English.xml
4. Chinese.xml
shawl.qiu
2007-10-14
http://blog.youkuaiyun.com/btbtd
下载:
http://files.myopera.com/btbtd/csharp/Example/MulitLanguage_DotNet(CSharp).7z
内容
1. web.config
<?xml version="1.0" encoding="UTF-8" ?>
<configuration>
<appSettings>
<add key="DefaultLanguage" value="Chinese" />
<add key="CookieName" value="CnLangCkName" />
</appSettings>
<location path="." allowOverride="true">
<system.web>
<httpRuntime maxRequestLength="20480"
useFullyQualifiedRedirectUrl="true"
executionTimeout="90"
/>
<!--
<pages validateRequest="false" />
-->
<!--
<customErrors defaultRedirect="/include/error/generalError.html"
mode="RemoteOnly" >
<error statusCode="404" redirect="/include/error/error404.html" />
</customErrors>
-->
<customErrors defaultRedirect="/GlobalError/GeneralError.html"
mode="RemoteOnly">
<error statusCode="404" redirect="/GlobalError/404.htm" />
<error statusCode="403" redirect="/GlobalError/404.htm" />
</customErrors>
<globalization
requestEncoding="UTF-8"
responseEncoding="UTF-8"
fileEncoding="UTF-8"
/>
<!-- <sessionState mode="InProc"
cookieless="false"
timeout="20" >
</sessionState>-->
<!-- <sessionState mode="SQLServer"
cookieless="true"
timeout="20"
sqlConnectionString="data source=127.0.0.1;user id=sqCsAtSysUser;password=sqCsAtSysUserPwd"
>
</sessionState>-->
<compilation defaultLanguage="c#"
debug="true"
numRecompilesBeforeAppRestart="15">
<assemblies>
<!--
<add assembly="SQ"/>
<add assembly="SqNs"/>
-->
</assemblies>
</compilation>
</system.web>
</location>
</configuration>
<!--Compile Marker-->
<configuration>
<appSettings>
<add key="DefaultLanguage" value="Chinese" />
<add key="CookieName" value="CnLangCkName" />
</appSettings>
<location path="." allowOverride="true">
<system.web>
<httpRuntime maxRequestLength="20480"
useFullyQualifiedRedirectUrl="true"
executionTimeout="90"
/>
<!--
<pages validateRequest="false" />
-->
<!--
<customErrors defaultRedirect="/include/error/generalError.html"
mode="RemoteOnly" >
<error statusCode="404" redirect="/include/error/error404.html" />
</customErrors>
-->
<customErrors defaultRedirect="/GlobalError/GeneralError.html"
mode="RemoteOnly">
<error statusCode="404" redirect="/GlobalError/404.htm" />
<error statusCode="403" redirect="/GlobalError/404.htm" />
</customErrors>
<globalization
requestEncoding="UTF-8"
responseEncoding="UTF-8"
fileEncoding="UTF-8"
/>
<!-- <sessionState mode="InProc"
cookieless="false"
timeout="20" >
</sessionState>-->
<!-- <sessionState mode="SQLServer"
cookieless="true"
timeout="20"
sqlConnectionString="data source=127.0.0.1;user id=sqCsAtSysUser;password=sqCsAtSysUserPwd"
>
</sessionState>-->
<compilation defaultLanguage="c#"
debug="true"
numRecompilesBeforeAppRestart="15">
<assemblies>
<!--
<add assembly="SQ"/>
<add assembly="SqNs"/>
-->
</assemblies>
</compilation>
</system.web>
</location>
</configuration>
<!--Compile Marker-->
2. default.aspx
<%@
Page
Language
=
"C#"
AutoEventWireup
=
"True"
%>
<%@ import namespace = "System.Data" %>
<script runat="server">
void Page_Load(Object s, EventArgs e)
{
string Lang = GetLangFromCookie();
DataTable Language = DataSetXmlToDataTable(Lang+".xml", "Language", true);
//Test.Text = GetLangValue(Language, "Test");
SetLangLabel(Language, "Test", Test);
SetLangLabel(Language, "SelectLang", SelectLang);
//ddl.SelectedValue = Lang;
//Response.Write("<br/>Lang: "+Lang);
} // end Page_Load
void DdlIndexChange(Object o, EventArgs e)
{
string DefaultLanguage = ConfigurationSettings.AppSettings["DefaultLanguage"];
string CookieName = ConfigurationSettings.AppSettings["CookieName"];
//Response.Write("<br/>: "+ddl.SelectedValue);
AddCookie(CookieName, ddl.SelectedValue);
// HttpContext.Current.Response.Write("<script>setTimeout(function(){location.href=document.URL;}, 1000);</"+"script>");
//HttpContext.Current.Response.Write("ok");
HttpContext.Current.Response.Redirect(HttpContext.Current.Request.Url.ToString());
}
string GetLangFromCookie()
{
string DefaultLanguage = ConfigurationSettings.AppSettings["DefaultLanguage"];
string CookieName = ConfigurationSettings.AppSettings["CookieName"];
string Lang = ReadCookie(CookieName);
if(Lang!="")
{
if(!System.IO.File.Exists(HttpContext.Current.Server.MapPath(Lang+".xml")))
{
//HttpContext.Current.Response.Write("<br/>Lang: "+Lang);
//HttpContext.Current.Response.Write("<br/>no exists");
Lang = DefaultLanguage;
}
}
if(Lang=="") Lang = DefaultLanguage;
return Lang;
}
string GetLangValue(DataTable Dt, string Key)
{
DataRow[] Dr = Dt.Select("key='"+Key+"'");
if(Dr.Length>0) return Dr[0]["Value"]+"";
return "";
}
void SetLangLabel(DataTable Dt, string Key, Label Lbl)
{
DataRow[] Dr = Dt.Select("key='"+Key+"'");
if(Dr.Length>0) Lbl.Text = Dr[0]["Value"]+"";
else Lbl.Text = "";
}
DataTable DataSetXmlToDataTable(string path, string tblName)
{
DataSet dsTemp = new DataSet();
DataTable dtTemp = new DataTable();
dsTemp.ReadXml(path);
dtTemp=dsTemp.Tables[tblName];
return dtTemp;
}
DataTable DataSetXmlToDataTable(string path, string tblName, bool cov)
{
if(cov)
{
path = System.Web.HttpContext.Current.Server.MapPath(path);
}
return DataSetXmlToDataTable(path, tblName);
}
string ReadCookie(string key)
{
return ReadCookie(key, true);
}
string ReadCookie(string key, bool decode)
{
HttpCookie hc = HttpContext.Current.Request.Cookies[key];
if(hc==null) return "";
if(decode) return HttpContext.Current.Server.UrlDecode(hc.Value);
return hc.Value;
}
void AddCookie(string key, string value)
{
AddCookie(key, value, 60*24*7, true);
}
void AddCookie(string key, string value, int minutes)
{
AddCookie(key, value, minutes, true);
}
void AddCookie(string key, string value, int minutes, string sPath)
{
AddCookie(key, value, minutes, true, sPath);
}
void AddCookie(string key, string value, int minutes, bool encode)
{
if(encode) value = HttpContext.Current.Server.UrlEncode(value);
HttpCookie hc = new HttpCookie(key, value);
DateTime dt = DateTime.Now;
TimeSpan ts = new TimeSpan(0, 0, minutes, 0);
hc.Expires = dt.Add(ts);
HttpContext.Current.Response.Cookies.Add(hc);
}
void AddCookie(string key, string value, int minutes, bool encode, string sPath)
{
if(encode) value = HttpContext.Current.Server.UrlEncode(value);
HttpCookie hc = new HttpCookie(key, value);
DateTime dt = DateTime.Now;
TimeSpan ts = new TimeSpan(0, 0, minutes, 0);
hc.Expires = dt.Add(ts);
hc.Path = sPath;
HttpContext.Current.Response.Cookies.Add(hc);
}
</script>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>shawl.qiu template </title>
</head>
<body>
<form runat="server">
<asp:Label id="SelectLang" runat="Server" />:
<asp:DropDownList id="ddl" runat="Server" AutoPostBack="true"
OnSelectedIndexChanged = "DdlIndexChange"
>
<asp:ListItem>请选择 </asp:ListItem>
<asp:ListItem Value="Chinese">Chinese </asp:ListItem>
<asp:ListItem Value="English">English </asp:ListItem>
</asp:DropDownList>
<hr/>
<asp:Label id="Test" runat="Server" />
</form>
<script type="text/javascript">
/*<![CDATA[*/
/*]]*/
</script>
</body>
</html>
<%@ import namespace = "System.Data" %>
<script runat="server">
void Page_Load(Object s, EventArgs e)
{
string Lang = GetLangFromCookie();
DataTable Language = DataSetXmlToDataTable(Lang+".xml", "Language", true);
//Test.Text = GetLangValue(Language, "Test");
SetLangLabel(Language, "Test", Test);
SetLangLabel(Language, "SelectLang", SelectLang);
//ddl.SelectedValue = Lang;
//Response.Write("<br/>Lang: "+Lang);
} // end Page_Load
void DdlIndexChange(Object o, EventArgs e)
{
string DefaultLanguage = ConfigurationSettings.AppSettings["DefaultLanguage"];
string CookieName = ConfigurationSettings.AppSettings["CookieName"];
//Response.Write("<br/>: "+ddl.SelectedValue);
AddCookie(CookieName, ddl.SelectedValue);
// HttpContext.Current.Response.Write("<script>setTimeout(function(){location.href=document.URL;}, 1000);</"+"script>");
//HttpContext.Current.Response.Write("ok");
HttpContext.Current.Response.Redirect(HttpContext.Current.Request.Url.ToString());
}
string GetLangFromCookie()
{
string DefaultLanguage = ConfigurationSettings.AppSettings["DefaultLanguage"];
string CookieName = ConfigurationSettings.AppSettings["CookieName"];
string Lang = ReadCookie(CookieName);
if(Lang!="")
{
if(!System.IO.File.Exists(HttpContext.Current.Server.MapPath(Lang+".xml")))
{
//HttpContext.Current.Response.Write("<br/>Lang: "+Lang);
//HttpContext.Current.Response.Write("<br/>no exists");
Lang = DefaultLanguage;
}
}
if(Lang=="") Lang = DefaultLanguage;
return Lang;
}
string GetLangValue(DataTable Dt, string Key)
{
DataRow[] Dr = Dt.Select("key='"+Key+"'");
if(Dr.Length>0) return Dr[0]["Value"]+"";
return "";
}
void SetLangLabel(DataTable Dt, string Key, Label Lbl)
{
DataRow[] Dr = Dt.Select("key='"+Key+"'");
if(Dr.Length>0) Lbl.Text = Dr[0]["Value"]+"";
else Lbl.Text = "";
}
DataTable DataSetXmlToDataTable(string path, string tblName)
{
DataSet dsTemp = new DataSet();
DataTable dtTemp = new DataTable();
dsTemp.ReadXml(path);
dtTemp=dsTemp.Tables[tblName];
return dtTemp;
}
DataTable DataSetXmlToDataTable(string path, string tblName, bool cov)
{
if(cov)
{
path = System.Web.HttpContext.Current.Server.MapPath(path);
}
return DataSetXmlToDataTable(path, tblName);
}
string ReadCookie(string key)
{
return ReadCookie(key, true);
}
string ReadCookie(string key, bool decode)
{
HttpCookie hc = HttpContext.Current.Request.Cookies[key];
if(hc==null) return "";
if(decode) return HttpContext.Current.Server.UrlDecode(hc.Value);
return hc.Value;
}
void AddCookie(string key, string value)
{
AddCookie(key, value, 60*24*7, true);
}
void AddCookie(string key, string value, int minutes)
{
AddCookie(key, value, minutes, true);
}
void AddCookie(string key, string value, int minutes, string sPath)
{
AddCookie(key, value, minutes, true, sPath);
}
void AddCookie(string key, string value, int minutes, bool encode)
{
if(encode) value = HttpContext.Current.Server.UrlEncode(value);
HttpCookie hc = new HttpCookie(key, value);
DateTime dt = DateTime.Now;
TimeSpan ts = new TimeSpan(0, 0, minutes, 0);
hc.Expires = dt.Add(ts);
HttpContext.Current.Response.Cookies.Add(hc);
}
void AddCookie(string key, string value, int minutes, bool encode, string sPath)
{
if(encode) value = HttpContext.Current.Server.UrlEncode(value);
HttpCookie hc = new HttpCookie(key, value);
DateTime dt = DateTime.Now;
TimeSpan ts = new TimeSpan(0, 0, minutes, 0);
hc.Expires = dt.Add(ts);
hc.Path = sPath;
HttpContext.Current.Response.Cookies.Add(hc);
}
</script>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>shawl.qiu template </title>
</head>
<body>
<form runat="server">
<asp:Label id="SelectLang" runat="Server" />:
<asp:DropDownList id="ddl" runat="Server" AutoPostBack="true"
OnSelectedIndexChanged = "DdlIndexChange"
>
<asp:ListItem>请选择 </asp:ListItem>
<asp:ListItem Value="Chinese">Chinese </asp:ListItem>
<asp:ListItem Value="English">English </asp:ListItem>
</asp:DropDownList>
<hr/>
<asp:Label id="Test" runat="Server" />
</form>
<script type="text/javascript">
/*<![CDATA[*/
/*]]*/
</script>
</body>
</html>
3. English.xml
<?xml version="1.0" standalone="yes"?>
<NewDataSet>
<Language>
<Key>Test </Key>
<Value>test string </Value>
</Language>
<Language>
<Key>Test1 </Key>
<Value>test1 string </Value>
</Language>
<Language>
<Key>SelectLang </Key>
<Value>Language </Value>
</Language>
</NewDataSet>
<NewDataSet>
<Language>
<Key>Test </Key>
<Value>test string </Value>
</Language>
<Language>
<Key>Test1 </Key>
<Value>test1 string </Value>
</Language>
<Language>
<Key>SelectLang </Key>
<Value>Language </Value>
</Language>
</NewDataSet>
4. Chinese.xml
<?xml version="1.0" standalone="yes"?>
<NewDataSet>
<Language>
<Key>Test </Key>
<Value>测试 </Value>
</Language>
<Language>
<Key>Test1 </Key>
<Value>测试1 </Value>
</Language>
<Language>
<Key>SelectLang </Key>
<Value>语言 </Value>
</Language>
</NewDataSet>
<NewDataSet>
<Language>
<Key>Test </Key>
<Value>测试 </Value>
</Language>
<Language>
<Key>Test1 </Key>
<Value>测试1 </Value>
</Language>
<Language>
<Key>SelectLang </Key>
<Value>语言 </Value>
</Language>
</NewDataSet>