Finding the current culture settings using the CultureInfo class in .NET C#
December 25, 2016 Leave a comment
Finding the the culture settings – the locale – of a thread is straightforward in .NET:
CultureInfo cultureInfo = Thread.CurrentThread.CurrentCulture;
We extract the current culture from the current thread. The CultureInfo class holds a number of properties to extract information from it. Examples:
string cultureName = cultureInfo.Name; string cultureDisplayName = cultureInfo.DisplayName; string nativeName = cultureInfo.NativeName; string englishName = cultureInfo.EnglishName; string cultureAbbreviation = cultureInfo.TwoLetterISOLanguageName;
As my computer is set to run with Swedish settings I got the following values from top to bottom:
- Swedish (Sweden)
- svenska (Sverige)
- Swedish (Sweden)
Cultures are represented by the following format:
- Neutral culture, “sv” in the above case, is the ISO language name which is tied to the language
- Specific culture, “SE” in the above case, denotes the geographical location of the culture
- The two elements are connected with a hyphen “-“, i.e. “sv-SE” in the above example
Specific culture is the most precise description of the user’s locale. It not only designates the language but the region as well. E.g. Swedish is spoken in Finland as well so the neutral culture “sv” is not enough to locate the user. There’s a specific “sv-FI” format for that. This aspect becomes a lot more important with broadly used languages such as French or English. French spoken in France is different from French spoken in Canada. Therefore we need to use fr-FR and fr-CA for the purpose of formatting and proper localisation.
Besides neutral and specific cultures there’s a third type of culture called invariant culture. This is not tied to any specific culture but is closest to English. It may be tempting to use this culture for other purposes such as date and time comparisons but you’ll most likely get false results in a globalised, culture aware application.
The current culture is used for formatting purposes behind the scenes:
decimal price = 10.43M; string formattedDecimalDefault = price.ToString("C");
I got “10,43 kr” where ‘kr’ denotes the currency in Sweden, i.e. Swedish krona.
You can also set the current culture of the thread:
CultureInfo usCulture = new CultureInfo("en-US"); Thread.CurrentThread.CurrentCulture = usCulture; string formattedPriceUs = price.ToString("C");
This immediately yields “$10.43”.
The invariant culture is denoted by an empty string:
CultureInfo invariantCulture = new CultureInfo(""); Thread.CurrentThread.CurrentCulture = invariantCulture; string formattedPriceUs = price.ToString("C");
Neutral cultures are denoted by the ISO language names, e.g.:
CultureInfo englishCulture = new CultureInfo("en"); Thread.CurrentThread.CurrentCulture = englishCulture; string formattedPriceUs = price.ToString("C");
Which will format the price according to the en-US specific culture.
As you typed “Thread.CurrentThread.” in the editor you may have noticed the CurrentUICulture property. That also returns a CultureInfo object. As the name suggests, it denotes the culture used to show prices, dates, time etc. in the application UI. CurrentCulture and CurrentUICulture will most often be the same, but be aware that they can be different. You might perform calculations in a culture but show the results in another one. Also, you can always manipulate the CurrentCulture of the thread during an application’s lifetime. However, you can only set the current UI culture at the application’s start up.
Read all posts related to Globalisation in .NET here.