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
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.
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 fieldsErrorMessageResource
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,...
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