简体   繁体   中英

How is a default culture selected locally - (and why not when running on Azure)

I'm building then running my asp.net CORE site in visual studio, and I get my localised resources loading as soon as the site launches, set to the default ie the English.

    public void ConfigureServices(IServiceCollection services)
    {

        services.AddLocalization(options => options.ResourcesPath = "Resources");

        services.Configure<RequestLocalizationOptions>(options =>
        {
            var supportedCultures = new[]
            {
                new CultureInfo("sv-SE"),
                new CultureInfo("en-GB"),
                new CultureInfo("fr-FR")
            };

            // Explicitly state numbers/dates etc.
            options.SupportedCultures = supportedCultures;
            // Explicitly state UI strings (e.g. localised strings)
            options.SupportedUICultures = supportedCultures;

            // etc...

            services.AddMvc().AddViewLocalization();
        });
    }

However, when I publish this to Azure and visit site, there is no default language engaged, it gives me all the @SharedLocalizer variable names. If I then select a language, everything works as intended so I'm left with two questions:

1) When running my site in VS how / where is it setting the default culture?

2) How can I get my site to do this when published via Azure?

I've got this method in my home/index controller but this sets language based on user selection - how is it happening automatically when I'm running it in VS?!

    public IActionResult SetLanguage(string culture, string returnUrl)
    {
        CultureInfo ci = new CultureInfo(culture);
        CultureInfo.DefaultThreadCurrentCulture = ci;
        CultureInfo.DefaultThreadCurrentUICulture = ci;
        return Redirect(returnUrl);
    }

You do not set DefaultRequestCulture for RequestLocalizationOptions .

services.Configure<RequestLocalizationOptions>(options =>
    {
        var supportedCultures = new[]
        {
            new CultureInfo("sv-SE"),
            new CultureInfo("en-GB"),
            new CultureInfo("fr-FR")
        };

        // State what the default culture for your application is. This will be used if no specific culture
        // can be determined for a given request.
        options.DefaultRequestCulture = new RequestCulture("sv-SE"),
        // Explicitly state numbers/dates etc.
        options.SupportedCultures = supportedCultures;
        // Explicitly state UI strings (e.g. localised strings)
        options.SupportedUICultures = supportedCultures;
   }

Refer to https://docs.microsoft.com/en-us/aspnet/core/fundamentals/localization?view=aspnetcore-3.0#localization-middleware

Update:

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
        var locOptions = app.ApplicationServices.GetService<IOptions<RequestLocalizationOptions>>();
        app.UseRequestLocalization(locOptions.Value);
        //other middlewares
        app.UseMVc();
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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