【问题标题】:Localization in ASP.NET Core on AzureAzure 上 ASP.NET Core 中的本地化
【发布时间】:2016-03-21 08:43:08
【问题描述】:

我正试图强制我的本地化为瑞典语 (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"));

    ...
}

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

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

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

【问题讨论】:

  • 您的浏览器以接受语言发送什么?
  • 这是我的浏览器发送的内容:Accept-Language:en-US,en;q=0.8,nb;q=0.6,ru;q=0.4,sv;q=0.2。即使我使用请求参数 ?culture=sv-SE 它也会输出英语。看起来代码正在运行,但在 Azure 上它找不到瑞典本地化资源并输出英语。至少这是我的猜测。
  • 浏览器语言无关紧要,因为只有瑞典语是受支持的文化。您是否将瑞典资源文件部署到 Azure?
  • 我没有包含任何额外的资源。我应该在哪里包含哪些文件?
  • 你确定你得到英文版或本地化字符串的密钥吗?

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


【解决方案1】:

基于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?}");
    });
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-01-06
    • 2016-10-16
    • 1970-01-01
    • 1970-01-01
    • 2023-03-12
    • 2018-07-23
    • 1970-01-01
    • 2020-11-29
    相关资源
    最近更新 更多