Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET Core IViewLocalizer Not pulling my dictionary look-up values

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?

like image 892
smurtagh Avatar asked Mar 02 '18 21:03

smurtagh


1 Answers

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.

like image 62
smurtagh Avatar answered Nov 05 '22 08:11

smurtagh