5

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?

smurtagh
  • 490
  • 4
  • 16

2 Answers2

4

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.

smurtagh
  • 490
  • 4
  • 16
0

Just seeing this briefly on mobile, but on this localization docs page I saw:

The localization middleware must be configured before any middleware which might check the request culture (for example, app.UseMvcWithDefaultRoute()).

It looks like on your example above, you’re using request localization before using Mvc, which would mean in the middleware pipeline, the culture likely isn’t set yet.

Let me know if this helps; interested as to whether that’s the case.

SeanKilleen
  • 8,202
  • 14
  • 69
  • 124
  • Tried moving the middleware registration before and after the UseMvc() and it did not make a difference. – smurtagh Mar 03 '18 at 19:58
  • Also did sniff the culture of the thread on my controller to make sure it's set correctly and it is indeed set to "en-US". – smurtagh Mar 03 '18 at 20:13