Why not all countries are presented in CultureInfo.GetCultures()?
Asked Answered
F

3

15

I am using this standard code for populating list of countries:

static void Main(string[] args)
{
    List cultureList = new List();

    CultureInfo[] cultures = CultureInfo.GetCultures(CultureTypes.AllCultures & ~CultureTypes.NeutralCultures);

    foreach (CultureInfo culture in cultures)
    {
        try
        {
            RegionInfo region = new RegionInfo(culture.LCID);

            if (!(cultureList.Contains(region.EnglishName)))
            {
                cultureList.Add(region.EnglishName);
                Console.WriteLine(region.EnglishName);
            }
        }
        catch (ArgumentException ex) 
        {
            // just ignore this
            continue;
        }
    }
}

I saw that some countries are missed. Just wondered what's the reason of such situation?

Formation answered 29/6, 2010 at 9:39 Comment(4)
sashaeve, a concrete example would be nice. IIRC it depends on the Windows version/edition as well.Honeybunch
@Henk Holterman: I did not find Ghana, Cote d'Ivoire, Cameroon and some others.Formation
possible duplicate of [Missing Countries & locations from CultureInfo when trying to ](#2920774)Steelworks
until the release of vista, Greenland was not among them either! :)Janayjanaya
E
8

The answer is: By design

CultureInfo.GetCultures is not designed to be a complete and definitive list of all the cultures in the world. It's only designed to get you the cultures that can be found on the computer.

CultureInfo documentation says:

Remember that the culture names and identifiers represent only a subset of cultures that can be found on a particular computer. Windows versions or service packs can change the available cultures. Applications add custom cultures using the CultureAndRegionInfoBuilder class. Users add their own custom cultures using the Microsoft Locale Builder tool. Microsoft Locale Builder is written in managed code using the CultureAndRegionInfoBuilder class.


Notes

Links on the MSDN that may be usefull:

And by the way, you can shorten your code with a simple LINQ 'command':

var regionInfos = CultureInfo.GetCultures(CultureTypes.SpecificCultures)
  .Select(c => new RegionInfo(c.LCID))
  .Distinct()
  .ToList();
Expand answered 2/4, 2014 at 10:10 Comment(1)
when I tried this it produced an error: System.ArgumentException: 'Customized cultures cannot be passed by LCID, only by name. Arg_ParamName_Name'Kempe
C
2

You are not getting all cultures:

CultureTypes.AllCultures & ~CultureTypes.NeutralCultures
Campball answered 29/6, 2010 at 9:43 Comment(3)
Oded, in the context of the question you're wrong. Neutral cultures don't represent a country, but only a language, and they are very limited in their use (mostly useful only for localizing resources or to get a specific culture from them).Steelworks
@Steelworks - he is doing a bitwise complement on NeutralCultures.Campball
which is correct because he does not want the neutral cultures in the result: Console.WriteLine(CultureTypes.AllCultures & ~CultureTypes.NeutralCultures); returns SpecificCultures, InstalledWin32Cultures which should include all specific cultures (e.g. those with countries).Steelworks
B
1

I would use CultureTypes.SpecificCultures but it does not answer your question.

Why there is only subset of world's countries? Well, there are so many of them. Somebody would have to maintain them and it does cost money. I think that's why Microsoft decided to support only the most "popular" ones.

BTW. You may create your own CultureInfo. Also, I haven't tried, but you can create RegionInfo instance by passing its ISO code in constructor. I am not sure what will happen if there is no matching CultureInfo, though.

Bath answered 7/9, 2010 at 16:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.