ASP.NET 2.0: 如何?按一個按鍵在兩種語言之間切換

來源:互聯網
上載者:User
asp.net 我要實現這樣一個功能,就是按一個按鍵在兩種語言(比如中文和英文)之間切換。

註:我用的是ASP.NET Beta 2。參考文章列在最後。

參考第一篇文章,"Implicit Localization Expressions"一節。一個頁面(Page)的本地化內容是在FrameworkInitlize()中建立所有的控制項時決定的。比如

   button1.Text = ((string)
      base.GetLocalResourceObject("LinkButtonResource1.Text"));

所以要在FrameworkInitlize()之前設定好你所需要的頁面的文化(Culture).

第二篇文章"Page Lifecycle"一節列出了頁面的生命週期(Lifecycle)涉及的方法。下面列出了從頁面的建構函式(Constructor)開始與我的問題相關的一些方法。

    Constructor
    Construct
    TestDeviceFilter
    AddParsedSubObject
    DeterminePostBackMode
    OnPreInit
    LoadPersonalizationData
    InitializeThemes
    OnInit

在第三篇文章"The Page Lifecycle"的開頭寫道:

Once the HTTP page handler class is fully identified, the ASP.NET run time calls the handler's ProcessRequest method to process the request. ... (it) begins by calling the method FrameworkInitialize, which builds the controls tree for the page. The method is a protected and virtual member of the TemplateControl class - the class from which Page itself derives. Any dynamically generated handler for an .aspx resource overrides FrameworkInitialize. In this method, the whole control tree for the page is built.

Next, ProcessRequest makes the page transit various phases: initialization, loading of view state information and postback data, loading of the page's user code and execution of postback server-side events. After that, the page enters in rendering mode: the updated view state is collected; the HTML code is generated and then sent to the output console. Finally, the page is unloaded and the request is considered completely served.

從這裡,我推斷FrameworkInitlize()至少是在OnPreInit()之前被調用的。但是仍然看不出來FrameworkInitlize的準確位置。

繞過上面所說的這些。有兩種方法可以實現上述的要求。

方法一。在某頁面的InitializeCulture方法中設定你所需要的文化。比如

public partial class _Default : System.Web.UI.Page
{
    protected override void InitializeCulture()
    {
        string cul = Session["UICulture"];
        if (!String.IsNullOrEmpty(cul))
        {
            Thread.CurrentThread.CurrentUICulture =
                new CultureInfo(cul);
        }
    }
}

但這是針對某一個頁面的。要應用到整個網站,就要多一點兒麻煩。要把這個InitializeCulture方法寫在一個System.Web.UI.Page的子類中,然後讓所有頁面的類繼承於這個子類而不是直接繼承System.Web.UI.Page。

方法二。在PreRequestHandlerExecute中設定你所需要的文化。參見HttpApplication類的線上協助:
An application executes events that are handled by modules or user code that is defined in the Global.asax file in the following sequence:
   1. BeginRequest
   2. AuthenticateRequest
   3. PostAuthenticateRequest
   4. AuthorizeRequest
   5. PostAuthorizeRequest
   6. ResolveRequestCache
      An event handler (a page corresponding to the request URL) is created at this point.
   7. PostResolveRequestCache
   8. PostMapRequestHandler
   9. AcquireRequestState
  10. PostAcquireRequestState
  11. PreRequestHandlerExecute
      The event handler is executed.
  12. PostRequestHandlerExecute
  13. ...

我試過 BeginRequest, 但是不行。我想原因是因為Page.Request在此之後才被建立或賦值,所以在BeginRequest中設定的文化會被覆蓋了。直到 10. PostAcquireRequestState,才被準備好,只有在此之後我們才可以設定所需要的文化。
11和12之間的 "The event handler is executed."就應該是整個頁面的生命週期的處理了。方法一使用InitializeCulture就是在這裡。

這樣就能夠達到最初的目的了。但是還有一個小問題。按鍵的事件處理函數只會在設定了線程的文化之後才會被執行(不論是用PreRequestHandlerExecute,抑或InitializeCulture)。結果就是在頁面的HTML傳到瀏覽器時,所要設的文化與線程的文化不一樣。而在其他的事件發生時,由於所要設的文化沒有改變,就會和線程的文化一樣。這樣程式的行為就不一致了,容易使使用者產生混淆。有一個不太完善的結局方法,就是在按鍵的事件處理函數最後加上下面這句,這樣使用者就能立刻看到改變了的語言了。

Response.Redirect(Request.Path);

缺憾是由於當前頁面要重建,原先的狀態(比如使用者已經輸入的東西)就不會保留了。

另外還有一個與此相關的問題,就是如何本地化sitemap(網頁導航?)。請參考下面這篇文章。
ASP.NET 2.0: 在使用web.sitemap時,如何本地化網頁導航(sitemap)



聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.