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

1

12

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

Shoreless answered 10/5, 2011 at 21:47 Comment(0)
E
19

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
);
Estebanesteem answered 10/5, 2011 at 21:59 Comment(6)
Thanks Jeff, it works very well. I like how it has just English and French - rather than the specifics. What I do not understand is why it will have Czech "cs-CZ" but not Filipino "fil-PH". Both are specific (only 1 country each).Shoreless
@aron: I'm not sure I understand what you are saying. The first part of the name is just the ISO 639-1 code code while the second part is the ISO 3166 code. ISO 639-1 code alone refers to a language (a neutral culture not associated with a specific region). Though the Czech Republic (cs-CZ) uses the the Czech language (cs), they are not equivalent. Both of those are specific cultures and should not be listed here. If it is, it is probably a culture object that was created by you (which is included).Estebanesteem
Instead of calling GetCultures twice and using Except, you can use GetCultures(CultureTypes.AllCultures & ~CultureTypes.SpecificCultures)Nauru
@RuudLenders: You know, you're right. Inspecting the values and would suggest that should work, and the docs support that. Good call.Estebanesteem
What does (CultureTypes.AllCultures & ~CultureTypes.SpecificCultures) mean? I am flabbergasted by the operators & and ~. I've researched a bit about these but I could not wrap my head around that line.Cleo
@KristianneNerona CultureTypes is a set of flags. It's just using bitwise operations to exclude a particular flag -- in this case, SpecificCultures.Estebanesteem

© 2022 - 2024 — McMap. All rights reserved.