Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When to use CultureInfo.GetCultureInfo(String) or CultureInfo.CreateSpecificCulture(String)

When should I call CultureInfo.CreateSpecificCulture(String) rather then CultureInfo.GetCultureInfo(String). The MSDN documentation is not very clear.

Also is there a way to cheaper check if the name of a culture is valid?

I think if you pass “en” rather then “en-GB” to CultureInfo.CreateSpecificCulture(String) you will get an error, but that CultureInfo.GetCultureInfo(String) does not mind. E.g. CultureInfo.GetCultureInfo(String) can cope if you only pass an language. However I am still unclear on this.

like image 813
Ian Ringrose Avatar asked Jun 12 '09 13:06

Ian Ringrose


People also ask

What is difference between CurrentCulture and CurrentUICulture?

CurrentCulture is the . NET representation of the default user locale of the system. This controls default number and date formatting and the like. CurrentUICulture refers to the default user interface language, a setting introduced in Windows 2000.

What is the use of CultureInfo InvariantCulture?

The InvariantCulture property can be used to persist data in a culture-independent format. This provides a known format that does not change and that can be used to serialize and deserialize data across cultures.

What is the use of CultureInfo in C#?

The CultureInfo class provides culture-specific information, such as the language, sublanguage, country/region, calendar, and conventions associated with a particular culture. This class also provides access to culture-specific instances of the DateTimeFormatInfo, NumberFormatInfo, CompareInfo, and TextInfo objects.

What is CultureInfo CurrentCulture?

The CultureInfo. CurrentCulture property is a per-thread setting; that is, each thread can have its own culture. You get the culture of the current thread by retrieving the value of the CultureInfo. CurrentCulture property, as the following example illustrates. C# Copy.


2 Answers

Cultures are grouped into three sets: the invariant culture, the neutral cultures, and the specific cultures. The culture en is a neutral culture while the culture en-US is a specific culture.

GetCultureInfo will give you whatever culture you requested so if you request a neutral culture you also get a neutral culture like en.

CreateSpecificCulture will create a specific culture from a neutral culture so if you call CreateSpecificCulture("en") the CultureInfo returned is for the en-US culture. I am not sure how neutral cultures are mapped to specific cultures but there must some table inside the BCL or Windows that contains those mappings and decides that it is the en-US and not en-GB that is returned. Specifying a specific culture as the argument to CreateSpecificCulture will give you that specific CultureInfo just as GetCultureInfo does.

But there is a somewhat surprising feature of the specific culture created:

If the culture identifier of the specific culture returned by this method matches the culture identifier of the current Windows culture, this method creates a CultureInfo object that uses the Windows culture overrides. The overrides include user settings for the properties of the DateTimeFormatInfo object returned by the DateTimeFormat property and the NumberFormatInfo object returned by the NumberFormat property.

What this means is that if the specific culture returned by CreateSpecificCulture matches the culture selected by the user in Region and Language control panel in Windows then any user customizations to that culture is included in the CultureInfo returned. E.g. the user may change the long date pattern or the decimal separator used in numbers. Another way to think about this is that when CreateSpecificCulture returns a culture that matches the name of CurrentCulture it will actually return this culture including any user customizations.

As far as I can tell GetCultureInfo does not have this property and will always return a unmodified CultureInfo.

And to check if a culture is valid I would use GetCultureInfo.

like image 136
Martin Liversage Avatar answered Oct 03 '22 04:10

Martin Liversage


It depends a bit on what you need the culture for. The short names ("en", "fr" etc) are used for neutral cultures, sufficient for language specific resource management. But for numerical and date formatting you need a specific culture, like "en-GB".

And CultureInfo.CreateSpecificCulture("en"); works fine over here. It is especially intended to get 'a' specific culture for a neutral one.

like image 23
Henk Holterman Avatar answered Oct 03 '22 05:10

Henk Holterman