damienbod / AspNetCoreLocalization

Localization.SqlLocalizer & ASP.NET Core MVC Localization Examples
http://damienbod.com/2015/10/21/asp-net-5-mvc-6-localization/
MIT License
251 stars 101 forks source link

SqlStringLocalizer as DataAnnotationLocalizerProvider #56

Open haeberle opened 5 years ago

haeberle commented 5 years ago

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

pbru87 commented 1 year ago

@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