Get display name in your language

I get 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, not ...

How can I get DisplayName culture in this particular language?

Thanks Miguel

+6
source share
4 answers

You need to display NativeName instead of DisplayName .

+11
source

DisplayName will be displayed in the location language of the current .NET platform.

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

Edit

After testing 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; 

Results:

native = "Deutsch (Deutschland)"

display = "German (Germany)"

name = "de-DE"

+5
source

If you just try to get a localized language of culture (without a country), you can use this snippet:

 CultureInfo culture = Thread.CurrentThread.CurrentCulture; string nativeName = culture.IsNeutralCulture ? culture.NativeName : culture.Parent.NativeName; 

If you use a specific localized language name, you can use this:

 string language = "es-ES"; CultureInfo culture = new CultureInfo(language); string nativeName = culture.IsNeutralCulture ? culture.NativeName : culture.Parent.NativeName; 

If you want to have a title name name (e.g. Français instead of français), use this line:

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

How to 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; } 
+1
source

Searching for cultureinfo.Displayname led to nativename on msdn and this led to How to translate CultureInfo language names

0
source

Source: https://habr.com/ru/post/956417/