gpt4 book ai didi

c# - 如何使用全名创建 CultureInfo 实例或将全名转换为 CultureInfo 代码?

转载 作者:行者123 更新时间:2023-11-30 15:01:38 26 4
gpt4 key购买 nike

是否可以使用完整的显示名称创建 cultureInfo 对象的实例:

CultureInfo userCulture = new CultureInfo("Japanese (Japan)");

或将完整的显示名称转换为文化代码:

toCultureCode("Japanese (Japan)"); 

将返回 "ja-JP"

最佳答案

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

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

DEMO

toCultureCodeToString:

String name = "Japanese (Japan)";
CultureInfo japanCulture = getCultureByEnglishName(name);
Console.Write(japanCulture.ToString()); // -> "ja-JP"

关于c# - 如何使用全名创建 CultureInfo 实例或将全名转换为 CultureInfo 代码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13934774/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com