How to get language without country from CultureInfo
Asked Answered
P

3

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!

Premolar answered 1/10, 2009 at 12:12 Comment(0)
B
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);
}
Bissell answered 1/10, 2009 at 12:22 Comment(3)
Unfortunately, for Great Britain (gb), it results Unknown Language (gb) :(Unveil
is there a way to do this with bindings?Brewer
"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."Lillylillywhite
Q
6

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

Quintin answered 1/10, 2009 at 12:18 Comment(0)
A
-3

You can use the HTTP_ACCEPT_LANGUAGE object.

Akvavit answered 1/10, 2009 at 13:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.