NoWar
NoWar

Reputation: 37633

Get CultureInfo by culture English name

Is it possible to get CultureInfo by culture English name?

Imagine we have got: Danish, English, Spanish and etc...

Thank you!

Upvotes: 7

Views: 4767

Answers (3)

Tim Schmelter
Tim Schmelter

Reputation: 460018

There's no builtin method to get a culture by it's english name, so you could write one:

public static CultureInfo getCultureByEnglishName(String englishName)
{
    // create an array of CultureInfo to hold all the cultures found, 
    // these include the users local culture, and all the
    // cultures installed with the .Net Framework
    CultureInfo[] cultures = CultureInfo.GetCultures(CultureTypes.AllCultures & ~CultureTypes.NeutralCultures);
    // get culture by it's english name 
    var culture = cultures.FirstOrDefault(c => 
        c.EnglishName.Equals(englishName, StringComparison.InvariantCultureIgnoreCase));
    return culture;
}

Here's a demo: http://ideone.com/KX4U8l

Upvotes: 3

Habib
Habib

Reputation: 223187

var EnglishCulture = CultureInfo.GetCultures(CultureTypes.AllCultures)
                                .Where(r => r.EnglishName == "English");

Or if you need FirstOrDefault

var EnglishCulture = CultureInfo.GetCultures(CultureTypes.AllCultures)
                                .FirstOrDefault(r=> r.EnglishName == "English");

Upvotes: 10

m0sa
m0sa

Reputation: 10940

var names = CultureInfo.GetCultures(CultureTypes.AllCultures).ToLookup(x => x.EnglishName);
names["English"].FirstOrDefault();

Upvotes: 11

Related Questions