CodeBetter.Com
CodeBetter.Com
RSS 2.0 via Feedburner
           Do you Twitter? Follow us @CodeBetter

Peter's Gekko

public Blog MyNotepad : Imho { }

How to set the UI culture of an ASP.NET page from code

The globalization of an asp.net web site is not that difficult. When rendering a page the language preferences of the visiting browser are used. When the resources are set up correctly the translations are read from the corresponding resource. The browsers language preference is also used by the code behind. Also the result of this snippet

Label10.Text = DateTime.Now.ToLongDateString();

depends on the browser settings.

But you can also set the language in which to render the page from code. How to do this can be found on MSDN but (as usual..) that example is not clear because it (again..) tries to demonstrate several things at the same time and doing that it skips a major point.

To set the culture from code you have to override the InitializeCulture method of the (base) Page. In my snippet the language is set by an (optional) parameter in the url.

protected override void InitializeCulture()

{

    const string nlCulture = "nl-NL";

    const string enCulture = "en-GB";

    string rqCulture = Request.Params["nl"];

 

    if (!string.IsNullOrEmpty(rqCulture))

    {

        switch (rqCulture)

        {

            case "0":

                UICulture = enCulture;

                Culture = enCulture;

                break;

            case "1":

                UICulture = nlCulture;

                Culture = nlCulture;

                break;

        }

    }

    base.InitializeCulture();

}

 

When a "1" is passed the page is rendered in Dutch, using the Dutch resource files. When a "0" is passed the page is rendered in British.

You have to set two properties of the page: UICulture and Culture. To what kind of a value is a mystery in the MSDN example. Over there it is set to the selected value in a listbox. What kind of value that it is beyond the scope of the example. It was even harder to guess because reading the property in the debugger (it is hard to write a test on a live HTTP Context) displays long descriptions, not the short en-GB kind of thing. For an overview of all cultures available check the documentation on the CultureInfo class. In case you use any other value than the ones listed there your code will hit an exception.

Another small puzzle is why you have to set two properties. The UICulture is used to select the right resource file. The running thread, which runs the code behind also has a culture, this is the other Culture property. In case you would not set this property the snippet with the ToLongDateTimeString would still lead to a string in the culture of the browsers preferences.

That's all there is to it.



Comments

pvanooijen said:

Thanks, that's another way. But it assumes every user wants to use the same culture.

Besides that, I have no problem with a base class.

# February 11, 2008 5:28 AM

Leave a Comment

(required)  
(optional)
(required)  

Enter the numbers above:
Add
Check out Devlicio.us!