簡體   English   中英

如何在本地選擇默認文化 - (以及在 Azure 上運行時為什么不選擇)

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

我正在構建然后在 Visual Studio 中運行我的 asp.net CORE 站點,一旦站點啟動,我就會加載我的本地化資源,設置為默認值,即英語。

    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();
        });
    }

但是,當我將其發布到 Azure 並訪問站點時,沒有使用默認語言,它為我提供了所有@SharedLocalizer變量名稱。 如果我然后 select 一種語言,一切都按預期工作,所以我有兩個問題:

1)在VS中運行我的網站時,它如何/在哪里設置默認文化?

2) 通過 Azure 發布時,如何讓我的網站執行此操作?

我在我的家/索引 controller 中有這個方法,但這會根據用戶選擇設置語言 - 當我在 VS 中運行它時它是如何自動發生的?!

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

您沒有為RequestLocalizationOptions設置DefaultRequestCulture

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;
   }

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

更新:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM