asp.net 网站多国语言的实现
时间:2010-11-24 来源:clound
1.资源文件管理
对于资源文件的管理,可以使用SE String Resource。
File-New Project 新建项目。选择Add Culture 添加新的语言资源文件,Remove 删除语言文件。
可以方便管理资源文件内容。
选择Generate 设置 默认语言,命名空间、接口、生成路径等点击Generate即可生成该资源文件。
2.项目中使用资源文件
新建项目--在项目中添加个类 LanguageFactory
using System; using System.Collections.Generic; using System.Linq; using System.Web; using Super.Language; /// <summary> ///LanguageFactory 的摘要说明 /// </summary> public class LanguageFactory { static List<ILanguage> _languages; private static Object obj = new object(); private LanguageFactory() { // //TODO: 在此处添加构造函数逻辑 // } public static List<ILanguage> languages { get { if (null == _languages) { lock (obj) { if (null == _languages) { _languages = Language.GetLanguages(); } } } return _languages; } } public static ILanguage defaullang() { //如果cookie的languages值,客户端浏览器语言类型,系统初始化语言类型 return _languages[0]; } }
default页面
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %> <!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 runat="server"> <title></title> </head> <body> <form id="form1" runat="server"> <div> <asp:Label ID="Label5" runat="server" Text="This is a simple demonstration."></asp:Label> <br /> <br /> <asp:DropDownList ID="dropDownListLanguages" runat="server"> </asp:DropDownList> <asp:Button ID="btnApply" runat="server" onclick="btnApply_Click" Text="Apply" /> <br /> <br /> <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label> <br /> <asp:Label ID="Label2" runat="server" Text="Label"></asp:Label> <br /> <asp:Label ID="Label3" runat="server" Text="Label"></asp:Label> <br /> <asp:Label ID="Label4" runat="server" Text="Label"></asp:Label> <br /> </div> </form> </body> </html>
cs页面
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using Super.Language; public partial class _Default : System.Web.UI.Page { private static List<ILanguage> _languages; protected void Page_Load(object sender, EventArgs e) { // if (_languages == null) // _languages = Language.GetLanguages(); _languages = LanguageFactory.languages; var a = Request.UserLanguages; if (IsPostBack == false) { dropDownListLanguages.DataSource = _languages; dropDownListLanguages.DataBind(); } } protected void btnApply_Click(object sender, EventArgs e) { /* * Please refer to : DEMO.WinForm,LocalisationHelper */ //写入cookie ILanguage language = LanguageFactory.defaullang();// _languages[dropDownListLanguages.SelectedIndex]; //string dd= language.Culture.Name; //var BrowsLanguage = HttpContext.Current.Request.UserLanguages; // string d = BrowsLanguage[0]; Label1.Text = language.Home; Label2.Text = language.Other; // Label4.Text = language.ResourceManager.GetString("Help"); } }
最后效果,
附件:WebLanage.rar SSR.zip
相关阅读 更多 +