ASP.NET Core MVC Localization Warning: AcceptLanguageHeaderRequestCultureProvider returned the following unsupported cultures
Asked Answered
B

4

12

I have an ASP.NET Core MVC app that use resource localization. It currently supports only one culture (fa-IR) and I want to all localizations be processed based on this culture. In ASP.NET Core 1.1 I have no issue but after migrating from ASP.NET Core 1.1 to 2.1 I see this warning for each HTTP request:

AcceptLanguageHeaderRequestCultureProvider returned the following unsupported cultures 'en-US, en, fa'.

This is my Startup:

public class Startup
{
    protected CultureInfo DefaultCultureInfo { get; private set; } = new CultureInfo("fa-IR");

    public void ConfigureServices(IServiceCollection services)
    {
        CultureInfo.DefaultThreadCurrentCulture = DefaultCultureInfo;
        CultureInfo.DefaultThreadCurrentUICulture = DefaultCultureInfo;
        services.AddLocalization(options => { options.ResourcesPath = "Resources"; });

        services.AddMemoryCache();
        services.AddSession();

        services.AddMvc()
        .AddDataAnnotationsLocalization()
        .AddViewLocalization()
        .AddControllersAsServices()
        .SetCompatibilityVersion(CompatibilityVersion.Version_2_1)
        .AddSessionStateTempDataProvider();

        services.Configure<RequestLocalizationOptions>(options =>
        {
            var supportedCultures = new[] { new CultureInfo("fa-IR"), new CultureInfo("en-US") };
            options.DefaultRequestCulture = new RequestCulture("fa-IR", "fa-IR");
            options.SupportedCultures = supportedCultures;
            options.SupportedUICultures = supportedCultures;

            options.RequestCultureProviders.Insert(0, new CustomRequestCultureProvider(async context =>
            {
                return new ProviderCultureResult("fa-IR");
            }));
        });
    }

    public void Configure(IApplicationBuilder app)
    {
        var supportedCultures = new[] { DefaultCultureInfo };
        app.UseRequestLocalization(new RequestLocalizationOptions()
        {
            DefaultRequestCulture = new RequestCulture(DefaultCultureInfo),
            SupportedCultures = supportedCultures,
            SupportedUICultures = supportedCultures,
            FallBackToParentCultures = true,
            FallBackToParentUICultures = true,
        });

        app.UseSession();
        app.UseMvc();
        app.UseCookiePolicy();
    }
}

In fact it's just a warning, My app works fine but my log files are filled by this warning so I am searching for a way to make MVC know what I want.

[Edit]: I have added CustomRequestCultureProvider but has no effect and after putting a breakpoint in that line realized that line does not get hit.

[Edit2]: As user2429841 suggested I added "fa" to the supportedCultures the warnings gone but my resource files (that are named x.fa-IR.resx) are not picked up any more. Is there any way to say to MVC that if you get some culture treat it as another culture?

Barren answered 12/8, 2018 at 10:25 Comment(0)
D
25

As documented you can filter logging by specifying the minimum log levels per provider.

In your appsettings.json Logging configuration add Microsoft.AspNetCore.Localization and set the value to Error. In that case the messages will no longer appear in the log.

"Logging": {
  "IncludeScopes": false,
  "LogLevel": {
    "Default": "Debug",
    "System": "Information",
    "Microsoft": "Information",
    "Microsoft.AspNetCore.Localization": "Error"  // <-- Disables the warnings
  }
},

Update ASP.NET Core 3.0

This issue has been resolved in ASP.NET Core 3.0. As documented here (the official documentation isn't updated yet):

The issue was the localization middleware will log tons if not hundreds of logs if the requested culture is unsupported, image that you will receive 1 warning log per request which is noisy and trouble over the time.

For that we simply decide to change the LogLevel.Warning to LogLevel.Debug to reduce the amount of logs at least.

Devoe answered 8/11, 2018 at 15:56 Comment(1)
This should be an accepted answer, because the issue at hand is just log junk and filtering is how you solve it. I see no point in warning when client sends in header culture you don't support, it correctly goes with default, but makes unnecessary fuss about it.Nick
B
1
services.AddOptions();            

Add this line to your Configure method

app.UseRequestLocalization(
  app.ApplicationServices.GetService<IOptions<RequestLocalizationtions>>().Value);
Brush answered 12/8, 2018 at 11:31 Comment(6)
Mind to... explain your answer?Sinuous
this works, and I realized that even removing the entire app.UseRequestLocalization() removes the warnings and the localizations still works. but does this have any side effect? can you explain more pleaseBarren
yes, you don't need previous app.UseRequestLocalization update it with this new one. It does not have any side effect as you are not doing something out of context. Can you please clarify which part you did not understand?Brush
In ASP Core 1 app.UseRequestLocalization was used to configure SupportedCultures , SupportedUICultures and some other options. So you mean this is not required in ASP Core 2?Barren
@Barren I have the same problem and just removing "UseRequestLocalization" is not an option. Did you find a solution?Natatory
@Natatory unfortunately not yetBarren
P
1

It's your Webbrowser who sends 'en-US, en, fa' in the Accept-Language-HTTP-Header and asp.net core just tells you, that you don't support any of this cultures.

The answer from Kakos649 doesn't makes sense, because if you resolve your RequestLocalizationtions-instance over the options service, it will result into the same.

EDIT: If you support any of this languages, the warning disappears.

Puritanical answered 13/8, 2018 at 10:50 Comment(4)
I added "fa" to the supportedCultures the warnings gone but my resource files (that are named x.fa-IR.resx) are not picked up any more. Is there any way to say to MVC that if you get some culture treat it as another culture?Barren
Just use fa for your resource files instead of fa-IR, because if your browser sends fa-IR, .net will match it with fa. If you have a specific resource key for fa-IR only, create a *.fa-IR.resx file containing this key with the specific location. .net will cascade it -> fa-IR -> fa -> your default localization.Puritanical
Where is this 'fa' come from? can I change it to 'fa-IR'? If I change my localization files to *.fa.resx is it possible that some client send request with 'fa-IR' or any other localizations and resource files does not get picked up?Barren
If you support just "fa" and the browser sends only "fa-IR", ASP.NET will pick "fa" instead.Puritanical
N
1

The localization middleware uses 3 providers by default to figure out the request's culture.

They are evaluated in order:

  • Query string
  • Cookie
  • Accept-Language header

If you do not set a cookie to get the culture, the localization middleware will default to "Accept-Language header".

For example: Accept-Language header "en-US, en, fa" will requests for US English first, then any other English locale, and finally fa-IR.So the warnings looking for en locale makes sense.

You can set a cookie and your warnings should go away.

More details plus how to set a culture cookie https://joonasw.net/view/aspnet-core-localization-deep-dive

Nananne answered 19/3, 2019 at 22:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.