-
Notifications
You must be signed in to change notification settings - Fork 2.1k
old style DataAnnotations Localization Attributes not working RC2 #4761
Comments
@damienbod did you call See https://github.com/aspnet/Entropy/blob/dev/samples/Localization.StarterWeb/Startup.cs#L61. |
@Eilon Thanks for your reply. Yes
The DataAnnotations Localization works, when I use the ErrorMessage property, but not ErrorMessageResourceName and ErrorMessageResourceType . Greetings Damien |
@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. |
@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 |
@DamianEdwards can you add your thoughts to this? |
Example would be great. I also can't get it worked. I have created the following resx file
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; }
} |
@ryanbrandenburg do you know how to make this work? Do we have a sample or docs for this? |
@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. |
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 |
One more note. in the public documentation https://docs.asp.net/en/latest/fundamentals/localization.html#dataannotations-localization, section: DataAnnotations localization there is paragraph:
But in your test project you have named resource files as -Fr.resx. Accordion to the doc it should be dot delimiter instead of - |
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 |
@DamianEdwards What's the plan with ErrorMessageResourceName and ErrorMessageResourceType, do you plan to fix this or remove it? Greetings Damien |
@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 |
OK, thanks |
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. Value in resx file: |
@NGancevich please open a separate bug for the |
@DamianEdwards and @NGancevich I am able to confirm that DataAnnotations work for non-default languages. And yes @NGancevich you're correct, it's |
@NGancevich are you all solved here other than the issue @dougbu asked you to open separately? |
No more issues except [StringLength] attribute . I also expected Localization for Display attributed worked (as an example ....) but I understood that is future work. |
In that case I'm closing one. |
@ryanbrandenburg These are part of the .NET Core and should be supported by ASP.NET Core localization. |
@damienbod they should work as they always have, they are totally separate to the new |
@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 |
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
working
Greetings Damien
The text was updated successfully, but these errors were encountered: