Question

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!

Was it helpful?

Solution

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

OTHER TIPS

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

You can use the HTTP_ACCEPT_LANGUAGE object.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top