Super frustrated with this one because I can get it working on a "Hello World" application but not my real application. Here's how I'm configured:
ConfigureServices:
services.AddLocalization(options => options.ResourcesPath = "Resources");
services.AddMvc(config =>
{
var policy = new AuthorizationPolicyBuilder()
.RequireAuthenticatedUser()
.Build();
config.Filters.Add(new AuthorizeFilter(policy));
}).AddViewLocalization(Microsoft.AspNetCore.Mvc.Razor.LanguageViewLocationExpanderFormat.Suffix)
.AddDataAnnotationsLocalization();
Configure:
IList<CultureInfo> supportedCultures = new List<CultureInfo>
{
new CultureInfo("en-US"),
new CultureInfo("es-ES"),
};
app.UseDefaultFiles();
app.UseRequestLocalization(new RequestLocalizationOptions
{
DefaultRequestCulture = new RequestCulture("en-US"),
SupportedCultures = supportedCultures,
SupportedUICultures = supportedCultures
});
app.UseStaticFiles();
app.UseSession();
app.UseAuthentication();
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Dashboard}/{action=Index}/{id?}");
});
_ViewImports.cshtml (added taghelpers nuget pkg)
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers.Localization
/Views/Account/Login.cshtml
@inject IViewLocalizer Localizer
@{
ViewData["Title"] = "Log in";
}
<h1>@Localizer["test"]</h1>
/Resources/Views/Account/Login.en-US.resx
"test" -> "result" mapping
But when I run my site, Localizer is just displaying the Key "test" and not "result"
Am I missing a config somewhere?
This appears to be an issue if your assembly name != default namespace. Made them match and things work as expected.
from the doc:
If your targeted class's namespace isn't the same as the assembly name you will need the full type name.