Document directory
- Set the culture and UI culture of ASP. NET web pages in declarative Mode
- Set the culture and UI culture of ASP. NET web pages programmatically
Updated: February 1, November 2007
On the ASP. NET webpage, you can set two regional values, namely, the Culture and UICulture attributes. The result of the Culture-related function, such as the date, number, and currency format. The UICulture value determines which resources are loaded on the page.
Note: |
The Culture and UICulture attributes are Internet standard strings (for example, en stands for English, es stands for Spanish, de stands for German) and Internet standard strings (for example, US stands for the United States, GB stands for the United Kingdom, MX stands for Mexico, DE stands for Germany. Some examples include: en-US stands for English/US, en-GB stands for English/UK, and es-MX stands for Spanish/Mexico. For more information, see CultureInfo. |
These two regional settings do not need to have the same value. Depending on your application, it may be important to set them separately. The Web auction site is such an example. For each Web browser, the UICulture attribute may change, while the Culture attribute remains unchanged. Therefore, prices are always displayed in the same currency and format.
The Culture value can only be set to a specific Culture, such as en-US or en-GB. In this way, you do not need to identify the correct currency symbol used for en (for this string, en-US and en-GB have different currency symbols.
Users can set the culture and UI culture in their browsers. For example, on the "Tools" menu of Microsoft Internet Explorer, you can click "Internet Options", "general" tab, and "language", and set their language preferences. If the enableClientBasedCulture attribute of the globalization element in the Web. config file is set to true, ASP. NET can automatically set the webpage culture and UI culture based on the value sent by the browser.
It is not the best practice to determine the UI culture of a webpage based on browser settings. Browsers used by users are generally not set as their preferences (for example, in an Internet cafe ). You should provide users with methods to explicitly select the language or language and culture (CultureInfo name) of the page.
Set the culture and UI culture of ASP. NET web pages in declarative Mode
To set the culture and UI culture of all pages, add a globalization section to the Web. config file and set the uiculture and culture attributes, as shown in the following example:
Copy code<globalization uiCulture="es" culture="es-MX" />
To set the Culture and UI Culture of a single Page, set the Culture and UICulture attributes of the @ Page Command, as shown in the following example:
Copy code<%@ Page UICulture="es" Culture="es-MX" %>
To enable ASP. NET to set the Culture and UI Culture to the first language specified in the current browser settings, set UICulture and Culture to auto. You can also set this value to auto: culture_info_name, where culture_info_name is the regional name. For a list of regional names, see CultureInfo. You can perform this setting in the @ Page command or the Web. config file.
Set the culture and UI culture of ASP. NET web pages programmatically
Override the InitializeCulture method of the page.
In the override method, determine the language and culture to set for the page.
Note: |
The InitializeCulture method is called early in the lifecycle of a page. At this time, no controls have been created for the page and no properties have been set for the page. Therefore, to read the values passed from the control to the page, you must use the Form set to directly obtain these values from the request. |
You can set the culture and UI culture in one of the following ways:
Set the Culture and UICulture attributes of the page to a language and a regional string (such as en-US ). These two properties are the internal properties of the page and can only be used in the page.
Set the CurrentUICulture and CurrentCulture attributes of the current thread to the UI culture and culture respectively. The CurrentUICulture attribute uses a linguistic and regional information string. To set the CurrentCulture attribute, create an instance of the CultureInfo class and call its CreateSpecificCulture method.
The following code example shows an ASP. NET webpage that allows users to select their preferred language from the drop-down list. This page imports two namespaces, making it easier to use thread processing classes and global classes.
Copy code in Visual Basic<%@ Page Language="VB" uiculture="auto" %>
<%@ Import Namespace="System.Threading" %>
<%@ Import Namespace="System.Globalization" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML
1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<script runat="server">
Protected Overrides Sub InitializeCulture()
If Request.Form("ListBox1") IsNot Nothing Then
Dim selectedLanguage As String = _
Request.Form("ListBox1")
UICulture = Request.Form("ListBox1")
Culture = Request.Form("ListBox1")
Thread.CurrentThread.CurrentCulture = _
CultureInfo.CreateSpecificCulture(selectedLanguage)
Thread.CurrentThread.CurrentUICulture = New _
CultureInfo(selectedLanguage)
End If
MyBase.InitializeCulture()
End Sub
</script>
<body>
<form id="form1" runat="server">
<div>
<asp:ListBox ID="ListBox1" runat="server">
<asp:ListItem Value="en-US"
Selected="True">English</asp:ListItem>
<asp:ListItem Value="es-MX">Español</asp:ListItem>
<asp:ListItem Value="de-DE">Deutsch</asp:ListItem>
</asp:ListBox><br />
<asp:Button ID="Button1" runat="server"
Text="Set Language"
meta:resourcekey="Button1" />
<br />
<asp:Label ID="Label1" runat="server"
Text=""
meta:resourcekey="Label1" />
</div>
</form>
</body>
C # copy code<%@ Page Language="C#" uiculture="auto" %>
<%@ Import Namespace="System.Threading" %>
<%@ Import Namespace="System.Globalization" %>
<script runat="server">
protected override void InitializeCulture()
{
if (Request.Form["ListBox1"] != null)
{
String selectedLanguage = Request.Form["ListBox1"];
UICulture = selectedLanguage ;
Culture = selectedLanguage ;
Thread.CurrentThread.CurrentCulture =
CultureInfo.CreateSpecificCulture(selectedLanguage);
Thread.CurrentThread.CurrentUICulture = new
CultureInfo(selectedLanguage);
}
base.InitializeCulture();
}
</script>
<body>
<form id="form1" runat="server">
<div>
<asp:ListBox ID="ListBox1" runat="server">
<asp:ListItem Value="en-US"
Selected="True">English</asp:ListItem>
<asp:ListItem Value="es-MX">Español</asp:ListItem>
<asp:ListItem Value="de-DE">Deutsch</asp:ListItem>
</asp:ListBox><br />
<asp:Button ID="Button1" runat="server"
Text="Set Language"
meta:resourcekey="Button1" />
<br />
<asp:Label ID="Label1" runat="server"
Text=""
meta:resourcekey="Label1" />
</div>
</form>
</body>
See
Other resources
ASP. NET globalization and Localization
From: http://msdn.microsoft.com/zh-cn/library/bz9tc508.aspx