11

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
  • PT becomes Portuguese instead of Português
  • FR becomes French instead of ...

How can I get the DisplayName of a Culture in that specific language?

Thank You, Miguel

Miguel Moura
  • 36,732
  • 85
  • 259
  • 481

3 Answers3

21

You need to display the NativeName instead of the DisplayName.

Roy Dictus
  • 32,551
  • 8
  • 60
  • 76
10

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 (e.g. Français instead of français), use this line:

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;
}
Baccata
  • 473
  • 1
  • 7
  • 15
7

DisplayName will be shown in the language of the location of the current .NET framework.

You can use NativeName (or maybe even just Name I've not tried this) instead of DisplayName, that should do the trick.

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)"

display = "German (Germany)"

name = "de-DE"

tomsullivan1989
  • 2,760
  • 14
  • 21