Search code examples
c#asp.netglobalizationcultureinfo

How to make a dropdown list of all cultures (but no repeats)


I'm trying to make 2 dropdown lists.

The top one offers all cultures, (but no repeats). Example: English, Spanish, Filipino

After selecting from the top list the bottom list will show any specific types.

I right now I use this code for my top list.

foreach (CultureInfo cultureInfo in CultureInfo.GetCultures(CultureTypes.NeutralCultures))

However it does not show Filipino (Philippines) I'd rather not use GetCultures(CultureTypes.AllCultures)) because it shows too many at once.

It seems like I may need to load NeutralCultures into an IList. Then iterate through AllCultures to make sure it's ThreeLetterISOLanguageName is in the list, if not add it.

There a best practice for this?

Thanks


Solution

  • Look at the reference for the different CultureTypes values. It tells you what is included for each.

    I guess you want everything that's in all but the specific cultures? You could either combine all non-specific cultures into a set or get all cultures and exclude the specific ones. The second approach would be easiest to express in LINQ:

    var cultures = CultureInfo.GetCultures(CultureTypes.AllCultures)
                              .Except(CultureInfo.GetCultures(CultureTypes.SpecificCultures));
    

    Though it seems that since CultureTypes has the flags attribute, we could also just mask out the SpecificCultures when getting them.

    var cultures = CultureInfo.GetCultures(
        CultureTypes.AllCultures & ~CultureTypes.SpecificCultures
    );