简体   繁体   English

ASP.NET Core MVC本地化警告:AcceptLanguageHeaderRequestCultureProvider返回了以下不支持的区域性

[英]ASP.NET Core MVC Localization Warning: AcceptLanguageHeaderRequestCultureProvider returned the following unsupported cultures

I have an ASP.NET Core MVC app that use resource localization. 我有一个使用资源本地化的ASP.NET Core MVC应用程序。 It currently supports only one culture (fa-IR) and I want to all localizations be processed based on this culture. 它目前仅支持一种文化(fa-IR),我希望根据这种文化来处理所有本地化。 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: 在ASP.NET Core 1.1中,我没有任何问题,但是从ASP.NET Core 1.1迁移到2.1之后,对于每个HTTP请求,我都会看到以下警告:

AcceptLanguageHeaderRequestCultureProvider returned the following unsupported cultures 'en-US, en, fa'. AcceptLanguageHeaderRequestCultureProvider返回了以下不受支持的区域性“ 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. 实际上,这只是一个警告,我的应用程序运行正常,但此警告填充了我的日志文件,因此我正在寻找一种使MVC知道我想要的方法。

[Edit]: I have added CustomRequestCultureProvider but has no effect and after putting a breakpoint in that line realized that line does not get hit. [编辑]:我添加了CustomRequestCultureProvider,但是没有效果,在该行中添加断点后意识到该行未命中。

[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. [Edit2]:正如user2429841所建议的那样,我在supportedCultures中添加了“ fa”警告已消失,但是我的资源文件(名为x.fa-IR.resx)不再被使用。 Is there any way to say to MVC that if you get some culture treat it as another culture? 有什么办法对MVC说,如果您掌握了某种文化,就将其视为另一种文化?

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 . 在您的appsettings.json Logging配置中,添加Microsoft.AspNetCore.Localization并将其值设置为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 更新ASP.NET Core 3.0

This issue has been resolved in ASP.NET Core 3.0. 此问题已在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. 问题是,如果不支持所请求的区域性,本地化中间件将记录大量日志(如果不是数百个日志的话),那么您每次请求将收到1条警告日志的图像,随着时间的流逝,这将是嘈杂的麻烦。

For that we simply decide to change the LogLevel.Warning to LogLevel.Debug to reduce the amount of logs at least. 为此,我们只是简单地决定将LogLevel.Warning更改为LogLevel.Debug,以至少减少日志数量。

services.AddOptions();            

Add this line to your Configure method 将此行添加到您的Configure方法

app.UseRequestLocalization(
  app.ApplicationServices.GetService<IOptions<RequestLocalizationtions>>().Value);

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. 是您的Web浏览器在Accept-Language-HTTP-Header和asp.net核心中发送“ en-US,en,fa”,只是告诉您,您不支持任何一种文化。

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. Kakos649的答案没有道理,因为如果您通过选项服务解决RequestLocalizationtions-instance,它将得到相同的结果。

EDIT: If you support any of this languages, the warning disappears. 编辑:如果您支持任何一种语言,警告将消失。

The localization middleware uses 3 providers by default to figure out the request's culture. 默认情况下,本地化中间件使用3个提供程序来确定请求的区域性。

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". 如果您未设置Cookie来获取区域性,则本地化中间件将默认为“ 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. 例如:Accept-Language标头“ en-US,en,fa”将首先要求使用美国英语,然后要求使用其他任何英语语言环境,最后要求使用fa-IR。因此,寻找en区域设置的警告很有意义。

You can set a cookie and your warnings should go away. 您可以设置一个cookie,并且警告会消失。

More details plus how to set a culture cookie https://joonasw.net/view/aspnet-core-localization-deep-dive 更多详细信息以及如何设置文化Cookie https://joonasw.net/view/aspnet-core-localization-deep-dive

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM