Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 472
sashaeve Avatar asked Jun 29 '10 09:06

sashaeve


People also ask

What does the Getcultures method of the CultureInfo class do?

Provides information about a specific culture (called a locale for unmanaged code development). The information includes the names for the culture, the writing system, the calendar used, the sort order of strings, and formatting for dates and numbers.

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.


1 Answers

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:

  • Predefined RegionInfo list: http://msdn.microsoft.com/en-us/library/system.globalization.regioninfo(VS.80).aspx
  • How to create custom Cultures: http://msdn.microsoft.com/en-us/library/ms172469(VS.80).aspx

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();
like image 67
Yves M. Avatar answered Sep 22 '22 05:09

Yves M.