6

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?

haim770
  • 48,394
  • 7
  • 105
  • 133
Omu
  • 69,856
  • 92
  • 277
  • 407

1 Answers1

3

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"));
haim770
  • 48,394
  • 7
  • 105
  • 133
  • 2
    for me (mvc 6 rc1-final) this extension is available without installing the localization package ( I tried installing, makes no difference ); also the extension requires at least 1 parameter so I tried `app.UseRequestLocalization(new RequestCulture("en"));` it only works if you put it before `app.UseMvc` and it ignores the browser language – Omu Apr 13 '16 at 07:45
  • that also requires specifying RequestCulture (so 2 parameters), and the result is the same – Omu Apr 13 '16 at 07:57
  • Can you check which is the assembly that provides your `UseRequestLocalization()` extension method? – haim770 Apr 13 '16 at 07:58
  • namespace Microsoft.AspNet.Builder Microsoft.AspNet.Localization.dll – Omu Apr 13 '16 at 08:10
  • How are you actually checking the current culture? – haim770 Apr 13 '16 at 08:15
  • (it looks like localization is a dependency now on Microsoft.AspNet.Mvc;) I check it by having this in the view: `@CultureInfo.CurrentCulture.Name @CultureInfo.CurrentCulture.DateTimeFormat.ShortDatePattern` – Omu Apr 13 '16 at 08:18
  • What is your actual `Accept-Language` in the request, and what does `@CultureInfo.CurrentCulture.Name` outputs? – haim770 Apr 13 '16 at 08:20
  • `Accept-Language:de-DE,de;q=0.8,en-US;q=0.6,en;q=0.4` and it's outputting `en` and `M/d/yyyy` – Omu Apr 13 '16 at 08:29
  • @Omu, I believe the problem is that you need to add `de-DE` to the `SupportedCultures` list in `RequestLocalizationOptions`. Something like: `app.UseRequestLocalization(new RequestCulture("en"), new RequestLocalizationOptions { SupportedCultures = new List { new CultureInfo("de-DE") } });` – haim770 Apr 13 '16 at 08:38
  • adding SupportedCultures worked, but that means that I need to add all the possible cultures to that list – Omu Apr 13 '16 at 08:54
  • @Omu, Apparently. And this line is the culprit: https://github.com/aspnet/Localization/blob/b52e33caa7927093dc3ec540fd3282f4f69ae169/src/Microsoft.Extensions.Globalization.CultureInfoCache/CultureInfoCache.cs#L34 – haim770 Apr 13 '16 at 09:03
  • @Omu, I added an issue on GitHub: https://github.com/aspnet/Localization/issues/237 – haim770 Apr 13 '16 at 09:20
  • A small correction, it doesn't register to `AcceptLanguageHeaderRequestCultureProvider` only. All 3 providers are registered (QueryStringRequestCultureProvider, CookieRequestCultureProvider and AcceptLanguageHeaderRequestCultureProvider) in exact this order. The order is important because it decides which will take precedence. If you provide a query parameter, this one will be taken and cookie and accept header will be ignored even if set. If no query was provided it will check if there is a cookie and use that instead. Only then it uses accept-language header if provided – Tseng Apr 13 '16 at 10:29
  • You can also see that in the code on GitHub: https://github.com/aspnet/Localization/blob/1.0.0-rc1/src/Microsoft.AspNet.Localization/RequestLocalizationOptions.cs – Tseng Apr 13 '16 at 10:30
  • @Tseng, I never meant that `AcceptLanguageHeaderRequestCultureProvider` is the only provider used. I only mentioned it because it's the one that is relevant to the question. – haim770 Apr 13 '16 at 10:52
  • Yes, but for other users and users new to ASP.NET Core who come and find this question it may not be obvious that there are other types of providers and that they take precedence and may be surprised, so it would be good to mention it in the answer for the future readers – Tseng Apr 13 '16 at 11:38