多语言开发 之 通过基页类及Session 动态响应用户对语言的选择
在用户通过UserLogin.aspx登录系统时 提供其对语言的选择
选择后 将所选存入Session 以便登录系统后的其他页面进行按语言显示
当然相关页面需要支持多语言
具体信息可参看
使用 根据语言环境不同 而显示不同的 资源本地化 ASP.NET 网页
App_Code下定义基页类 BasePage.cs
Code
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using MyStudy.BLL;
using System.Threading;
using System.Globalization;
namespace MyStudy.Common
{
/// <summary>
/// BasePage的摘要说明
/// </summary>
public class BasePage : System.Web.UI.Page
{
public BasePage()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
//各子页可公用的相关信息
public string MyCommonInfo
{
get
{
//
return "";
}
}
//处理用户所选择的语言种类
protected override void InitializeCulture()
{
if (Session["myCurrentUICulture"] != null && Session["myCurrentCulture"] != null)
{
if (Session["myCurrentUICulture"].ToString() != "" && Session["myCurrentCulture"].ToString() != "")
{
System.Threading.Thread.CurrentThread.CurrentUICulture = CultureInfo.CreateSpecificCulture(Session["myCurrentUICulture"].ToString()); //"en";
System.Threading.Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(Session["myCurrentCulture"].ToString()); //"en";
}
}
base.InitializeCulture();
}
}
}
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using MyStudy.BLL;
using System.Threading;
using System.Globalization;
namespace MyStudy.Common
{
/// <summary>
/// BasePage的摘要说明
/// </summary>
public class BasePage : System.Web.UI.Page
{
public BasePage()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
//各子页可公用的相关信息
public string MyCommonInfo
{
get
{
//
return "";
}
}
//处理用户所选择的语言种类
protected override void InitializeCulture()
{
if (Session["myCurrentUICulture"] != null && Session["myCurrentCulture"] != null)
{
if (Session["myCurrentUICulture"].ToString() != "" && Session["myCurrentCulture"].ToString() != "")
{
System.Threading.Thread.CurrentThread.CurrentUICulture = CultureInfo.CreateSpecificCulture(Session["myCurrentUICulture"].ToString()); //"en";
System.Threading.Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(Session["myCurrentCulture"].ToString()); //"en";
}
}
base.InitializeCulture();
}
}
}
那么登录页面的示例代码为:
Code
public partial class UserLogin : BasePage
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
if (Session["myCurrentUICulture"] != null && Session["myCurrentCulture"] != null)
{
if (Session["myCurrentUICulture"].ToString() != "" && Session["myCurrentCulture"].ToString() != "")
{
this.ddl_Language.SelectedValue = Session["myCurrentUICulture"].ToString();//
}
}
else
{
string str = System.Threading.Thread.CurrentThread.CurrentUICulture.ToString();
this.ddl_Language.SelectedValue = str;//
Session["myCurrentUICulture"] = this.ddl_Language.SelectedValue;
Session["myCurrentCulture"] = this.ddl_Language.SelectedValue;
}
}
}
protected void ddl_Language_SelectedIndexChanged(object sender, EventArgs e)
{
Session["myCurrentUICulture"] = this.ddl_Language.SelectedValue;
Session["myCurrentCulture"] = this.ddl_Language.SelectedValue;
Response.Redirect("UserLogin.aspx",true);
}
}
public partial class UserLogin : BasePage
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
if (Session["myCurrentUICulture"] != null && Session["myCurrentCulture"] != null)
{
if (Session["myCurrentUICulture"].ToString() != "" && Session["myCurrentCulture"].ToString() != "")
{
this.ddl_Language.SelectedValue = Session["myCurrentUICulture"].ToString();//
}
}
else
{
string str = System.Threading.Thread.CurrentThread.CurrentUICulture.ToString();
this.ddl_Language.SelectedValue = str;//
Session["myCurrentUICulture"] = this.ddl_Language.SelectedValue;
Session["myCurrentCulture"] = this.ddl_Language.SelectedValue;
}
}
}
protected void ddl_Language_SelectedIndexChanged(object sender, EventArgs e)
{
Session["myCurrentUICulture"] = this.ddl_Language.SelectedValue;
Session["myCurrentCulture"] = this.ddl_Language.SelectedValue;
Response.Redirect("UserLogin.aspx",true);
}
}
posted on 2009-06-30 11:30 freeliver54 阅读(551) 评论(0) 编辑 收藏 举报