Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Enabling Client-Based Culture in Asp.Net Core

In MVC 6 by default, CultureInfo.CurrentCulture is the one used by windows, not by the browser.

In MVC 5 I could put this in web.config:

<globalization culture="auto" uiCulture="auto"/>

and that would make the CultureInfo.CurrentCulture be the same as specified by the browser (Accept-Language header).

Is there a way to configure the MVC 6 app to use the browser culture by default?

like image 908
Omu Avatar asked Apr 13 '16 07:04

Omu


People also ask

What is enable client based culture?

Enable Client Based Culture [enableClientBasedCulture]Gets or sets a value indicating whether the Culture and UICulture properties should be based on the AcceptLanguage header field value that is sent by the client browser.

How can use culture in asp net core?

Applying Cultures Copy the required culture JavaScript file from the \js\culture\ folder of your Telerik UI for ASP.NET Core installation to the wwwroot/lib/kendo-ui/js/cultures/ folder of your application. Alternatively, provide the culture files by using the Kendo CDN service.

What is ASP Net culture setting?

In an ASP.NET Web page, you can set to two culture values, the Culture and UICulture properties. The Culture value determines the results of culture-dependent functions, such as the date, number, and currency formatting, and so on. The UICulture value determines which resources are loaded for the page.


1 Answers

You need to install the Microsoft.AspNet.Localization NuGet package and add the following to your Startup.cs:

public void Configure(IApplicationBuilder app)
{
    app.UseRequestLocalization();
    app.UseMvc();
}

By default, it registers the AcceptLanguageHeaderRequestCultureProvider as a culture-provider, that should be equivalent to the legacy enableClientBasedCulture setting.

Update:

As per your comment, since you're using the RC1 version, you must provide a default culture to the method. For example:

app.UseRequestLocalization(new RequestCulture("en"));
like image 116
haim770 Avatar answered Oct 31 '22 10:10

haim770