简体   繁体   English

如何在本地选择默认文化 - (以及在 Azure 上运行时为什么不选择)

[英]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.我正在构建然后在 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();
        });
    }

However, when I publish this to Azure and visit site, there is no default language engaged, it gives me all the @SharedLocalizer variable names.但是,当我将其发布到 Azure 并访问站点时,没有使用默认语言,它为我提供了所有@SharedLocalizer变量名称。 If I then select a language, everything works as intended so I'm left with two questions:如果我然后 select 一种语言,一切都按预期工作,所以我有两个问题:

1) When running my site in VS how / where is it setting the default culture? 1)在VS中运行我的网站时,它如何/在哪里设置默认文化?

2) How can I get my site to do this when published via Azure? 2) 通过 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?!我在我的家/索引 controller 中有这个方法,但这会根据用户选择设置语言 - 当我在 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 .您没有为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;
   }

Refer to https://docs.microsoft.com/en-us/aspnet/core/fundamentals/localization?view=aspnetcore-3.0#localization-middleware参考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();
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 运行 Azure DevOps 构建管道时自定义文化不适用 - Custom Culture not applying when running an Azure DevOps build pipeline 为什么 .NET“de-CH”文化数字组分隔符在本地和 Azure 上不同? - Why is .NET "de-CH" culture number group separator different locally and on Azure? C# Azure:本地运行时如何设置Azure超时和重试策略? - C# Azure: How to set Azure timeout and retry policy when running locally? Azure Vault 在本地运行 - Azure Vault Running Locally 默认的 Culture 和 UICulture 是如何确定的? - How is the default Culture and UICulture determined? 如何查找Culture的默认日期格式 - How to find the default dateformat of a Culture 如何将文化设置为用户为登录页面选择的文化? - How can I set the culture to the user selected culture for the login page? 默认文化 - Default Culture 如何将所选区域性传递给InitializeCulture()方法 - how to pass the selected culture into InitializeCulture() method 当应用程序部署为 Azure Web 应用程序时,OnTokenValidated 事件未触发,但在本地运行时触发 - OnTokenValidated event not firing when application is deployed as Azure Web App, but fires when running locally
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM