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

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

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

 

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

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

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值