Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get language without country from CultureInfo

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!

like image 967
Peter Avatar asked Oct 01 '09 12:10

Peter


2 Answers

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);
}
like image 136
Frederik Gheysels Avatar answered Sep 18 '22 22:09

Frederik Gheysels


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

like image 25
Krzysztof Król Avatar answered Sep 21 '22 22:09

Krzysztof Król