Skip to content

can blazor use the same logic to handle dataAnnotation localization as aspnetcore?Β #29804

@liuliang-wt

Description

@liuliang-wt

Summary

Currently, the logic of handling localization of DataAnnotations is different in blazor and aspnetcore:

  1. Blazor requires a compiled resources class which aspnetcore doesn't require
  2. 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)

Metadata

Metadata

Assignees

No one assigned

    Labels

    affected-fewThis issue impacts only small number of customersarea-blazorIncludes: Blazor, Razor Componentsdesign-proposalThis issue represents a design proposal for a different issue, linked in the descriptionenhancementThis issue represents an ask for new feature or an enhancement to an existing onefeature-localizationseverity-majorThis label is used by an internal tool

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions