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:
How can I get the DisplayName of a Culture in that specific language?
Thank You, Miguel
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;
}
A search on cultureinfo.Displayname led to nativename on msdn, and that led to How to translate CultureInfo language names
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With