19

Does anyone know in ASP.Net how to get the language of the currentculture without it's countryname? I know this invariant culture's don't have this problem, but I don't know how to create them without specifying an explicit language. I want to display the active language and in nl-nl this is Dutch (Netherlands).

This is how I set the currentCulture:

private void Application_BeginRequest(Object source, EventArgs e)
{
    string[] languages = HttpContext.Current.Request.UserLanguages;
    string language = languages[0].ToLowerInvariant().Trim();
    if (!string.IsNullOrEmpty(language))
    {
        System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo(language);
        System.Threading.Thread.CurrentThread.CurrentCulture = System.Globalization.CultureInfo.CreateSpecificCulture(language);
    }
}

In my case, the culture is "nl-nl". Problem is that what is shown on the site when using CurrentCulture.EnglishName is "Dutch (Netherlands)". I only want to see Dutch!

Thanks!

Peter
  • 14,221
  • 15
  • 70
  • 110

3 Answers3

41

Simple:

CultureInfo ci = CultureInfo.GetCultureInfo ("nl-nl");

if( ci.IsNeutralCulture )
{
    Console.WriteLine (ci.EnglishName);
    Console.WriteLine (ci.NativeName);
}
else
{
    Console.WriteLine (ci.Parent.EnglishName);
    Console.WriteLine (ci.Parent.NativeName);
}
Frederik Gheysels
  • 56,135
  • 11
  • 101
  • 154
  • Unfortunately, for Great Britain (gb), it results `Unknown Language (gb)` :( – Yusril Maulidan Raji Jul 19 '17 at 08:54
  • is there a way to do this with bindings? – Tommehh Aug 08 '18 at 09:24
  • "IsNeutralCulture" means "...the language and regional parameters associated with the language, but not with the country or region. It differs from a specific language and regional parameters, which are associated both with the language and with the country or region. For example, fr is the name of the neutral French language, and fr-FR is the name of the French language and regional settings in France." – Sergey_T Dec 20 '19 at 10:24
6

CultureInfo object contains property called Parent - if it's set then then there is CultureInfo with desired EnglishName = Dutch

Krzysztof Król
  • 150
  • 1
  • 6
-3

You can use the HTTP_ACCEPT_LANGUAGE object.

Brandon Michael Hunter
  • 1,179
  • 3
  • 20
  • 48