1

I need to localize a data annotation error message in the Blazor. I have created a SharedResource class, but the validation summary returns the resource's key instead of the resource's value.

Italian Trulli

@page "/"
@using WebApplication1.Data
@using WebApplication1.Resources
@inject LocService SharedLocalizer




@SharedLocalizer.GetLocalizedHtmlString("Title")

<EditForm Model="@Movie" OnValidSubmit="@HandleValidSubmit">
    <DataAnnotationsValidator />
    <ValidationSummary />

    <InputText id="Title" bind-Value="@Movie.Title" />


    <button type="submit">Submit</button>
</EditForm>

@functions {
        private Movie Movie = new Movie();


    private void HandleValidSubmit()
    {
        Console.WriteLine("OnValidSubmit");
    }
}



public class Movie
{
    public int Id { get; set; }

    [Required(ErrorMessage = "titleRequired")]
    [EmailAddress]
    [Display(Name = "titleRequired")]
    public string Title { get; set; }
}
user3903484
  • 744
  • 2
  • 14
  • 24

1 Answers1

1

This is not an issue with Blazor, you have hardcoded your error message in the data annotation. You will need to pull your localised error message out of your resource files.

This SO post (How to provide localized validation messages for validation attributes) should help you with getting that configured.

Chris Sainty
  • 7,861
  • 2
  • 37
  • 59
  • [Simple Localisation in Blazor](https://chrissainty.com/simple-localisation-in-blazor/) post is also related. The article is written by a Blazor widely renowned expert. – dani herrera Jun 13 '19 at 20:18