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; }
}
4

1 回答 1

1

这不是 Blazor 的问题,您已在数据注释中硬编码错误消息。您需要从资源文件中提取本地化错误消息。

这篇 SO 帖子(如何为验证属性提供本地化验证消息)应该可以帮助您进行配置。

于 2019-06-08T15:36:04.460 回答