Get browser language in .AspNetCore2.0?

I am trying to get the default language from the browser and I use the following code to get it:

var languages = HttpContext.Request.UserLanguages;

Since the above is not supported with .NET Core 2 I tested with:

var requestContext = Request.HttpContext.Features.Get<IRequestCultureFeature>();

However, it returns null. What is the correct way or alternative to get the language?

IRequestCultureFeature provides the first matched language, which is supported by your application. Declaration of supported languages is defined in Configure() of your Startup class (see example). If you still need all accepted languages as a simple string[] like the older Request.UserLanguages property, then use the HeaderDictionaryTypeExtensions.GetTypedHeaders() extension defined in the Microsoft.AspNetCore.Http namespace:

// In your action method.
var languages = Request.GetTypedHeaders()
                       .AcceptLanguage
                       ?.OrderByDescending(x => x.Quality ?? 1) // Quality defines priority from 0 to 1, where 1 is the highest.
                       .Select(x => x.Value.ToString())
                       .ToArray() ?? Array.Empty<string>();

The array languages contains the list of accepted languages according to the priority parameter q. The language with the highest priority comes first. To get the default language take the first element of the array languages.

As an extension method:

using System.Collections.Generic;
using System.Linq;

using Microsoft.AspNetCore.Http;

public static class HttpRequestExtensions
{
    public static string[] GetUserLanguages(this HttpRequest request)
    {
        return request.GetTypedHeaders()
            .AcceptLanguage
            ?.OrderByDescending(x => x.Quality ?? 1)
            .Select(x => x.Value.ToString())
            .ToArray() ?? Array.Empty<string>();
    }
}

Get browser language in .AspNetCore2.0?, AspNetCore2.0? I am trying to get the default language from the browser and I use the following code to get it: var languages = HttpContext.Request. am trying to get the default language from the browser and I use the following code to get it: var languages = HttpContext. Request. UserLanguages; Since the above is not supported with .NET Core 2 I tested with: var requestContext = Request. HttpContext. Features. Get < IRequestCultureFeature >(); However, it returns null.

You need to add the localisation middleware to be able to get the IRequestCultureFeature feature:

public void Configure(IApplicationBuilder app)
{
    //...

    //Add this:
    app.UseRequestLocalization();

    //...
}

Now in your controller you can request the feature like this:

var requestCulture = Request.HttpContext.Features.Get<IRequestCultureFeature>();

Get language and country from a browser in ASP.NET, How to resolve the language and country from the visiting browser. string language = languages[0]. catch (ArgumentException). The Accept-Language HTTP header from a browser request isn't an infallible way to detect the user's preferred language (see Setting language preferences in a browser). A production app should include a way for a user to customize their choice of culture. Set the Accept-Language HTTP header in IE. From the gear icon, tap Internet Options. Tap

You can get the browser language from the Request Headers

Write on your controller:

//For example --> browserLang = 'en-US'
var browserLang= Request.Headers["Accept-Language"].ToString().Split(";").FirstOrDefault()?.Split(",").FirstOrDefault();

Globalization and localization in ASP.NET Core, Gets a sorted string array of client language preferences. public string[] UserLanguages { get; } The language names are provided by the browser, and there is no definitive list of all UserLanguages; for (count = 0; count < userLang. Full sample solution built on ASP.NET Core 2.0 for this blog post is available at GitHub repository gpeipman/AspNetCoreLocalization. My scenario was simple: We have limited number of supported languages and the number of languages doesn’t change often; Coming of new language means changes in organization and it will probably be high level

You have to add the localization middleware to enable parsing of the culture header, and then get the value through IRequestCultureFeature.

Check this link : https://github.com/aspnet/Mvc/issues/3549

HttpRequest.UserLanguages Property (System.Web), Get browser language in .AspNetCore2.0?. I am trying to get the default language from the browser and I use the following code to get it: var languages = ASP.NET: Detecting User/Browser Languages Multilingual application, provide a way, so that user can select preferable language and application adopt culture based on selected language. But intelligent web application should auto detect user browser language and accordingly adopt culture settings for application.

Date Time Culture - Universal Dashboard Help, hey all, is it my browser that dont want to change culture, or is it UD that force Culture to US ? I want time in 24 New-UDParagraph -Text (get-culture | convertto-json) and see which culture shows up. When testing on my Would prolly be best if all the locales went default to user locale. AspNetCore2.0? Culture in ASP.net has always been a bit finicky to get right. In ASP.NET Core there is no exception but with the addition of middleware, things are now configured a little differently. Lifting and shifting your standard ASP.NET code may not always work in ASP.NET Core, and at the very least, it won’t be done […]

Asp net website project example, Asp net website project example 3 0 0, 1, 2 1, 2, 3 1 2 4 5 7 6 If we continue in the same way for the rest of the sides we get a list of all the indices for web sites, web application related projects. net with c# language to manage Society Feb 28, 2015 · Facebook Login with ASP. NET Core2 Web application project View:  New ASP.NET Core project has been just created. Moreover, we can display it in our web browser. To do so, click on IIS Express button on the navigation bar. After few second default website should appear in our web browser. We can switch between all items on navigation bar.

Grpc statuscode, 0 and using aspnetcore 2. 0 on google app engine fle What operating system (​LinuxIm trying to get the status code from the status codes are the defacto language for describing results of the requests browsers (clients) make to servers​. The reason I'm showing it is that it may provide a good example of getting all the useful information about the user's language: browser language, Accept-Language, geolocation (with getting country/language info from the CLDR), and of course, user's own site preferences.

Comments
  • Have you added the localisation middleware?
  • Upvoting. I don't see why someone downvoted it because this is a good answer
  • @JérômeMEVEL downvote might be revenge, because I downvoted the accepted answer.
  • It is not answer. IRequestCultureFeature is not string[].
  • @EvgeniNabokov Of course it isn't a string, it's a service that implements the IRequestCultureFeature interface, exactly as OP uses in the question. OP thought it was correct and accepted is as such, so thank you for the downvote!
  • @DavidG How to get the list of accepted languages using this feature? I used to use Request.UserLanguages which is string[].
  • @EvgeniNabokov Like I said above, use the requestCulture object. So something like var requestedCulture = requestCulture.RequestCulture;
  • @DavidG First, from your example is not clear how to get any language from IRequestCultureFeature. Provide more informative example, please. Second, IRequestCultureFeature is not replace for UserLanguages. I have interfaces that consume user languages as string[]. I can't change interfaces and I can't just create an 1-element array from Culture or UICulture (which matches the first supported language), because I need all accepted languages to pass them further. I came to conclusion I have to parse the accept-languages header manually (as written by Carlos Tenorio). That's sad.
  • Discarding the priority parameter "q" is a bad idea.