2
votes

I'm trying to force my localization to Swedish (sv-SE). Everything is working locally, but after deploying to Azure the validation errors always show in English.

This is my configuration in Startup.cs.

public void ConfigureServices(IServiceCollection services)
{
    ...

    services.AddLocalization();

    ...
}

public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerfactory)
    ...

    RequestLocalizationOptions localizationOptions = new RequestLocalizationOptions
    {
       SupportedCultures = new List<CultureInfo> { new CultureInfo("sv-SE") },
       SupportedUICultures = new List<CultureInfo> { new CultureInfo("sv-SE") }
    };

    app.UseRequestLocalization(localizationOptions, new RequestCulture("sv-SE"));

    ...
}

When rendering the view I can see that the UI-Culture and Culture is correctly set to sv-SE both locally and on Azure.

Do I have to include something when publishing to Azure, a localization resource or something?

I'm using the latest version of ASP.NET Core (RC1).

1
What does your browser send as accept-language?Joachim Isaksson
This is what my browser is sending: Accept-Language:en-US,en;q=0.8,nb;q=0.6,ru;q=0.4,sv;q=0.2. Even if I use the request argument ?culture=sv-SE it outputs English. It seems like the code is working, but on Azure it cant find the Swedish localization resources and outputs English instead. That is my guess at least.Christian Sörensen
The browser language shouldn't matter, because only Swedish is a supported culture. Did you deploy the Swedish resource files to Azure?Juergen Gutsch
I haven't included any extra resources. Which files and where should I include them?Christian Sörensen
Are you sure you get English version or key for localized string?Iren Saltalı

1 Answers

0
votes

based on this article, first you need to set it up the RequestLocalizationMiddleware as a service

services.Configure<RequestLocalizationOptions>(
    opts =>
    {
        var supportedCultures = new List<CultureInfo>
        {
            new CultureInfo("sv-SE")
        };

        opts.DefaultRequestCulture = new RequestCulture("sv-SE");
        // Formatting numbers, dates, etc.
        opts.SupportedCultures = supportedCultures;
        // UI strings that we have localized.
        opts.SupportedUICultures = supportedCultures;
    });

and this is not enough, the application pipeline should also be included:

public void Configure(IApplicationBuilder app)  
{
    app.UseStaticFiles();

    var options = 
    app.ApplicationServices.GetService<IOptions<RequestLocalizationOptions>>();
    app.UseRequestLocalization(options.Value);

    app.UseMvc(routes =>
    {
         routes.MapRoute(
             name: "default",
             template: "{controller=Home}/{action=Index}/{id?}");
    });
}