Ian Ringrose
Ian Ringrose

Reputation: 51927

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.

Upvotes: 47

Views: 27466

Answers (2)

Henk Holterman
Henk Holterman

Reputation: 273701

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.

Upvotes: 18

Martin Liversage
Martin Liversage

Reputation: 106926

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.

Upvotes: 45

Related Questions