How to: Set the Culture and UI Culture for ASP.NET Web Page Globalization

yuzhangqi發表於2010-09-16

Globalization is the process of designing and developing applications that function for multiple cultures. Localization is the process of customizing your application for a given culture and locale.

In an ASP.NET Web page, you can set to two culture values, the Culture and UICulture properties. The Culture value determines the results of culture-dependent functions, such as the date, number, and currency formatting, and so on. The UICulture value determines which resources are loaded for the page.

The Culture and UICulture properties are set using Internet-standard strings that identify the language (for example, en for English, es for Spanish, and de for German) and culture (for example, US for the United States, GB for Great Britain, MX for Mexico, and DE for Germany). Some examples are en-US for English/United States, en-GB for English/Great Britain, and es-MX for Spanish/Mexico.

The two culture settings do not have to have the same value. Depending on your application, it might be important to set them separately. An example is a Web auction site. The UICulture property might change for each Web browser, whereas the Culture stays constant. Therefore, prices are always displayed in the same currency and formatting.

The Culture value can be set to specific cultures only, such as en-US or en-GB. This prevents the requirement to identify the correct currency symbol to use for en, where en-US and en-GB have different currency symbols.

Users can set the UI culture and culture in their browsers. For example, in Microsoft Internet Explorer, on the Tools menu, users can click Internet Options, on the General tab, click Language, and then set their language preference. If the enableClientBasedCulture attribute of the element in the Web.config file is set to true, ASP.NET can set the UI culture and culture for a Web page automatically, based on the values that are sent by a browser.

It is not a best practice to rely exclusively on browser settings to determine the UI culture for a page. Users frequently use browsers that are not set to their preferences (for example, in an Internet cafe). You should provide a method for users to explicitly choose a language or language and culture (CultureInfo name) for the page.

To set the culture and UI culture for an ASP.NET Web page declaratively

To set the UI culture and culture for all pages, add a globalization section to the Web.config file, and then set the uiculture and culture attributes, as shown in the following example:

To set the UI culture and culture for an individual page, set the Culture and UICulture attributes of the directive, as shown in the following example:

To have ASP.NET set the UI culture and culture to the first language that is specified in the current browser settings, set UICulture and Culture to auto. Alternatively, you can set this value to auto:culture_info_name, where culture_info_name is a culture name. For a list of culture names, see CultureInfo. You can make this setting either in the directive or Web.config file.

To set the culture and UI culture for an ASP.NET Web page programmatically

1. Override the InitializeCulture method for the page.

2. In the overridden method, determine which language and culture to set the page to.

The InitializeCulture method is called very early in the page life cycle, before controls are created or properties are set for the page. Therefore, to read values that are passed to the page from controls, you must get them directly from the request using the collection.

3. Set the UI culture and culture in one of the following ways:

  • Set the Culture and UICulture properties of the page to the language and culture string (for example, en-US). These properties are internal to the page, and can only be used in a page.

  • Set the CurrentUICulture and CurrentCulture properties of the current thread to the UI culture and culture, respectively. The CurrentUICulture property takes a language and culture information string. To set the CurrentCulture property, you create an instance of the CultureInfo class and call its CreateSpecificCulture method.

    The following code example shows an ASP.NET Web page that lets users select their preferred language from a drop-down list. The page imports two namespaces to make it more convenient to work with threading and globalization classes.

    "C#" UICulture="auto" %>
    "System.Threading" %>
    "System.Globalization" %>



    "form1" runat="server">

    "ListBox1" runat="server">
    "en-US"
    Selected="True">English

    "es-MX">Español
    "de-DE">Deutsch





  • [@more@]

    來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/13651903/viewspace-1038440/,如需轉載,請註明出處,否則將追究法律責任。

    相關文章