Home   |   QuickStart Welcome   |   ASP.NET   |   Web Services   |   How Do I...?   
  |   I want my samples in...      

ASP.NET 2.0 Quickstart Tutorials

Culture-sensitive Formatting

Setting a Preferred Language in Internet Explorer

HTTP allows browsers to send a list of preferred languages to the web server using the Accept-Language HTTP request header field. In Internet Explorer the list can be configured like this:
  • Select the "Tools | Internet Options" command
  • Select the "Languages" button.
  • In the dialog that is shown, click on the "Add" button to add a new language and select a new culture. Press "OK".
  • Ensure your preferred language is at the top of the list. Select that language and click "Move Up". Click "OK" to exit the dialog.
  • Refresh the page using F5.
Initially the list is preconfigured to the language of the Internet Explorer user interface, which makes sense as typically users want to see web content in the language of the computer they are working on. Other browsers have similar features to configure this header field.

Auto-detecting the browser language for culture-sensitive formatting New in 2.0

While not originally intended for the purpose this language list can be used to determine which culture the user most likely prefers for formatting of numbers, dates and times. ASP.NET v2.0 now provides functionality to easily detect the first language in the list and use this for formatting:
<%@ Page Culture="auto:en-US" %>
If the first language in the list matches a name of a specific culture supported by the .NET Framework ASP.NET will set the CurrentCulture of the running thread to this culture. This happens early in the page life cycle so that all subsequent culture-sensitive formatting will be done with this culture.

In case the first language in the list matches the name of a neutral culture supported by the .NET Framework ASP.NET will try to use the function CultureInfo.CreateSpecificCulture() to create a specific culture for the purpose of formatting. This will map for example fr into the culture for French (France). This determination will of course be inappropriate for somebody in the French-speaking part of Canada which is why an application should provide the means to configure both culture and language - more on this personalization in the localization section.

In case the first language in the list isn't a culture supported by the .NET Framework ASP.NET will fall back to the culture specified after the colon in the Culture declaration. This would be en-US - English (United States) in the example above.

This auto-detection functionality can be used in all places where Culture can be specified declaratively. Therefore also in the globalization section of web.config. The auto-detection works the same for UICulture which is used for localization and is explained later.

The following sample demonstrates a Currency Exchange Calculator that reacts to different language settings in the browser (see above for information how to change this). It will display the date in the format appropriate for the first language in the list and as well use the right decimal separator for the exchange values. Further this decimal separator can be used for the entry of the value to be converted.
Culture-sensitive date and number formatting (VB)
Run Sample View Source
In order to view text in some East Asian languages and some languages with complex scripts, you'll potentially need to install the appropriate font for Internet Explorer. Refer to Internet Explorer Help Topics for more information (see in the index under "foreign languages used in Web pages"). Alternatively you can install additional fonts from the Regional and Language Options of the Control Panel.