dotnet / aspnetcore

ASP.NET Core is a cross-platform .NET framework for building modern cloud-based web applications on Windows, Mac, or Linux.
https://asp.net
MIT License
35.21k stars 9.95k forks source link

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

Open liuliang-wt opened 3 years ago

liuliang-wt commented 3 years ago

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)

ghost commented 3 years ago

We've moved this issue to the Backlog milestone. This means that it is not going to be worked on for the coming release. We will reassess the backlog following the current release and consider this item at that time. To learn more about our issue management process and to have better expectation regarding different types of issues you can read our Triage Process.

glatzert commented 1 year ago

I'd like to see a simple and uniform way to do this as well.

The development story for blazor is another than the one for aspnet core and then there's ApiControllers which also seem to have a slightly different usage. Chiming in Client/Server shared models in blazor and it all get's messy, since they'll influence each other.

Optimally there would just be ErrorCodes on the DataAnnotation, that'll just be used as lookup everywhere the DataAnnotation is printed. Same for the field name, which might be part of a DataAnnotation.

najeeb-anwari commented 1 year ago

Hello guys I have the same issue with localization but I found this package. It is working but I also have another problem. How to pass a parameter to the localization string? I used to do this in Laravel framework in PHP easily like __(":resource added successfully!", ["resource" => "Student"]) In my localization files, I had localization for both the Student and ":resource added successfully!" strings.

AntMaster7 commented 10 months ago

I am having the same issue and look forward for this feature to be added to Blazor.

quiian commented 7 months ago

Yeah, this would be quite nice to have... :-\