Browse DevX
Sign up for e-mail newsletters from DevX


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

Learn to localize dates, numbers, and currencies and get a wrap-up of the entire process for ASP.NET internationalization.




Building the Right Environment to Support AI, Machine Learning and Deep Learning

In this article you'll finish examining the features that ASP.NET provides to help you build fully localized Web applications. In particular you'll see how .NET manages dates, numbers, and currencies, and how can localize them. Finally, you'll see a brief wrap-up of localization that consolidates the information in Part 1 and Part 2.

Localizing Dates
As long as you store dates in a DateTime object, they are pretty easy to localize, which is good because, to give you an idea of the problem that needs solving, just look at how differently French and UK English dates are usually displayed:

  • UK: 20 August 2001
  • French : lundi 20 août 2001
It's easy to see that the language for long-format dates is different, but short-format dates are different as well. For example, the order of the month and day differs between UK and US dates. In the UK August 20, 2001 is 20/8/01, but in the States it's 8/20/01. Clearly, such differences are critical to communicating information correctly.

In ASP.NET, to display a date in a different culture, you use the Thread object to change the current thread culture, and then use one of the DateTime class display methods to display the date. For example, the following fragment displays a date in long format determined by the French (fr-FR) culture setting.

CultureInfo culture = CultureInfo.CreateSpecificCulture("fr-FR"); System.Threading.Thread.CurrentThread.CurrentCulture = culture; string date = DateTime.Now.ToLongDateString(); Response.Write(date);

Localizing Numbers and Currencies
Currencies and numbers vary in much the same way as dates—and .NET handles them just as neatly. As with dates, after you set the culture for the current thread, the framework handles formatting. For example, the French/France locale uses commas in numbers where English cultures generally use periods. To make the change, you first set the culture and then display the formatted string. For example, to display the value 5.5 in French as "5,5", you can write:

CultureInfo culture = CultureInfo.CreateSpecificCulture("fr-FR"); System.Threading.Thread.CurrentThread.CurrentCulture = culture; Label1.Text = (5.5).ToString();

Currencies work similarly, but you need to specify explicitly that you want a value displayed as a currency. For example, to display the Japanese price ¥100 (100 Yen), you can write:

CultureInfo culture = CultureInfo.CreateSpecificCulture("ja-JP"); System.Threading.Thread.CurrentThread.CurrentCulture = culture; Label1.Text = String.Format("{0:c}",100);

The preceding code uses the String format specifier "c", which displays the value as a currency using the thread's culture setting.

Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date