开发者

Which event applies UICulture to Page

开发者 https://www.devze.com 2023-02-22 21:38 出处:网络
I know how to apply Localization to a page by creating the resource file. but one thing is really hurting to my mind since few days is, How the UIculture is getting applied to a ASP.NET page in the ba

I know how to apply Localization to a page by creating the resource file. but one thing is really hurting to my mind since few days is, How the UIculture is getting applied to a ASP.NET page in the background of the screen ?

The thoughts coming to mind is in the events of Page Life Cycle. If yes, then in which event 开发者_运维百科and how to test that?

Please clarify. Thanks


To set the culture and UI culture for an ASP.NET Web page declaratively

To set the UI culture and culture for all pages, add a globalization section to the Web.config file, and then set the uiculture and culture attributes, as shown in the following example:

<globalization uiculture="es" culture="es-MX" />

To set the UI culture and culture for an individual page, set the Culture and UICulture attributes of the @ Page directive, as shown in the following example:

<%@ Page UICulture="es" Culture="es-MX" %>

To have ASP.NET set the UI culture and culture to the first language that is specified in the current browser settings, set UICulture and Culture to auto. Alternatively, you can set this value to auto:culture_info_name, where culture_info_name is a culture name. For a list of culture names, see CultureInfo. You can make this setting either in the @ Page directive or Web.config file.

To set the culture and UI culture for an ASP.NET Web page programmatically

  1. Override the InitializeCulture method for the page.
  2. In the overridden method, determine which language and culture to set the page to.
    • Note: The InitializeCulture method is called very early in the page life cycle, before controls are created or properties are set for the page. Therefore, to read values that are passed to the page from controls, you must get them directly from the request using the Form collection.
  3. Set the UI culture and culture in one of the following ways:
    • Set the Culture and UICulture properties of the page to the language and culture string (for example, en-US). These properties are internal to the page, and can only be used in a page.
    • Set the CurrentUICulture and CurrentCulture properties of the current thread to the UI culture and culture, respectively. The CurrentUICulture property takes a language and culture information string. To set the CurrentCulture property, you create an instance of the CultureInfo class and call its CreateSpecificCulture method.

C# code sample:

protected override void InitializeCulture()
{
    if (Request.Form["ListBox1"] != null)
    {
        String selectedLanguage = Request.Form["ListBox1"];
        UICulture = selectedLanguage ;
        Culture = selectedLanguage ;

        Thread.CurrentThread.CurrentCulture = 
            CultureInfo.CreateSpecificCulture(selectedLanguage);
        Thread.CurrentThread.CurrentUICulture = new 
            CultureInfo(selectedLanguage);
    }
    base.InitializeCulture();
}

See below for a detailed page lifecycle:

  1. Construct
  2. ProcessRequest
  3. FrameworkInitialize
  4. InitializeCulture
  5. If child controls are present
    1. AddParsedSubObject
    2. CreateControlCollection
    3. AddedControl
    4. ResolveAdapter
  6. DeterminePostBackMode
  7. OnPreInit
  8. OnInit
  9. TrackViewState
  10. OnInitComplete
  11. OnPreLoad
  12. OnLoad
  13. OnLoadComplete
  14. EnsureChildControls
    1. CreateChildControls
  15. OnPreRender
  16. OnPreRenderComplete
  17. SaveViewState
  18. OnSaveStateComplete
  19. CreateHtmlTextWriter
  20. RenderControl
  21. Render
    1. RenderChildren
    2. VerifyRenderingInServerForm
  22. OnUnload
  23. Dispose


Besides settings in web.config, and page tag there is a page method InitializeCulture(), you can override it to include you extra logic.

protected override void InitializeCulture()
{
   // include your extra logic
   //base.Culture
   base.InitializeCulture();
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号