How to get language without country from CultureInfo

Peter picture Peter · Oct 1, 2009 · Viewed 13.2k times · Source

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!

Answer

Frederik Gheysels picture Frederik Gheysels · Oct 1, 2009

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);
}