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

aron
  • 2,856
  • 11
  • 49
  • 79

1 Answers1

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
);
Jeff Mercado
  • 129,526
  • 32
  • 251
  • 272
  • 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). – aron May 11 '11 at 02:49
  • @aron: I'm not sure I understand what you are saying. The first part of the name is just the [ISO 639-1 code](http://en.wikipedia.org/wiki/ISO_639-1) code while the second part is the [ISO 3166 code](http://en.wikipedia.org/wiki/Iso_3166). 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). – Jeff Mercado May 11 '11 at 03:22
  • 2
    Instead of calling `GetCultures` twice and using `Except`, you can use `GetCultures(CultureTypes.AllCultures & ~CultureTypes.SpecificCultures)` – Rudey Oct 11 '17 at 02:29
  • @RuudLenders: You know, you're right. Inspecting the values and would suggest that should work, and the docs support that. Good call. – Jeff Mercado Oct 11 '17 at 02:54
  • 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. – Kristianne Nerona Oct 07 '19 at 14:31
  • 1
    @KristianneNerona `CultureTypes` is a set of flags. It's just using bitwise operations to exclude a particular flag -- in this case, `SpecificCultures`. – Jeff Mercado Oct 07 '19 at 15:37