Wednesday, December 20, 2023

Using CultureInfo for Localization

As the computer programs are worldwide and the world has so many countries and languages and cultures, it is an important topic to understand the .Net Capability to support it.

.Net has a class call CultureInfo available in System.Globalization namespace to use the most of the cultures available in the world.

We can use these cultures for various localization purpose such as currency formatting.

To list all the supported CultureInfo, we can use the following code snippet.

var cultures = System.Globalization.CultureInfo.GetCultures(System.Globalization.CultureTypes.AllCultures);
foreach (var culture in cultures)
{
  Console.WriteLine(culture.DisplayName + "\t"
    + culture.Name + "\t"
    + (12001).ToString("C", culture));
}

Above code uses the culture to convert the number to currency format in particular culture.

For Example, en-US culture will convert 12001 to $12,001.00

We can pick a particular culture with its code

For Example to pick the en-US culture and use it, we can use the below code snippet.

var culture = System.Globalization.CultureInfo.GetCultureInfo("en-US");
Console.WriteLine((12001).ToString("C", culture));

No comments:

Post a Comment

Object Oriented Programming- 2- Inheritance

Inheritance is one of the fundamental attributes of object-oriented programming. It allows you to define a child class...