Setting the Culture and UI Culture for Web Forms Globalization
By specifying a culture it is possible to use a set of common preferences for information like strings and date and number formats that correspond to users' cultural conventions. A Web Forms page has two culture values, Culture and UICulture. The Culture value determines or influences results of culture-dependent functions, such as displaying the date. The UICulture value determines how the resources are loaded for the form and is used for culture-specific lookup of resource data. The only purpose of the UICulture value is to indicate the language the resources should load, that is, determine which language the UI strings should use. The Culture value determines everything else — date formatting, number formatting, and so on.
The two culture settings do not need to have the same value. Depending on your application, it may be important to set them separately. An example is a Web auction site. The UI culture changes for each Web browser, but the culture stays constant, so prices are always displayed in the same currency and formatting.
The Culture value can only be set to specific cultures such as en-US or en-GB. This prevents the problem of identifying the correct currency symbol to use for "en", where en-US and en-GB have different currency symbols.
To set the Culture and UI Culture values
- In the Globalization section of the Web.config file, add lines similar to the code below. For more information on Web.config files, see Editing the Web.config File for Web Forms Globalization.
- In HTML view, add the Culture and UICulture values to the page directive.
<%@ Page UICulture="de" Culture="en-US"...%>
- In the Code Editor, add code to set the CurrentCulture and CurrentUICulture properties.
The following example sets both the CurrentCulture and the CurrentUICulture properties to the culture specified in the browser's Accept-Language header. Note that the CurrentCulture property is set using the CreateSpecificCulture method, since this property must be set to a specific culture and not a neutral culture.
' Visual Basic ' Import these namespaces at the beginning of the code module Imports System.Globalization Imports System.Threading ' Set the culture and UI culture to the browser's accept language Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(Request.UserLanguages(0)) Thread.CurrentThread.CurrentUICulture = New CultureInfo(Request.UserLanguages(0)) // C# // Import these namespaces at the beginning of the code module. using System.Globalization; using System.Threading; // Set the culture and UI culture to the browser's accept language Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(Request.UserLanguages); Thread.CurrentThread.CurrentUICulture = new CultureInfo(Request.UserLanguages);