asp.net 2.0实现语言和主题的切换

本文详细阐述了在ASP.NET2.0中实现多语言转换及主题样式的步骤,包括使用DropDownList收集用户语言偏好,通过Profile、Cookies、Session或QueryStrings存储并应用这些设置。同时讨论了全局事件如Application_BeginRequest用于初始化文化的可行性,并对比了自定义BasePage类的方法以减少重复代码。最后,强调了在不同场景下选择合适的技术路径的重要性。

在ASP.NET 2.0 中提供多语言转换和多样式主题转换功能,两种实现形式比较类似,所以放在一起说明一下。
  1. Language switcher 多语言转换
  在Quick Start Tutorial 中,介绍了如何存储和应用用户选择的语言。一般是用一个DropDownList展示支持的语言,供用户选择,通常是放在masterpage 里面,将用户选择的语言存储起来 这里用了ASP.NET 2.0的Profile,当然也可以存在cookie session 或者querystring里。在页面里重写InitializeCulture 方法,使用用户之前选择的语言。因为设置语言的操作 (这里是SelectedIndexChanged事件)发生在InitializeCulture 时间后面,所以在设置操作完成后为了使的当前页面也马上生效,需要做个重转向,以从新加载本页面,触发InitializeCulture 事件。下面使quickstart中的部分代码,注意红色部分。因为有的页面地址后面可能还存在queystring,所以个人觉得红色代码部分最好用Response.Redirect(Request.Url.PathAndQuery);代替。
   protected void DropDownLanguage_SelectedIndexChanged(object sender, EventArgs e)
   {
   string SelectedLanguage = DropDownLanguage.SelectedValue.ToString();
   //Save selected user language in profile
   Profile.SetPropertyValue("PreferredCulture", SelectedLanguage);
  
   //Force re-initialization of the page to fire InitializeCulture()
   Response.Redirect(Request.Url.LocalPath);
   }
   protected override void InitializeCulture()
   {
   // override virtual method InitializeCulture() to check if profile contains a user language setting
   string UserCulture = Profile.GetPropertyValue("PreferredCulture").ToString();
   if ( UserCulture != "")
   {
   // there is a user language setting in the profile: switch to it
   Thread.CurrentThread.CurrentUICulture = new CultureInfo(UserCulture);
   Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(UserCulture);
   }
   }
  为了减少代码的重复,一般会自定义一个customer base page类,使它继承Page类,然后在自定义的页基类中重新InitializeCulture方法。最后把你的每个页面继承自你的自定义页面基类。这样你就不需要每个页面都重写InitializeCulture方法了。
  
  但是上面这个方法还是不是很爽,因为每添加一个页面都要去修改后置代码,来继承自定义页基类。
  我们注意到,在InitializeCulture方法中实际上只是修改了当前线程的Culture和UICulture。那么可不可以在一个全局的事件中,比如Application的某个事件,来修改这两个属性呢?很早以前我这么试过,在Application的BeginRequest事件触发时来实现InitializeCulture 的细节,类似于下面代码:
   void Application_BeginRequest(object sender, EventArgs e)
   {
   string lang = string.Empty;//default to the invariant culture
   lang = Profile.PreferredCulture;
   if (string.IsNullOrEmpty(lang))

在ASP.NET 2.0 中提供多语言转换和多样式主题转换功能,两种实现形式比较类似,所以放在一起说明一下。 1. Language switcher 多语言转换 在Quick Start Tutorial 中,介绍了如何存储和应用用户选择的语言。一般是用一个DropDownList展示支持的语言,供用户选择,通常是放在masterpage 里面,将用户选择的语言存储起来 这里用了ASP.NET 2.0的Profile,当然也可以存在cookie session 或者querystring里。在页面里重写InitializeCulture 方法,使用用户之前选择的语言。因为设置语言的操作 (这里是SelectedIndexChanged事件)发生在InitializeCulture 时间后面,所以在设置操作完成后为了使的当前页面也马上生效,需要做个重转向,以从新加载本页面,触发InitializeCulture 事件。下面使quickstart中的部分代码,注意红色部分。因为有的页面地址后面可能还存在queystring,所以个人觉得红色代码部分最好用Response.Redirect(Request.Url.PathAndQuery);代替。 protected void DropDownLanguage_SelectedIndexChanged(object sender, EventArgs e) { string SelectedLanguage = DropDownLanguage.SelectedValue.ToString(); //Save selected user language in profile Profile.SetPropertyValue("PreferredCulture", SelectedLanguage); //Force re-initialization of the page to fire InitializeCulture() Response.Redirect(Request.Url.LocalPath); } protected override void InitializeCulture() { // override virtual method InitializeCulture() to check if profile contains a user language setting string UserCulture = Profile.GetPropertyValue("PreferredCulture").ToString(); if ( UserCulture != "") { // there is a user language setting in the profile: switch to it Thread.CurrentThread.CurrentUICulture = new CultureInfo(UserCulture); Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(UserCulture); } } 为了减少代码的重复,一般会自定义一个customer base page类,使它继承Page类,然后在自定义的页基类中重新InitializeCulture方法。最后把你的每个页面继承自你的自定义页面基类。这样你就不需要每个页面都重写InitializeCulture方法了。 但是上面这个方法还是不是很爽,因为每添加一个页面都要去修改后置代码,来继承自定义页基类。 我们注意到,在InitializeCulture方法中实际上只是修改了当前线程的Culture和UICulture。那么可不可以在一个全局的事件中,比如Application的某个事件,来修改这两个属性呢?很早以前我这么试过,在Application的BeginRequest事件触发时来实现InitializeCulture 的细节,类似于下面代码: void Application_BeginRequest(object sender, EventArgs e) { string lang = string.Empty;//default to the invariant culture lang = Profile.PreferredCulture; if (string.IsNullOrEmpty(lang)) { lang = string.Empty; } Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo(lang); Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(lang); } 注意红色部分应用其他方式取代,因为在beginrequest触发阶段,profile对象还没有被asp.net创建。可以用cookies取代。 我记得当时这么做后,语言设置后并不起作用,当时认为在全局事件中处理,可能到后来还是会被覆盖掉,所以可能不行。所以当时还是用了 InitializeCulture方法。今天在asp.net论坛里看到有人如此实现了, void Application_BeginRequest(Object se

详细出处参考:http://www.itqun.net/content-detail/192431.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值