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

本文介绍了一种在ASP.NET应用程序中实现多语言支持的方法。通过在登录页面UserLogin.aspx提供语言选择功能,并利用Session保存用户的选择,使得后续页面能够根据用户的语言偏好显示相应的内容。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

ContractedBlock.gif ExpandedBlockStart.gif 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();
}
}
}

那么登录页面的示例代码为:

ContractedBlock.gif ExpandedBlockStart.gif 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);
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值