Skip to content
This repository was archived by the owner on Dec 14, 2018. It is now read-only.

old style DataAnnotations Localization Attributes not working RC2 #4761

Closed
damienbod opened this issue May 27, 2016 · 23 comments
Closed

old style DataAnnotations Localization Attributes not working RC2 #4761

damienbod opened this issue May 27, 2016 · 23 comments
Assignees

Comments

@damienbod
Copy link

damienbod commented May 27, 2016

The old style of DataAnnotations Localization Attributes do not work in RC2. These should be removed or fixed.

Problem 2: how can you specify the Resource to use for the DataAnnotations? These models can be used in n controllers etc, so it should be possible to define the Resource.

Not working

[Required(ErrorMessageResourceName = "BoxLengthRequired", ErrorMessageResourceType = typeof(SharedResource))]
[Range(1.0, 100.0, ErrorMessageResourceName = "BoxLengthRange", ErrorMessageResourceType = typeof(SharedResource))]

working

[Required(ErrorMessage = "BoxLengthRequired")]
[Range(1.0, 100.0, ErrorMessage = "BoxLengthRange")]

Greetings Damien

@Eilon
Copy link
Member

Eilon commented May 27, 2016

@damienbod
Copy link
Author

damienbod commented May 27, 2016

@Eilon Thanks for your reply. Yes

 services.AddMvc()
                .AddViewLocalization()
                .AddDataAnnotationsLocalization();

The DataAnnotations Localization works, when I use the ErrorMessage property, but not ErrorMessageResourceName and ErrorMessageResourceType .

Greetings Damien

@Eilon
Copy link
Member

Eilon commented May 27, 2016

@damienbod ah, yes, in MVC it works best if you use ASP.NET Core's localization feature. The problem with using the ErrorMessageResourceName/Type is that by the time ASP.NET Core sees the message, the Data Annotations system has already localized the message (or not) and has already formatted in the values (e.g. "Le value 'abc123' is not le'valid 'integer"). But by leaving it all up to ASP.NET Core, it can make the right decision about loading resources, formatting, etc.

@damienbod
Copy link
Author

@Eilon Thanks, makes sense, will you fix this or remove it? It is also important to set the ResourceType in the attribute, because you should not have to duplicate localization fields in each controller, view, etc.

Greetings Damien

@Eilon
Copy link
Member

Eilon commented May 29, 2016

@DamianEdwards can you add your thoughts to this?

@NGancevich
Copy link

NGancevich commented Jun 5, 2016

Example would be great. I also can't get it worked.

I have created the following resx file

ViewModels.Account.RegisterViewModel.en.resx

and class

namespace Blablabla.ViewModels.Account
{
    public class RegisterViewModel
    {
        [Required(ErrorMessage = "The Email field is required.")]
        [EmailAddress(ErrorMessage = "The Email field is not a valid e-mail address.")]
        [Display(Name = "Email")]

        public string Email { get; set; }

        [Required]
        [StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 6)]
        [DataType(DataType.Password)]
        //[Display(Name = "Password")]
        public string Password { get; set; }

        [DataType(DataType.Password)]
        [Display(Name = "Confirm password")]
        [Compare("Password", ErrorMessage = "The password and confirmation password do not match.")]
        public string ConfirmPassword { get; set; }

        [Required(ErrorMessage = "The FirstName field is required.")]
        [StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 2)]
        [Display(Name = "FirstName")]
        public string FirstName { get; set; }

        //[StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 2)]
        //[Display(Name = "SecondName")]
        //public string SecondName { get; set; }

        [Required]
        [StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 2)]
        [Display(Name = "LastName")]
        public string LastName { get; set; }

        //[Required]
        //[StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 2)]
        //[Display(Name = "Phone")]
        //public string Phone { get; set; }

    }

@Eilon
Copy link
Member

Eilon commented Jun 6, 2016

@ryanbrandenburg do you know how to make this work? Do we have a sample or docs for this?

@ryanbrandenburg
Copy link
Contributor

@NGancevich I threw together an example of DataAnnotation localization running here. Have a look at that and see if you find the problem with your project based off it. If you're still having trouble after that please link to the whole project as a Github repo.

@NGancevich
Copy link

Based on you example I just managed to localize ErrorMessage for en localization (ViewModels.Account.LoginViewModel.resx). For ru (It doesn't work ViewModels.Account.LoginViewModel-ru.resx). Also in you example property localization is missed. For instance how to localize word "Password". Thanks

@NGancevich
Copy link

One more note. in the public documentation https://docs.asp.net/en/latest/fundamentals/localization.html#dataannotations-localization, section: DataAnnotations localization there is paragraph:

Resources/ViewModels.Account.RegisterViewModel.fr.resx
Resources/ViewModels/Account/RegisterViewModel.fr.resx

But in your test project you have named resource files as -Fr.resx. Accordion to the doc it should be dot delimiter instead of -

@DamianEdwards
Copy link
Member

This worked when I created the sample at https://github.com/aspnet/Entropy/tree/dev/samples/Localization.StarterWeb

Can you take a look at that project and compare? @ryanbrandenburg did you confirm in your example that it works for non-default languages?

The DisplayName attribute (and other non-validation attributes) do not currently get localized, due to #3518

@damienbod
Copy link
Author

@DamianEdwards What's the plan with ErrorMessageResourceName and ErrorMessageResourceType, do you plan to fix this or remove it?

Greetings Damien

@DamianEdwards
Copy link
Member

@damienbod they still work the way they always have. It's part of .NET itself (it's in the .NET Framework and the .NET Core packages) so it can't be removed. The support in MVC is to run the message through the new IStringLocalizer system before it's rendered.

@damienbod
Copy link
Author

OK, thanks

@NGancevich
Copy link

One more possible bug

[StringLength(100, ErrorMessage = "The Password must be at least {2} characters long.", MinimumLength = 6)]

On the interface I got

The Password must be at least 100 characters long.
but should be 6 characters long instead

Value in resx file:
The Password must be at least {2} characters long.

@dougbu
Copy link
Member

dougbu commented Jun 7, 2016

@NGancevich please open a separate bug for the [StringLength] issue. That's an issue specifically in the StringLengthAttributeAdapter and it fortunately has a one-line fix.

@ryanbrandenburg
Copy link
Contributor

@DamianEdwards and @NGancevich I am able to confirm that DataAnnotations work for non-default languages. And yes @NGancevich you're correct, it's .'s not -, not quite sure what lead me to put it the other way but good catch. I updated my example branch with that fix.

@ryanbrandenburg
Copy link
Contributor

@NGancevich are you all solved here other than the issue @dougbu asked you to open separately?

@NGancevich
Copy link

No more issues except [StringLength] attribute . I also expected Localization for Display attributed worked (as an example ....) but I understood that is future work.

@ryanbrandenburg
Copy link
Contributor

In that case I'm closing one.

@damienbod
Copy link
Author

damienbod commented Jun 14, 2016

@ryanbrandenburg
ErrorMessageResourceName and ErrorMessageResourceType still don't work in an ASP.NET Core MVC application.

These are part of the .NET Core and should be supported by ASP.NET Core localization.

@DamianEdwards
Copy link
Member

@damienbod they should work as they always have, they are totally separate to the new IStringLocalizer system and require that you name the resource keys correctly.

@damienbod
Copy link
Author

@DamianEdwards ,thanks for the reply. I'll have a look at this again, I kept getting exceptions the last time I tried in RC2.

Greetings Damien

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

No branches or pull requests

6 participants