Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get browser language in ASP.NET Core?

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?

like image 698
Sajeetharan Avatar asked Mar 20 '18 10:03

Sajeetharan


People also ask

How can I get browser language in asp net core?

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.

How can I get browser culture?

To get a browser's culture in Blazor WebAssembly, call the (navigator. language) property in JavaScript using the JavaScript interop function in Blazor. It will return the current browser's language version. Follow this code to get the browser's culture in Blazor WebAssembly.

What is localizer C#?

Localization is the process of customizing your application for a given culture and locale. Cultures and Locales. The language needs to be associated with the particular region where it is spoken, and this is done by using locale (language + location).


1 Answers

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>();
    }
}
like image 105
Evgeni Nabokov Avatar answered Sep 17 '22 02:09

Evgeni Nabokov