通过文化英文名称获取CultureInfo

时间:2012-11-12 11:05:44

标签: c# .net

是否可以通过culture English name获取CultureInfo

想象一下我们有:丹麦语,英语,西班牙语等...

谢谢!

3 个答案:

答案 0 :(得分:10)

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

答案 1 :(得分:8)

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

或者如果您需要FirstOrDefault

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

答案 2 :(得分:3)

没有内置方法可以通过它的英文名称来获取文化,所以你可以写一个:

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;
}

以下是演示http://ideone.com/KX4U8l