9

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;
             });
alan
  • 6,705
  • 9
  • 40
  • 70

3 Answers3

7

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.

crynion
  • 71
  • 2
  • This was not the case for me, I confirmed no culture values were set at any point in the pipeline. – alan Feb 05 '18 at 14:34
3

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);
    }
alan
  • 6,705
  • 9
  • 40
  • 70
  • 1
    i have the same problem and your way does not work. https://learn.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.builder.requestlocalizationoptions.defaultrequestculture?view=aspnetcore-2.2 means DefaultRequestCulture only work in case a supported culture could not be determined by one of the configured IRequestCultureProviders (QueryStringRequestCultureProvider, CookieRequestCultureProvider, AcceptLanguageHeaderRequestCultureProvider). => so I try to change to cookie to the desired default culture as https://stackoverflow.com/a/53592514/1712232 with IsEssential = true – nam vo Mar 03 '19 at 15:53
1

I had a similar problem to this. I was translating to Ukrainian language and I was using the country code ua instead of uk.

I changed that and it worked straight away.

Not saying this will fix the OP's problem but hopefully will help someone who comes across this question who has a similar problem.

Full list of codes here: https://msdn.microsoft.com/en-gb/library/ee825488(v=cs.20).aspx

Remotec
  • 10,304
  • 25
  • 105
  • 147