c# - 如何在本地选择默认区域性 -(以及为什么不在 Azure 上运行时不选择)

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

我正在 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 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://learn.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();
    }

关于c# - 如何在本地选择默认区域性 -(以及为什么不在 Azure 上运行时不选择),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58272176/

相关文章:

c# - 脚本错误 : Not authorized to send Apple events to Terminal when running code on Visual Studio 2017 for Mac

c# 在多线程服务器中使用 Entity Framework

c# - 在页面部分或方法内实例化 ASP.NET 中的 C# 类

c# - System.ServiceModel.AddressAccessDeniedException:HTTP 无法注册 URL http::8080

c# - 找不到“Newtonsoft”

c# - 授权属性在 MVC 4 中不起作用

c# - 如何从 ABBBBBGG GGGRRRRR 两个字节格式中读取颜色分量?

c# - RegisterStartupScript 和 RegisterClientScriptBlock 的区别?

c++ - 使用 Visual Studio 为 x64 编译?

c# - 这个 .wixproj 文件的 $(WixTargetsPath) 属性在哪里设置?