Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between CultureInfo.CreateSpecificCulture() and the constructor of the class?

The class CultureInfo provides two way of creation:

  • via a factory method CreateSpecificCulture(string).
  • via a constructor with a string argument

The MSDN documentation does slightly differ for the two, mentioning some "Windows culture" for the constructor. But does that really matter?

Should I prefer one of the two over the other?

Note: I am using .NET version 3.5 if that matters, and I want to use it like this:

Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(culture); Thread.CurrentThread.CurrentUICulture = new CultureInfo(culture); 

as described in this answer.

like image 276
Marcel Avatar asked Aug 30 '12 09:08

Marcel


People also ask

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.

What is CultureInfo InvariantCulture in C#?

The CultureInfo. InvariantCulture property is used if you are formatting or parsing a string that should be parseable by a piece of software independent of the user's local settings. The default value is CultureInfo. InstalledUICulture so the default CultureInfo is depending on the executing OS's settings.

How do I declare CultureInfo in VB net?

How to CultureInfo in VB.NET. The CultureInfo class specifies a unique name for each culture, based on RFC 4646. The name is a combination of an ISO 639 two-letter lowercase culture code associated with a language and an ISO 3166 two-letter uppercase subculture code associated with a country or region.


2 Answers

The factory method has an fallback when it fails to create the culture info.

So if you use a specific culture like 'en-XX', the culture info instance can't be created, an exception will throw and a retry with the neutral culture 'en' will succeed.

Below the source of the factory method

public static CultureInfo CreateSpecificCulture(string name) {     CultureInfo info;     try     {         info = new CultureInfo(name);     }     catch (ArgumentException)     {         info = null;         for (int i = 0; i < name.Length; i++)         {             if ('-' == name[i])             {                 try                 {                     info = new CultureInfo(name.Substring(0, i));                     break;                 }                 catch (ArgumentException)                 {                     throw;                 }             }         }         if (info == null)         {             throw;         }     }     if (!info.IsNeutralCulture)     {         return info;     }     return new CultureInfo(info.m_cultureData.SSPECIFICCULTURE); } 

So the I prefer the factory method.

like image 102
Erwin Avatar answered Oct 14 '22 00:10

Erwin


This thread has alredy been answered but I came across a unique finding for CreateSpecificCulture API which might not be so obvious at times. So I considered this thread to be an apt place for my findings. I spent a few days on this so just thought of sharing my experience if it can save few hours or days for others as well.

While using the API when you pass it only the culture name like pt (for portuguese) or de (for German) this API returns a specific culture corresponding to the locale which is termed as default locale for that culture. Now this locale might not be so obvious as it sounds where I got stuck. For German, de-DE looks obvious which is German spoken in Germany. For Italian, it-IT looks obvious which is Italian spoken in Italy.

Likewise pt-PT looks obvious for Portuguese spoken in Portugal. Unfortunately this is not the case. Based on not sure what exact reason (may be population, country of origin, national language etc.) there is a global standardization based on which the default locale for a given culture is decided when you try to create a specific culture from a culture id (pt in this case). Microsoft has documented the entire list at following link :

http://msdn.microsoft.com/en-us/goglobal/bb896001.aspx

If you want to know which is the default country locale for a given culture or language just match the last column (Language Name Abbreviation) code in the above link.

For portuguese, the language name abbrevitaion of invariant culture "Portuguese" matches with "Portuguese (Brazil)" which is PTB. Portuguese(Portugal) has a different code PTG. So in this case Portuguese (Brazil) is the default locale for portuguese language.

If your application logic or requirements are in any way relying on this behavior of this API you got to be cautious. This behavior becomes more important in web based applications as all browsers in the market also follow these guidelines and send appropriate information in the http request header when you are looking at a localised version of a multi lingual website.

I'm still looking for the reason though which is THE factor behind setting a specific country as default locale for any culture which doesn't sound so obvious in case of Portuguese. Any information or comments are welcome.

like image 26
RBT Avatar answered Oct 14 '22 00:10

RBT