Home > OS >  DisplayNameFor doesn't lookup resx file despite IStringLocalizer and IHtmlLocalizer working cor
DisplayNameFor doesn't lookup resx file despite IStringLocalizer and IHtmlLocalizer working cor

Time:11-29

I've got my Login.cshtml page

@page
@using Microsoft.AspNetCore.Mvc.Localization
@using Microsoft.Extensions.Localization
@inject IStringLocalizer<LoginModel> localizer
@inject IHtmlLocalizer<LoginModel> htmlLocalizer
@model LoginModel

<h1>@htmlLocalizer["Title"]</h1> TRANSLATES WELL
<h1>@localizer["Title"]</h1> TRANSLATES WELL
[...]
div >
   <label asp-for="Input.RememberMe" >
        <input  asp-for="Input.RememberMe" />
        @Html.DisplayNameFor(m => m.Input.RememberMe) //DOESN'T WORK, DOESN'T LOOKUP RESX FILE
    </label>
</div>

In the scaffolded Login.cshtml.cs page model there is an attribute [Display] fr the RememberMe property, but this one is not getting translated despite translation being put in the same resource file

        /// <summary>
        ///     This API supports the ASP.NET Core Identity default UI infrastructure and is not intended to be used
        ///     directly from your code. This API may change or be removed in future releases.
        /// </summary>
        [Display(Name = "Remember")] //DOESN'T WORK, DOESN'T LOOKUP RESX FILE
        public bool RememberMe { get; set; }

We can see that the structure is correct

enter image description here

Resx file itself:

enter image description here

This is what gets rendered:

enter image description here

CodePudding user response:

Add data annotations localization in startup:

public void ConfigureServices(IServiceCollection services)
{
    services.AddMvc()
        .AddDataAnnotationsLocalization(options => {
            options.DataAnnotationLocalizerProvider = (type, factory) =>
                factory.Create(typeof(SharedResource));
        });
}

CodePudding user response:

This is Microsoft's bug which I reported: https://developercommunity.visualstudio.com/t/Custom-tool-cant-generate-designer-file/10213747?

Steps to reproduce:

Create ASP.Net Core MVC app Create resources directory Create resource file called name.pl.resx Add few keypairs Build Expected behaviour: Designer file is generate the resx file can be referenced as a type in the code.

Actual behaviour: Error: Custom tool PublicResXFileCodeGenerator failed to produce an output for input file…

Workaround: With each resx file edition

remove locale from its name Unload projectt Releoad project Clean Build Rename resx file to contain locale again

  • Related