Description
Summary
Currently, the logic of handling localization of DataAnnotations is different in blazor and aspnetcore:
- Blazor requires a compiled resources class which aspnetcore doesn't require
- Blazor needs to define ErrorMessageResourceName and ErrorMessageResourceType in DataAnnotations, whitch aspnetcore doesn't need to do so. In aspnectore, I can simply use ErrorMessage="xxx" and use AddDataAnnotationsLocalization in the startup to define the resources globally.
Motivation and goals
one advantage of blazor is that it can share some code between server side and the client. In most case, models with DataAnnotations and languages resource files have already be written on the server side. But because blazor use different logic to handle DataAnnotation localization, I can't share the model and resource files to blazor, I have to write it again in blazor...
Examples
At server side, I define model like this:
public class FrameworkUser
{
[Display(Name = "_Admin.Account")]
[Required(ErrorMessage = "Validate.{0}required")]
public string ITCode { get; set; }
}
and use some thing like this in the startup:
services.AddMvc()
.AddDataAnnotationsLocalization(options => {
options.DataAnnotationLocalizerProvider = (type, factory) =>
factory.Create(typeof(SharedResource));
});
aspnetcore will look for the SharedResource for "_Admin.Account", "Validate.{0}required", etc....
In blazor, AddDataAnnotationsLocalization dosenot work, and for wsam mode, there is no AddDataAnnotationsLocalization at all, I have to write the same model again only because of localization. the model in blazor is like this:
public class FrameworkUser
{
[Display(Name = "_Admin.Account",ResourceType = typeof(Resources.SharedResource))]
[Required(ErrorMessageResourceName = "Validate__0_required", ErrorMessageResourceType = typeof(Resources.SharedResource))]
public string ITCode { get; set; }
}
I also need add a default Resx file without a language suffix and make it compiled to a class. ( I don't need this file on server side)