Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SqlStringLocalizer as DataAnnotationLocalizerProvider #56

Open
haeberle opened this issue Nov 25, 2018 · 1 comment
Open

SqlStringLocalizer as DataAnnotationLocalizerProvider #56

haeberle opened this issue Nov 25, 2018 · 1 comment

Comments

@haeberle
Copy link

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
Copy link

pbru87 commented Aug 6, 2023

@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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants