How to CultureInfo in C#

In C#, the CultureInfo class is part of the System.Globalization namespace and provides information about specific cultures or regions. It allows you to work with culture-specific data, such as language, date and time formats, number formats, and currency symbols. CultureInfo is especially useful when developing internationalized applications that need to handle diverse cultural settings. Let's explore the details of CultureInfo along with examples:

Retrieving Current Culture

The CultureInfo.CurrentCulture property returns the CultureInfo object representing the culture settings of the current executing thread. It provides access to various properties and methods to access culture-specific information.

CultureInfo currentCulture = CultureInfo.CurrentCulture; string currentLanguage = currentCulture.Name; Console.WriteLine($"Current Language: {currentLanguage}");

Retrieving Specific Culture

You can create a CultureInfo object for a specific culture or region by passing the culture identifier or culture name to the CultureInfo constructor. This allows you to work with culture-specific settings for different regions.

CultureInfo germanCulture = new CultureInfo("de-DE"); DateTimeFormatInfo germanDateTimeFormat = germanCulture.DateTimeFormat; string germanShortDatePattern = germanDateTimeFormat.ShortDatePattern; Console.WriteLine($"German Short Date Pattern: {germanShortDatePattern}");

Formatting and Parsing

CultureInfo provides methods for formatting and parsing data based on specific cultures. For example, you can use the DateTime.ToString method with a specific CultureInfo to format a DateTime value as a string in a culture-specific way.

DateTime currentDate = DateTime.Now; string formattedDate = currentDate.ToString("d", CultureInfo.GetCultureInfo("fr-FR")); Console.WriteLine($"Formatted Date: {formattedDate}");

Number and Currency Formatting

CultureInfo enables you to format numbers and currencies based on specific cultures. You can use the NumberFormat and CurrencyFormat properties to access the specific number and currency formatting settings for a culture.

double number = 12345.6789; string formattedNumber = number.ToString("N", CultureInfo.GetCultureInfo("en-US")); Console.WriteLine($"Formatted Number: {formattedNumber}"); decimal currency = 1000.50m; string formattedCurrency = currency.ToString("C", CultureInfo.GetCultureInfo("de-DE")); Console.WriteLine($"Formatted Currency: {formattedCurrency}");

The following C# program shows how to get all culture names in the .NET Framework. To use CultureInfo class you need to include System.Globalization namespace in your project.

Full Source C#
using System; using System.Threading; using System.Globalization; using System.Windows.Forms; namespace WindowsFormsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { //MessageBox.Show(Thread.CurrentThread.CurrentCulture.Name); foreach (CultureInfo ci in CultureInfo.GetCultures(CultureTypes.AllCultures)) { MessageBox.Show(ci.EnglishName+ " - " + ci.Name); } } } }

Conclusion

The CultureInfo class in C# plays a vital role in handling culture-specific information and formatting data accordingly. By utilizing CultureInfo, you can develop applications that adapt to different cultures and provide a seamless experience for users across diverse regions.