在.net 1.x 中, 实现多语言功能, by shawl.qiu
来源:互联网 发布:来宾用户 安装软件 编辑:程序博客网 时间:2024/05/18 03:48
在.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.csdn.net/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>
- 在.net 1.x 中, 实现多语言功能, by shawl.qiu
- JScript 中 eval 实现 VBScript ExecuteGlobal 功能, 函数 fExecuteGlobal by shawl.qiu
- c# .net 上传多个文件 By shawl.qiu
- Javascript 中模拟 Enum By shawl.qiu
- asp.net 中 使用 ado 的 Recordset GetString 函数 By shawl.qiu
- js 中RGB十六进制码补色算法及实现 by shawl.qiu
- c# .net 生成缩略图(按比例) By shawl.qiu
- c# .net 生成图片验证码 By shawl.qiu
- sqEditor HTML 编辑器 for .net v1.0 By shawl.qiu
- ASP VBScript 动态包含文件, FSO 实现 By Shawl.qiu
- CSS 弹出菜单, 完美实现, 完美兼容 By shawl.qiu
- ASP 通用文章分页函数(非记录集分页), 返回多个结果, 字典实现 By shawl.qiu
- 关闭Modem拔号音 By Shawl.qiu
- ASP 初阶笔记 By Shawl.qiu
- js prototype 格式化数字 By shawl.qiu
- Javascript prototype 格式化日期 By shawl.qiu
- Javascript 读写 Cookie 操作 By shawl.qiu
- Ajax 强制刷新页面 By shawl.qiu
- 在两个ASP.NET页面之间传递值
- Solaris安装
- OpenAL Lesson 4: The ALC(转载)
- 软件设计师专题: 算法分析与设计
- ftp
- 在.net 1.x 中, 实现多语言功能, by shawl.qiu
- ASP.Net中自定义Http处理及应用之HttpHandler篇
- IIS服务器完全配置
- 实现一个ASP.NET Back控件
- Firefox - 附加软件 - Firebug - Console控制台视图 - console对象输出时,需要注意第一个参数被自动识别为格式字符串
- ASP.NET 2.0,无刷新页面新境界!
- 分页存储过程
- java面试必备
- 可以弹出确认对话框的自定义Web服务器控件ConfirmButton