Can i create a new CultureInfo from a two-character language code?
Asked Answered
F

2

7

I need to format a number, based on a culture, but all I have is the Accept-Language from the HTTP request, which is a two-letter code like fr (for French)

Is this enough to create a CultureInfo that can handle numbers?

Forfeit answered 30/1, 2017 at 15:26 Comment(1)
What happened when you tried it?Hydroquinone
H
4

Yes you can. The following code:

var ci = new CultureInfo("fr");
Console.WriteLine(13.45.ToString(ci));

outputs "13,45" which is using the french decimal separator in the number.

The CultureInfo documentation says:

A neutral culture is specified by only the two-letter lowercase language code. For example, fr specifies the neutral culture for French, and de specifies the neutral culture for German.

Hydroquinone answered 30/1, 2017 at 15:34 Comment(3)
Note that ci.CultureTypes will have the flag NeutralCultures in that case. Equivalently, ci.IsNeutralCulture gives true. That is not really recommended. It is better to use something like new CultureInfo("fr-CA") where you give both a language and a country, corresponding to flag SpecificCultures.Scintillator
@JeppeStigNielsen: Would you recommend just picking an arbitrary country rather than doing the above then? What are the drawbacks of it being a NeutralCulture compared to choosing a country for it?Hydroquinone
var ci = CultureInfo.CreateSpecificCulture("fr") picks a specific culture for you. In this example it picks "fr-FR", that is French (France). I am no expert here, though.Scintillator
S
8

This is a comment to illustrate the difference between neutral cultures and specific cultures:

var language = "fr";

var c1 = new CultureInfo(language);
Console.WriteLine(c1.EnglishName);              // "French"
Console.WriteLine(c1.IsNeutralCulture);         // "True"
Console.WriteLine((3.14m).ToString("C", c1));   // "3,14 €"

var c2 = CultureInfo.CreateSpecificCulture(language);
Console.WriteLine(c2.EnglishName);              // "French (France)"
Console.WriteLine(c2.IsNeutralCulture);         // "False"
Console.WriteLine((3.14m).ToString("C", c2));   // "3,14 €"

var languageAndCountry = "fr-CA";

var c3 = new CultureInfo(languageAndCountry);
Console.WriteLine(c3.EnglishName);              // "French (Canada)"
Console.WriteLine(c3.IsNeutralCulture);         // "False"
Console.WriteLine((3.14m).ToString("C", c3));   // "3,14 $"

In conclusion, if you have only the language, you cannot get everything. You can either get a neutral culture, or a "canonical" specific culture where some "typical" country is picked.

When you have both language and country, as in "fr-CA", things behave better.


On my system, the following specific cultures with "fr" exist:

fr-029, fr-BE, fr-BF, fr-BI, fr-BJ, fr-BL, fr-CA, fr-CD, fr-CF, fr-CG, fr-CH, fr-CI, fr-CM, fr-DJ, fr-DZ, fr-FR, fr-GA, fr-GF, fr-GN, fr-GP, fr-GQ, fr-HT, fr-KM, fr-LU, fr-MA, fr-MC, fr-MF, fr-MG, fr-ML, fr-MQ, fr-MR, fr-MU, fr-NC, fr-NE, fr-PF, fr-PM, fr-RE, fr-RW, fr-SC, fr-SN, fr-SY, fr-TD, fr-TG, fr-TN, fr-VU, fr-WF, fr-YT

(found with string.Join(", ", CultureInfo.GetCultures(CultureTypes.SpecificCultures).Where(x => x.TwoLetterISOLanguageName == "fr")))

Scintillator answered 30/1, 2017 at 16:12 Comment(0)
H
4

Yes you can. The following code:

var ci = new CultureInfo("fr");
Console.WriteLine(13.45.ToString(ci));

outputs "13,45" which is using the french decimal separator in the number.

The CultureInfo documentation says:

A neutral culture is specified by only the two-letter lowercase language code. For example, fr specifies the neutral culture for French, and de specifies the neutral culture for German.

Hydroquinone answered 30/1, 2017 at 15:34 Comment(3)
Note that ci.CultureTypes will have the flag NeutralCultures in that case. Equivalently, ci.IsNeutralCulture gives true. That is not really recommended. It is better to use something like new CultureInfo("fr-CA") where you give both a language and a country, corresponding to flag SpecificCultures.Scintillator
@JeppeStigNielsen: Would you recommend just picking an arbitrary country rather than doing the above then? What are the drawbacks of it being a NeutralCulture compared to choosing a country for it?Hydroquinone
var ci = CultureInfo.CreateSpecificCulture("fr") picks a specific culture for you. In this example it picks "fr-FR", that is French (France). I am no expert here, though.Scintillator

© 2022 - 2024 — McMap. All rights reserved.