How to Localize validation message (DataAnnotationsValidator) in blazor server side

前端 未结 3 1619
小鲜肉
小鲜肉 2021-02-07 18:23

I am using blazor 3.1 in latest version of VS 2019.

So far, I am able to localize page labels (title, table fields etc.).

On the ListEmployee.razor

相关标签:
3条回答
  • 2021-02-07 18:39

    Here is my solution for localizing data annotation error messages. I create two resource files, one for fields and another one for error messages.

    • DisplayNameResource for localizing fields
    • ErrorMessageResource for localizing error messages

    In view model class use Display attribute for localizing field name. To specify resource file use ResourceType property on Display attribute:

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

    And on validation attributes use ErrorMessageResourceName and ErrorMessageResourceType to specify resource file:

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

    Here is full example:

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

    Error message for MaxLengthError is {0} cannot be longer than {1} character, so {0} will be replaced with localized filed name and {1} will be replaced with the 256 you specified on attribute [StringLength(256,...

    0 讨论(0)
  • 2021-02-07 18:51

    This has been asked before:

    How to add ViewModel localization to Blazor?

    I suggested that using FluentValidation would be a better approach. Here is a link to my Github repo that demonstrates how it could work:

    https://github.com/conficient/BlazorValidationLocalization

    0 讨论(0)
  • 2021-02-07 18:59

    I did not tried this out!

    In the offical docs of asp.net core there is section how to localize DataAnnotations Maybe you find some clues there.

    0 讨论(0)
提交回复
热议问题