多语言开发 之 通过基页类及Session 动态响应用户对语言的选择

在用户通过UserLogin.aspx登录系统时 提供其对语言的选择
选择后 将所选存入Session 以便登录系统后的其他页面进行按语言显示
当然相关页面需要支持多语言
具体信息可参看
使用 根据语言环境不同 而显示不同的 资源本地化 ASP.NET 网页
App_Code下定义基页类 BasePage.cs

Code
<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />-->usingSystem;
usingSystem.Data;
usingSystem.Configuration;
usingSystem.Web;
usingSystem.Web.Security;
usingSystem.Web.UI;
usingSystem.Web.UI.WebControls;
usingSystem.Web.UI.WebControls.WebParts;
usingSystem.Web.UI.HtmlControls;
usingMyStudy.BLL;

usingSystem.Threading;
usingSystem.Globalization;

namespaceMyStudy.Common
{
///<summary>
///BasePage的摘要说明
///</summary>
publicclassBasePage:System.Web.UI.Page
{
publicBasePage()
{
//
//TODO:在此处添加构造函数逻辑
//
}

//各子页可公用的相关信息
publicstringMyCommonInfo
{
get
{
//
return"";
}
}

//处理用户所选择的语言种类
protectedoverridevoidInitializeCulture()
{
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
<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />-->publicpartialclassUserLogin:BasePage
{
protectedvoidPage_Load(objectsender,EventArgse)
{
if(!IsPostBack)
{
if(Session["myCurrentUICulture"]!=null&&Session["myCurrentCulture"]!=null)
{
if(Session["myCurrentUICulture"].ToString()!=""&&Session["myCurrentCulture"].ToString()!="")
{
this.ddl_Language.SelectedValue=Session["myCurrentUICulture"].ToString();//
}
}
else
{
stringstr=System.Threading.Thread.CurrentThread.CurrentUICulture.ToString();
this.ddl_Language.SelectedValue=str;//
Session["myCurrentUICulture"]=this.ddl_Language.SelectedValue;
Session[
"myCurrentCulture"]=this.ddl_Language.SelectedValue;
}
}
}

protectedvoidddl_Language_SelectedIndexChanged(objectsender,EventArgse)
{
Session[
"myCurrentUICulture"]=this.ddl_Language.SelectedValue;
Session[
"myCurrentCulture"]=this.ddl_Language.SelectedValue;

Response.Redirect(
"UserLogin.aspx",true);
}
}

你可能感兴趣的:(session)