c# - Azure 上的 ASP.NET Core 本地化

标签 c# azure localization asp.net-core asp.net-core-mvc

我正在尝试强制本地化为瑞典语 (sv-SE)。一切都在本地运行,但部署到 Azure 后,验证错误始终以英语显示。

这是我在 Startup.cs 中的配置。

public void ConfigureServices(IServiceCollection services)
{
    ...

    services.AddLocalization();

    ...
}

public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerfactory)
    ...

    RequestLocalizationOptions localizationOptions = new RequestLocalizationOptions
    {
       SupportedCultures = new List<CultureInfo> { new CultureInfo("sv-SE") },
       SupportedUICultures = new List<CultureInfo> { new CultureInfo("sv-SE") }
    };

    app.UseRequestLocalization(localizationOptions, new RequestCulture("sv-SE"));

    ...
}

渲染 View 时,我可以看到 UI-Culture 和 Culture 在本地和 Azure 上都正确设置为 sv-SE。

发布到 Azure 时是否必须包含某些内容、本地化资源或其他内容?

我使用的是最新版本的 ASP.NET Core (RC1)。

最佳答案

基于this article ,首先需要将 RequestLocalizationMiddleware 设置为服务

services.Configure<RequestLocalizationOptions>(
    opts =>
    {
        var supportedCultures = new List<CultureInfo>
        {
            new CultureInfo("sv-SE")
        };

        opts.DefaultRequestCulture = new RequestCulture("sv-SE");
        // Formatting numbers, dates, etc.
        opts.SupportedCultures = supportedCultures;
        // UI strings that we have localized.
        opts.SupportedUICultures = supportedCultures;
    });

这还不够,还应该包括应用程序管道:

public void Configure(IApplicationBuilder app)  
{
    app.UseStaticFiles();

    var options = 
    app.ApplicationServices.GetService<IOptions<RequestLocalizationOptions>>();
    app.UseRequestLocalization(options.Value);

    app.UseMvc(routes =>
    {
         routes.MapRoute(
             name: "default",
             template: "{controller=Home}/{action=Index}/{id?}");
    });
}

关于c# - Azure 上的 ASP.NET Core 本地化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36126327/

相关文章:

azure - FineUploader 到 Azure 存储

c# - 如何通过单击操作按钮在 gridview 中设置填充复选框被选中(真)

c# - 最大化窗口的大小

c# - ViewModel 直接绑定(bind)到 Entity Framework 模型的副作用

java - 如何在 Java 中按照区域设置正确的顺序格式化日期和月份?

Laravel 5.2- 如何为现有的 url() 辅助函数添加前缀?

xcode - 如何跨多个目录使用 genstring?

C#,如何简单地改变类成员的顺序

azure - 如何修复 Azure DevOps 发布管道中的错误 "Error Code: ERROR_DESTINATION_INVALID"

azure - Application Insights 连续导出不导出性能指标