Fredrik Normén's Blog - NSQUARED²
Any fool can write code that a computer can understand. Good programmers write code that humans can understand.

Microsoft Most Valuable Professional
     .Net Framework - ASP.Net - Architecture - Development
NOTE: This list of posts will only list the 15 latest posts, to see the rest, select from the Archives located in the menu to the left. The RSS will only list the 10 lastest posts.
Set the culture based on a value stored into the Profile feature.

Category:  ASP.Net 2.0

I have seen several posts on the web about how to get the culture from the Profile feature and set the current Thread’s CurrentUICulture and CurrentCultuer property based on the value saved to the Profile feature. In this post I’m going to give you another solution. As you all know, the WebForm inherits the Page class. The Page class has a protected InitializeCulture method that does nothing by default. This method will only be executed if the culture attribute of the Page directive is set to “auto”. If it’s set to auto, ASP.Net will get the culture information from the user’s browsers, for example in IE you have a Language setting where you can specify one to several different language. ASP.Net will get the language that is added to the top of the language list in IE, and set the current thread culture to that culture that represents the language. If you want to manually set the culture for the current thread, you can override the Page class InitializeCulture method. In this method you can get the culture from the Profile feature and set it on the current thread. The method will be called after the auto detection of culture is set.

 

The following is an example where the current threads culture is set to a culture stored in the Profile feature:

 

public partial class _Default : System.Web.UI.Page

{

    protected override void InitializeCulture()

    {

        if (!String.IsNullOrEmpty(Profile.UICulture))

            Thread.CurrentThread.CurrentUICulture = new CultureInfo(Profile.UICulture);

    }

    ...

}

 

If you want this behavior on all your pages, you can create your own custom page class that inherits the Page class, and where you add the InitializeCulture method. Make sure your WebForms inherits your custom Page class instead of the Page class.

Posted: Tuesday, April 05, 2005 - 14:01 GMT+1    Print     E-mail    Comments (3)
   fredrik.nsquared2.com - 2007