国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

ASP.NET 2.0 Language Swithcer and Theme Swicher 多語言轉換和多樣式主題轉換

2019-11-18 17:16:40
字體:
來源:轉載
供稿:網友

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 sender, EventArgs e){
      string lang = string.Empty;//default to the invariant culture
      HttpCookie cookie = Request.Cookies["DropDownName"];

      if (cookie != null && cookie.Value != null)
         lang = Request.Form[cookie.Value];

      Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo(lang);
      Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(lang);
}


 

所以覺得當時可能哪里沒有設置好,于是又試了一次,原來是頁面頭指令<%@ Page UICulture="auto" Culture="auto" %>的原因,如果在頁面中設置了UICulture和Culture后,它們就會覆蓋掉在全局中的設置。去掉之后,全局設置起作用了。看來頁面中的culture的設置會覆蓋全局的設置,而頁面中InitializeCulture方法(確切說是一切支持該方法的控件)的設置會覆蓋頁面的設置。其實在Page類中InitializeCulture方法的默認實現是空的,因此再將頁面頭指令 UICulture="auto" Culture="auto" 去掉后,Global中的設置就起作用了。
另外,如果很想使用Profile(像我一樣)來存儲用戶的選擇,那就不能在beginrequest階段來處理了,我是在PreRequestHandlerExecute事件觸發時處理:
    void Application_PreRequestHandlerExecute(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);
       }
這個時候Profile已經被創建了,所以可以使用了。
2. 多樣式主題轉換 Theme switcher
這篇文章講了Theme的切換,覺得形式上和語言的切換很類似。他使用了HttpModule,我覺得直接放在Global.asax文件里對應的事件處理發放下就可以了,說到底都是一樣的。他的存儲采用了cookie,我還時覺得用Profile好,既然提供了就用唄,Profile應該是有緩存的吧,所以性能應該不是問題。

出處:厚積而勃發 BLOG


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 阿克苏市| 东明县| 宣汉县| 沽源县| 棋牌| 醴陵市| 扬中市| 宜都市| 绥中县| 石河子市| 邹城市| 金塔县| 腾冲县| 共和县| 临泽县| 察隅县| 红河县| 万宁市| 丽水市| 城市| 西华县| 板桥市| 平遥县| 遂宁市| 哈巴河县| 柳河县| 沙洋县| 同心县| 札达县| 墨玉县| 伽师县| 郁南县| 佛山市| 富阳市| 临海市| 尼木县| 海淀区| 阳曲县| 瓦房店市| 望都县| 丹阳市|