.NET Core 2.0 IViewLocalizer编写密钥而不是文本

时间:2018-06-14 07:04:00

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

我目前正在尝试在我的应用程序中构建一个本地化程序。阅读完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?}");
    });
}

资源和观点:

Folders

在视图中:

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

通过将参数LanguageViewLocationExpanderFormat.SubFolder添加到AddViewLocalization

也无法正常工作

1 个答案:

答案 0 :(得分:0)

我找到了解决方案:

  1. 查看Microsoft的示例:https://github.com/aspnet/Entropy/tree/dev/samples/Localization.StarterWeb
  2. 清理您的Nuget-Packages
  3. 快乐编码!