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; 
});