Monday, June 4, 2007

Localization and UICulture="auto" attribute

While develop an application that requires location in all its pages I follow these 3 steps:
1) Develop the pages and generate the resources files (in VS2005 Tools Option --> Local Generate Resource)
2) Create a page where the user selects the language of the application, which is stored in a Session variable or Profile field or Database field.
3) Apply the language selected to all requests in the Application_PreAcquireRequestState.

culture = HttpContext.Current.Session["culture"].ToString();
System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo(culture);
System.Threading.Thread.CurrentThread.CurrentCulture = System.Globalization.CultureInfo.CreateSpecificCulture(culture);

When test the application I found that the changes of language were not reflected in the different pages. This is because the UICulture="auto" attribute of the @Page directive in each page, added automatically in step 1, does that the initialization of the localization succeeds at page level overriding the language selected by the user that is applied in the Application_PreAcquireRequestState. The solve this problem follow the next steps:
· Apply the default culture to all pages in web.config (using globalization tag)
· Delete UICulture="auto" attibute tags added when generating the resources files.

To have a better notion of the execution order of events in a request you can read this article or explore this graphic.

· Globalization:Das Blonde Blog
· Profiles : MSDN2

No comments: