【问题标题】:.NET Core 2.0 IViewLocalizer write the key instead of the text.NET Core 2.0 IViewLocalizer 编写密钥而不是文本
【发布时间】:2018-06-14 07:04:00
【问题描述】:

我目前正在尝试在我的应用程序中构建本地化程序。阅读 Microsoft 文档后,我添加了所有必要的部分,因此理论上它应该可以工作。

但是在第一次启动和重新加载页面时,我只能看到按键而不是文本。

这是我的 Startup.cs

public void ConfigureServices(IServiceCollection services)
{
    //...

    services.AddLocalization(options => options.ResourcesPath = "Resources");

    services.AddMvc()
        .AddViewLocalization()
        .AddDataAnnotationsLocalization();

    services.Configure<RequestLocalizationOptions>(options =>
    {                
        var supported = new[]
        {
            new CultureInfo("en"),
            new CultureInfo("de"),
            new CultureInfo("fr")
        };
        options.DefaultRequestCulture = new RequestCulture(culture: "en", uiCulture: "en");
        options.SupportedCultures = supported;
        options.SupportedUICultures = supported;
    });
}

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
    app.UseStaticFiles();

    /...

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

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

资源和观点:

在视图中:

@using Microsoft.AspNetCore.Mvc.Localization
@inject IViewLocalizer Localizer
...
@Localizer["Views_Contract_my_key_on_this_page"]

将参数 LanguageViewLocationExpanderFormat.SubFolder 添加到 AddViewLocalization 也不起作用

【问题讨论】:

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


【解决方案1】:

我找到了解决办法:

  1. 查看来自 Microsoft 的示例:https://github.com/aspnet/Entropy/tree/dev/samples/Localization.StarterWeb
  2. 清理您的 Nuget 包

编码愉快!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-01-29
    • 2018-01-29
    • 2020-11-24
    • 2012-05-22
    • 1970-01-01
    • 2021-12-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多