简体   繁体   中英

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

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?

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();

你没有得到所有文化:

CultureTypes.AllCultures & ~CultureTypes.NeutralCultures

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM