0

我遇到了一个奇怪的问题,并未在ASP.NET Core 1中发生。由于某种原因,如果我使用Index.es.cshtml该视图已正确找到,但如果我使用Localizer["Home"]Index.cshtml它不会被翻译。重复每种语言的观点并不是很理想。视图已本地化,但没有找到资源

public void ConfigureServices(IServiceCollection services) 
{ 
    var supportedCultures = new[] { new CultureInfo("en"), new CultureInfo("es") }; 

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

    services.Configure<RequestLocalizationOptions>(options => 
    { 
     options.DefaultRequestCulture = new RequestCulture("en"); 
     options.SupportedCultures = supportedCultures; 
     options.SupportedUICultures = supportedCultures; 
    }); 

    services.AddMvc() 
     .AddViewLocalization(LanguageViewLocationExpanderFormat.Suffix, options => options.ResourcesPath = "Resources") 
     .AddDataAnnotationsLocalization(); 
} 

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

查看路径:

~/Views/Home/Index.cshtml 

资源路径:

~/Resources/Views/Home/Index.es.resx 
Home => Inicio 

所以,我预计这个工作,因为文化已正确设置为es

@inject IViewLocalizer Localizer 
@{ 
    ViewData["Title"] = Localizer["Home"]; 
} 

回答

0

看来干净重建项目解决了这个问题。我有这个工作的最终配置:

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

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


services.Configure<RequestLocalizationOptions>(options => 
{ 
    var supportedCultures = new[] { new CultureInfo("en"), new CultureInfo("es") }; 

    options.DefaultRequestCulture = new RequestCulture(culture: "en", uiCulture: "en"); 
    options.SupportedCultures = supportedCultures; 
    options.SupportedUICultures = supportedCultures; 
});