The following Table lists all the .NET supported Cultures
The Abbreviation and several DateTime string formats are displayed for each Culture.
An IComparer is used to sort the Cultures alphabetically by EnglishName. See Source Code provided below.
You can change the Culture at the Page, Application and Thread level. The following details three techniques with code samples provided.
Option 1: Page level
The Culture used by the individual Page can be set via the
Cultureproperty.<%@ Page Culture="en-GB" Language="C#" %>Option 2: Application wide (web.Config)
The Culture can be set Application wide by adding the
<globalization>node to theweb.Configfile. The<globalization>node must be placed inside<system.web>.<system.web><globalization culture="en-GB"/>Option 3: Thread level
The Culture of the current Thread can be changed programatically be setting the
System.Threading.Thread.CurrentThread.CurrentCultureproperty.The following example demonstrates how to get the user-agent(browsers) preferred
UserLanguageproperty on each Request and set the ThreadsCurrentCultureduring theApplication_BeginRequestEvent.protected void Application_BeginRequest(Object sender, EventArgs e){CultureInfo requestCulture;try{// Request top UserLanguage from user agent and create CultureInfo object.requestCulture = CultureInfo.CreateSpecificCulture(Request.UserLanguages[0]);}catch{// Return server Culture if none available in HttpHeaders.requestCulture = CultureInfo.CurrentCulture;}System.Threading.Thread.CurrentThread.CurrentCulture = requestCulture;}
<%@ Page Language="C#" %>
<%@ Import Namespace="System.Globalization" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<title>.NET Framework Cultures</title>
<style type="text/css">
tr { cursor: pointer; }
.on { background-color: #E4EEF8; }
.selected { background-color: #E4EEF8; }
.off { background-color: white; }
</style>
<script runat="server">
protected void Page_Load(object sender, EventArgs e)
{
// Get server DateTime.
DateTime date = DateTime.Now;
// Get a list of all Cultures
CultureInfo[] cultures = CultureInfo.GetCultures(CultureTypes.SpecificCultures);
// Add the Cultures to an ArrayList so we can sort them Alphabetically by name.
ArrayList cultureList = new ArrayList(cultures.Length);
foreach (CultureInfo culture in cultures)
{
cultureList.Add(culture);
}
// Sort Cultures
cultureList.Sort(new StringComparer());
// Create a table and set a few display properties.
Table table = new Table();
table.Width = Unit.Percentage(100);
table.CellPadding = 0;
table.CellSpacing = 0;
table.CssClass = "datatable";
// Create a new header row and a bunch of header cells.
TableRow row = new TableRow();
TableHeaderCell th1 = new TableHeaderCell();
TableHeaderCell th2 = new TableHeaderCell();
TableHeaderCell th3 = new TableHeaderCell();
TableHeaderCell th4 = new TableHeaderCell();
TableHeaderCell th5 = new TableHeaderCell();
TableHeaderCell th6 = new TableHeaderCell();
TableHeaderCell th7 = new TableHeaderCell();
TableHeaderCell th8 = new TableHeaderCell();
TableHeaderCell th9 = new TableHeaderCell();
TableHeaderCell th10 = new TableHeaderCell();
TableHeaderCell th11 = new TableHeaderCell();
// Set the value of each header cell.
th1.Text = " ";
th2.Text = "English Name";
th3.Text = "Abbreviation";
th4.Text = "ShortDate Format";
th5.Text = "ShortDate Example";
th6.Text = "LongDate Format";
th7.Text = "LongDate Example";
th8.Text = "ShortTime Format";
th9.Text = "ShortTime Example";
th10.Text = "LongTime Format";
th11.Text = "LongTime Example";
// Add each cell to the row.
row.Cells.Add(th1);
row.Cells.Add(th2);
row.Cells.Add(th3);
row.Cells.Add(th4);
row.Cells.Add(th5);
row.Cells.Add(th6);
row.Cells.Add(th7);
row.Cells.Add(th8);
row.Cells.Add(th9);
row.Cells.Add(th10);
row.Cells.Add(th11);
// Add header row to table.
table.Rows.Add(row);
// Loop through the cells and set them nowrap.
foreach (TableCell cell in row.Cells)
{
cell.Wrap = false;
}
int count = 1;
foreach (CultureInfo culture in cultureList)
{
// Set the CurrentThread to the culture.
System.Threading.Thread.CurrentThread.CurrentCulture = culture;
// Create a new row and a bunch of new cells.
TableRow tr = new TableRow();
tr.Attributes.Add("class", "off");
TableCell td1 = new TableCell();
TableCell td2 = new TableCell();
TableCell td3 = new TableCell();
TableCell td4 = new TableCell();
TableCell td5 = new TableCell();
TableCell td6 = new TableCell();
TableCell td7 = new TableCell();
TableCell td8 = new TableCell();
TableCell td9 = new TableCell();
TableCell td10 = new TableCell();
TableCell td11 = new TableCell();
// Add the values to the cells.
td1.Text = count.ToString() + ".";
td2.Text = culture.EnglishName;
td3.Text = culture.Name;
td4.Text = culture.DateTimeFormat.ShortDatePattern;
td5.Text = date.ToShortDateString();
td6.Text = culture.DateTimeFormat.LongDatePattern;
td7.Text = date.ToLongDateString();
td8.Text = culture.DateTimeFormat.ShortTimePattern;
td9.Text = date.ToShortTimeString();
td10.Text = culture.DateTimeFormat.LongTimePattern;
td11.Text = date.ToLongTimeString();
// Add cells to the row.
tr.Cells.Add(td1);
tr.Cells.Add(td2);
tr.Cells.Add(td3);
tr.Cells.Add(td4);
tr.Cells.Add(td5);
tr.Cells.Add(td6);
tr.Cells.Add(td7);
tr.Cells.Add(td8);
tr.Cells.Add(td9);
tr.Cells.Add(td10);
tr.Cells.Add(td11);
// Add mouseover, mouseout and onclick effects.
tr.Attributes.Add("onmouseover", "toggleRow(this, true, false);");
tr.Attributes.Add("onmouseout", "toggleRow(this, false, false);");
tr.Attributes.Add("onclick", "toggleRow(this, true, true);");
// Add row to table.
table.Rows.Add(tr);
// Loop through the cells and set them nowrap.
foreach (TableCell cell in tr.Cells)
{
cell.Wrap = false;
}
count++;
}
// Add table to PlaceHolder.
this.PlaceHolder1.Controls.Add(table);
}
class StringComparer : IComparer
{
public int Compare(object o1, object o2)
{
// Simple compare between the EnglishName property.
// This will organize alphabetically.
return string.Compare(((CultureInfo)o1).EnglishName, ((CultureInfo)o2).EnglishName);
}
}
</script>
<script type="text/javascript">
function toggleRow(e, highlight, clicked)
{
var c = e.className;
if(highlight && clicked)
{
e.className = (c == "off" || c == "on") ? "selected" : "off";
return;
}
else if (c == "selected") { return; }
else if (highlight && c == "off") { e.className = "on"; }
else { e.className = "off"; }
return;
};
</script>
</head>
<body>
<form id="form1" method="post" runat="server">
<h4>Example</h4>
<asp:placeholder id="PlaceHolder1" runat="server"></asp:placeholder>
</form>
</body>
</html>