Login | Register   
RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX


Internationalize Your ASP.NET Applications (Part 2 of 3)


In this article you'll examine the features that ASP.NET provides to help you build fully localized web applications. In particular you'll see how .NET manages culture information, and how you localize different types of information—including images, content from a database, text such as copyright messages, and small pieces of information like numbers, currencies and dates. This article will give you a good grounding in the more interesting localization features of the platform.

Specifying Cultures
How do you specify a culture unambiguously? When you say "French," or "UK English" or "US English," can you be sure you're talking about the same language/culture as someone else? Thankfully there is an Internet standard called RFC 1766 that specifies codes for cultures. Here are some examples:

  • en-GB—English—United Kingdom
  • en-US—English—United States
  • fr-FR—French - France
  • zh-CN—Chinese - China
You can see that the first part (lower case) specifies the language, and the second part (upper case) specifies a country or region. There are more variants than are shown here, you can get much more information here: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfSystemGlobalizationCultureInfoClassTopic.asp.

Now that you can uniquely specify a culture, it's interesting to know that Windows 2000 introduced the concept of a pair of culture settings; one called the Current Culture and the other is called the Current UI Culture, or the user interface (UI) culture. Although the two settings are generally the same, there are cases where it is useful for them to be different. For example if a user is British and using a UK English application (CurrentUICulture=en-GB) but wanted to see application data displayed in the French format (CurrentCulture=fr-FR) the two settings could be different.

  • CurrentUICulture—specifies the culture for the user interface and is used for resource file lookups.
  • CurrentCulture—specifies the culture for data and information. It is used to format dates, numbers, currencies and sort strings.
To see how these settings work, put these two lines into any ASP.NET page:

<%=System.Threading.Thread.CurrentThread. CurrentUICulture.ToString()%><br /> <%=System.Threading.Thread.CurrentThread. CurrentCulture.ToString()%>

You'll see that the output shows the values of the two culture settings. On my machine I see these:

en-US en-GB

You can change the CurrentCulture setting through the Control Panel, but the CurrentUICulture is set when the operating system is installed. Applications use the CurrentUICulture setting to render the user interface, and the CurrentCulture setting to display data and information (for example, dates, numbers and currencies). When you write code that localizes text, you must consider whether the information should be classed as part of the user interface or as data and use the appropriate setting. On my machine, the user interface is setting US English, displays data in UK English; however, on most machines the two settings will either be the same, or set to very similar cultures.

End-to-end Unicode support is another .NET feature that's of great help to developers. The .NET Framework String classes all use Unicode, ASP.NET uses Unicode, and both SQL Server and Access both support Unicode.

To work with cultures, .NET provides a CultureInfo class that represents a particular culture. For example you can create a culture object representing UK English like this:

CultureInfo culture = new CultureInfo.CreateSpecificCulture("en-GB");

The CultureInfo class supports a variety of methods. For example, DisplayName displays its name in the culture's language; EnglishName gives you the culture name in English; Calendar specifies which calendar is in use; DateTimeFormat holds settings for displaying dates and times; NumberFormat provides options for displaying numbers; and LCID returns the locale/region identifier for the specified culture (1033 for UK). You can set the CurrentCulture and CurrentUICulture objects using a CultureInfo object as follows:

System.Threading.CurrentThread.CurrentCulture = culture; System.Threading.CurrentThread.CurrentUICulture = culture;

In Web applications it's often useful to set culture info in the Application_BeginRequest() method in the global.asax file. That method always runs first when an application begins, which ensures that the your application has already set the culture before any page code executes.

Comment and Contribute








