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

前端 未结 2 882
时光说笑
时光说笑 2020-12-29 19:01

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

Also is there

相关标签:
2条回答
  • 2020-12-29 19:27

    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.

    0 讨论(0)
  • 2020-12-29 19:27

    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.

    0 讨论(0)
提交回复
热议问题