Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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?