【问题标题】:How is a default culture selected locally - (and why not when running on Azure)如何在本地选择默认文化 - (以及在 Azure 上运行时为什么不选择)
【发布时间】:2020-02-04 21:41:45
【问题描述】:

我正在构建并在 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 变量名称。如果我然后选择一种语言,一切都会按预期进行,所以我有两个问题:

1) 在 VS 中运行我的网站时,它如何/在哪里设置默认文化?

2) 如何让我的网站在通过 Azure 发布时执行此操作?

我的 home/index 控制器中有这个方法,但这会根据用户选择设置语言 - 当我在 VS 中运行它时它是如何自动发生的?!

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

【问题讨论】:

    标签: c# asp.net visual-studio azure asp.net-core


    【解决方案1】:

    您没有为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();
        }
    

    【讨论】:

    • 这没有任何区别。在 Visual Studio 上运行给了我“en-GB”(这已经发生了)并且发布到 Azure 仍然给了我 sharedLocaliser 变量(即不将文化设置为“en-GB”)
    • @jamheadart 你的Configure 方法是什么样的? app.UseRequestLocalization 必须放在app.UseMvc 之前参考dotnetcoretutorials.com/2017/06/22/request-culture-asp-net-core
    • 我没有app.UseRequestLocalisation - 如果我使用它,那么我根本无法改变我的文化,它总是停留在en-GB。我在我的启动中使用services.AddMvc().AddViewLocalization();
    • @jamheadart 我更新我的回复使用app.UseRequestLocalization,参考herehere
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-01-25
    • 2011-11-29
    • 2012-05-16
    • 2012-11-13
    • 1970-01-01
    • 2017-11-02
    • 2017-09-22
    相关资源
    最近更新 更多