如何在blazor服务器端本地化验证消息(DataAnnotationsValidator)

时间:2020-01-09 08:00:25

标签: c# asp.net-core blazor blazor-server-side asp.net-core-localization

我正在最新版本的VS 2019中使用blazor 3.1。

到目前为止,我已经能够本地化页面标签(标题,表格字段等)。

ListEmployee.razor页上,我可以本地化表标题等。在AddEmplyeeValidation.razor页上,我可以本地化表单标签,但是在本地化验证消息时遇到问题。

对于Employee.cs文件的验证消息,验证消息在文件Resources/DataData.Employee.resx的{​​{1}}文件夹中定义,但这似乎不起作用。< / p>

Data.Employee.ar.resx

如何根据 using System.ComponentModel.DataAnnotations; namespace BlazorSPA1.Data { public class Employee { [MaxLength(50)] public string Id { get; set; } [Required (ErrorMessage ="Name is RRRequired")] [StringLength(20, ErrorMessage = "Name is too long.")] public string Name { get; set; } [Required] [StringLength(20)] public string Department { get; set; } [MaxLength(100)] public string Designation { get; set; } [MaxLength(100)] public string Company { get; set; } [MaxLength(100)] public string City { get; set; } } } 的语言从资源文件中加载验证消息?

AddEmployeForm

我已经阅读了几篇文章,并尝试了一些尝试,但似乎没有任何效果。

这是我的 @page "/addemployeeValidation" @inject NavigationManager NavigationManager @inject IEmployeeService EmployeeService @inject IStringLocalizer<AddEmployeeValidation> L <h2>Create Employee</h2> <hr /> <EditForm Model="@employee" OnValidSubmit="@CreateEmployee"> <DataAnnotationsValidator /> <ValidationSummary /> <div class="row"> <div class="col-md-8"> <div class="form-group"> <label for="Name" class="control-label">@L["Name"]</label> <input for="Name" class="form-control" @bind="@employee.Name" /> <ValidationMessage For="@(()=> employee.Name)" /> </div> <div class="form-group"> <label for="Department" class="control-label">@L["Department"]</label> <input for="Department" class="form-control" @bind="@employee.Department" /> </div> <div class="form-group"> <label for="Designation" class="control-label">@L["Designation"]</label> <input for="Designation" class="form-control" @bind="@employee.Designation" /> </div> <div class="form-group"> <label for="Company" class="control-label">@L["Company"]</label> <input for="Company" class="form-control" @bind="@employee.Company" /> </div> <div class="form-group"> <label for="City" class="control-label">@L["City"]</label> <input for="City" class="form-control" @bind="@employee.City" /> </div> </div> </div> <div class="row"> <div class="col-md-4"> <div class="form-group"> <input type="submit" class="btn btn-primary" value="Save" /> <input type="button" class="btn" @onclick="@Cancel" value="Cancel" /> </div> </div> </div> </EditForm> @code { Employee employee = new Employee(); protected async Task CreateEmployee() { await EmployeeService.CreateEmployee(employee); NavigationManager.NavigateTo("listemployees"); } void Cancel() { NavigationManager.NavigateTo("listemployees"); } } 代码:

Startup.cs

我使用以下示例进行本地化,但是未显示如何本地化错误消息: https://www.c-sharpcorner.com/article/localization-in-blazor-server/

供参考的文件夹结构图:

enter image description here

英语版本的资源文件示例,以同样的方式,我也有阿拉伯文件:

enter image description here

在下面的屏幕截图中,您将看到字段名称已从资源文件中正确提取,但是验证消息不起作用,仅以英文显示。

enter image description here

3 个答案:

答案 0 :(得分:9)

这是我的本地化数据注释错误消息的解决方案。我创建了两个资源文件,一个用于字段,另一个用于错误消息。

  • DisplayNameResource用于本地化字段
  • ErrorMessageResource用于本地化错误消息

enter image description here enter image description here enter image description here enter image description here

在视图模型类中,使用Display属性本地化字段名称。要指定资源文件,请使用ResourceType属性上的Display属性:

[Display(Name = "Address", ResourceType = typeof(DisplayNameResource))]

在验证属性上,使用ErrorMessageResourceNameErrorMessageResourceType指定资源文件:

[Required(ErrorMessageResourceName = "RequiredError", ErrorMessageResourceType = typeof(ErrorMessageResource))]

以下是完整示例:

public class SomeViewModel
{
    [Display(Name = "Address", ResourceType = typeof(DisplayNameResource))]
    [Required(ErrorMessageResourceName = "RequiredError", ErrorMessageResourceType = typeof(ErrorMessageResource))]
    [StringLength(256, ErrorMessageResourceName = "MaxLengthError", ErrorMessageResourceType = typeof(ErrorMessageResource))]
    public string Address { get; set; }

    [Display(Name = "Phone", ResourceType = typeof(DisplayNameResource))]
    [Required(ErrorMessageResourceName = "RequiredError", ErrorMessageResourceType = typeof(ErrorMessageResource))]
    [RegularExpression("^09([0-9]{9})$", ErrorMessageResourceName = "PhoneLengthError", ErrorMessageResourceType = typeof(ErrorMessageResource))]
    public string Phone { get; set; }

    [Display(Name = "Password", ResourceType = typeof(DisplayNameResource))]
    [Required(ErrorMessageResourceName = "RequiredError", ErrorMessageResourceType = typeof(ErrorMessageResource))]
    [StringLength(50, MinimumLength = 6, ErrorMessageResourceType = typeof(ErrorMessageResource), ErrorMessageResourceName = "MinxMaxLengthError")]
    public string Password { get; set; }

    [Display(Name = "ConfirmPassword", ResourceType = typeof(DisplayNameResource))]
    [Required(ErrorMessageResourceName = "RequiredError", ErrorMessageResourceType = typeof(ErrorMessageResource))]
    [StringLength(50, MinimumLength = 6, ErrorMessageResourceType = typeof(ErrorMessageResource), ErrorMessageResourceName = "MinxMaxLengthError")]
    [Compare("Password", ErrorMessageResourceName = "PasswordConfirmMisMatch", ErrorMessageResourceType = typeof(ErrorMessageResource))]
    public string ConfirmPassword { get; set; }
}

MaxLengthError的错误消息是{0} cannot be longer than {1} character,因此{0}将被替换为本地化的文件名,{1}将被替换为您在{属性256

答案 1 :(得分:1)

此问题曾被问过:

How to add ViewModel localization to Blazor?

我建议使用FluentValidation是更好的方法。这是我的Github仓库的链接,展示了它如何工作:

https://github.com/conficient/BlazorValidationLocalization

答案 2 :(得分:-1)

我没有尝试过!

在asp.net core的官方文档中,有一节介绍如何本地化DataAnnotations 也许您找到了clues there