Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.NET Core MVC RequestLocalization ignoring DefaultRequestCulture

I've implemented RequestLocalization for es-ES with a single MVC view via the following (note: this code is condensed to only the most relevant pieces):

Startup.cs

public void ConfigureServices(IServiceCollection services)
{
    services.AddViewLocalization(LanguageViewLocationExpanderFormat.Suffix,
                                     opts =>
                                     {
                                         opts.ResourcesPath = "Resources";
                                     });
}


public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
     var english = "en-US";
     var englishRequestCulture = new RequestCulture(culture: english, uiCulture: english);
     var supportedCultures = new List<CultureInfo>
                         {
                             new CultureInfo("en-US"),
                             new CultureInfo("es-ES")
                         };

     var options = new RequestLocalizationOptions
            {
                DefaultRequestCulture = englishRequestCulture,
                SupportedCultures = supportedCultures,
                SupportedUICultures = supportedCultures
            };

     app.UseRequestLocalization(options);
     app.UseMvc();
}

When passing culture=en-US or culture=es-ES as query string parameters, this works perfectly. My expectation is that the default culture should be en-US when no culture is provided. However, when I do not provide the culture parameter, my view is defaulting to es-ES. I have confirmed that all other Localization providers are also defaulted to en-US.

I should also note that I attempted Localization via ConfigureServices() but was unable to get this to function at all:

                services.Configure<RequestLocalizationOptions>(
             options =>
             {
                 var supportedCultures = new List<CultureInfo>
                     {
                         new CultureInfo("en-US"),
                         new CultureInfo("es-ES")
                     };

                 options.DefaultRequestCulture = new RequestCulture(culture: "en-US", uiCulture: "en-US");
                 options.SupportedCultures = supportedCultures;
                 options.SupportedUICultures = supportedCultures;
             });
like image 333
alan Avatar asked Jun 14 '17 15:06

alan


2 Answers

I had the same problem myself. Take a look at your HTTP requests! Do they contain an Accept-Language header set to es-ES (or anything)? Then your localization middleware is working just fine. One of the three default RequestCultureProviders, namely AcceptLanguageHeaderRequestCultureProvider, tries to determine the culture by doing what you did - looking for the Accept-Language header.

So no, the localization middleware does not ignore DefaultRequestCulture, as you and a previous answer suggested.

like image 159
crynion Avatar answered Nov 15 '22 01:11

crynion


After much trial and error, I determined that setting the DefaultRequestCulture property has no impact and, as a result, CookieRequestCultureProvider is actually defaulting to es-ES (though I am not entirely sure why, the machine this is running on is set to English and US locale).

As a workaround I modified my existing Configure() method to remove other (currently unused) providers:

    private void ConfigureApplicationLocalization(IApplicationBuilder app)
    {
        var english = "en-US";
        var englishRequestCulture = new RequestCulture(culture: english, uiCulture: english);
        var supportedCultures = new List<CultureInfo>
                     {
                         new CultureInfo("en-US"),
                         new CultureInfo("es-ES")
                     };

        var options = new RequestLocalizationOptions
        {
            DefaultRequestCulture = englishRequestCulture,
            SupportedCultures = supportedCultures,
            SupportedUICultures = supportedCultures
        };

        //RequestCultureProvider requestProvider = options.RequestCultureProviders.OfType<AcceptLanguageHeaderRequestCultureProvider>().First();
        //requestProvider.Options.DefaultRequestCulture = englishRequestCulture;

        RequestCultureProvider requestProvider = options.RequestCultureProviders.OfType<CookieRequestCultureProvider>().First();
        options.RequestCultureProviders.Remove(requestProvider);

        app.UseRequestLocalization(options);
    }
like image 3
alan Avatar answered Nov 15 '22 02:11

alan