AspNetCoreLocalization icon indicating copy to clipboard operation
AspNetCoreLocalization copied to clipboard

SqlStringLocalizer as DataAnnotationLocalizerProvider

Open haeberle opened this issue 6 years ago • 1 comments

First, cool work, very helpful!

But I'm struggling a bit to localize the MVC ViewModel, because I'd like to have the Fields in the database too:

[Required(ErrorMessageResourceName ="MissingEmailAddress", ErrorMessageResourceType = typeof(SharedResource))]
            [Display(Name = "Email", ResourceType = typeof(SharedResource))]
            [EmailAddress(ErrorMessageResourceName ="EmailAddressWrongFormat", ErrorMessageResourceType = typeof(SharedResource))]

How I have to e to configure it ? Thanks a lot

haeberle avatar Nov 25 '18 22:11 haeberle

@haeberle & @damienbod: I just stumbled upon this issue because I faced a similar one (using Localization.SqlLocalizer 3.1.0). For me, it was not possible to set the 'ResourceType' on the DisplayAttribute. It has always resulted in an 'InvalidOperationException' (as also described here).

There are two options to overcome this challenge:

  1. Skip setting a specific 'ResourceType'.
  2. Or, if you would like to use resource type 'SharedResource' for all of your data annotations, add the following code to your configuration (described here and here):
services.AddMvc()
        .AddDataAnnotationsLocalization(options => {
            options.DataAnnotationLocalizerProvider = (type, factory) =>
                factory.Create(typeof(SharedResource));
        });

Hope this reply helps anyone who faces similar issues in the future.

Best regards, Patrick

pbru87 avatar Aug 06 '23 19:08 pbru87