Wednesday, March 24, 2010

How to set culture of page

Here I show you how to set the culture of your page through code.

To do that you have to over ride InitializeCulture mthod of page. And Set the UICultuer and Culture properties of page for the specified culture like 'en-US', 'en-GB' etc.

For example

this.UICulture = “en-GB”;
this.Culture = “en-GB”;

This set you page culture to English UK (GB for grate brighten).

Now all you need is list of cultures available in .net to get that we use following code which will retrieve the list of culture available with the .net.

CultureInfo[] cultures = System.Globalization.CultureInfo.GetCultures(CultureTypes.SpecificCultures);
cultures = cultures.OrderBy(c=>c.EnglishName).ToArray();

Now you can bind these values with the drop down list.

foreach(CultureInfo c in cultures)
    ddlCulture.Items.Add(new ListItem(c.EnglishName + " : " + c.NumberFormat.CurrencySymbol, c.Name));
Get the selection from user and set the Session variable or any other way to set the culture of your page.