简体   繁体   English

以其语言获取文化显示名称

[英]Get Culture Display Name in its language

I am getting the current culture as follows: 我现在的文化如下:

var culture = Thread.CurrentThread.CurrentCulture.DisplayName;

The problem is that I always get the name in English: 问题是我总是用英文命名:

  • EN becomes English EN成为英语
  • PT becomes Portuguese instead of Português PT成为葡萄牙语而不是Português
  • FR becomes French instead of ... FR变成法语而不是......

How can I get the DisplayName of a Culture in that specific language? 如何以该特定语言获取文化的DisplayName?

Thank You, Miguel 谢谢你,米格尔

您需要显示NativeName而不是DisplayName

DisplayName will be shown in the language of the location of the current .NET framework. DisplayName将以当前.NET框架的位置语言显示。

You can use NativeName (or maybe even just Name I've not tried this) instead of DisplayName , that should do the trick. 您可以使用NativeName (或者甚至可能只是Name我没有尝试过这个)而不是DisplayName ,这应该可以解决问题。

Edit 编辑

After testing this with the following code: 用以下代码测试后:

// set the current culture to German    
Thread.CurrentThread.CurrentCulture = new CultureInfo("de-DE");
var native = Thread.CurrentThread.CurrentCulture.NativeName;
var display = Thread.CurrentThread.CurrentCulture.DisplayName;
var name = Thread.CurrentThread.CurrentCulture.Name;

The results were: 结果是:

native = "Deutsch (Deutschland)" native =“Deutsch(Deutschland)”

display = "German (Germany)" display =“德语(德国)”

name = "de-DE" name =“de-DE”

If one just tries to get the localized language of a culture (without the country) one can use this snippet: 如果只是试图获取文化的本地化语言(没有国家/地区),可以使用此代码段:

CultureInfo culture = Thread.CurrentThread.CurrentCulture;

string nativeName = culture.IsNeutralCulture
    ? culture.NativeName
    : culture.Parent.NativeName;

If one will use a specific localized language name, one can use this: 如果有人使用特定的本地化语言名称,可以使用:

string language = "es-ES";
CultureInfo culture = new CultureInfo(language);

string nativeName = culture.IsNeutralCulture
    ? culture.NativeName
    : culture.Parent.NativeName;

If one wants to have a title case name (eg Français instead of français), use this line: 如果想要一个标题案例名称(例如Français而不是français),请使用以下行:

string result = CultureInfo.CurrentCulture.TextInfo.ToTitleCase(nativeName);

As a method: 作为一种方法:

private static string GetTitleCaseNativeLanguage(string language)
{
    CultureInfo culture = new CultureInfo(language);

    string nativeName = culture.IsNeutralCulture
        ? culture.NativeName
        : culture.Parent.NativeName;

    return CultureInfo.CurrentCulture.TextInfo.ToTitleCase(nativeName);
}

Or as an extension method: 或者作为扩展方法:

public static string GetNativeLanguageName(this CultureInfo culture, bool useTitleCase = true)
{
    string nativeName = culture.IsNeutralCulture
        ? culture.NativeName
        : culture.Parent.NativeName;

    return useTitleCase
        ? CultureInfo.CurrentCulture.TextInfo.ToTitleCase(nativeName)
        : nativeName;
}

搜索cultureinfo.Displayname导致msdn上的nativename,这导致了如何翻译CultureInfo语言名称

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM