localize data annotations default messages ([Required] [StringLength] etc.)
Asked Answered
V

2

21

if I decorate the properties of my ViewModels with attributes like this:

public class Vm
{

[Required]
[StringLength(35)]
public string Name {get;set;}

}

I am going to get english validation messages:

"this field is required"
"The field Name must be a string with a maximum length of 35"

how could I translate them ?

Volar answered 21/9, 2010 at 8:28 Comment(1)
I described my approach here: #19399191Eponym
J
37

You could use the ErrorMessageResourceName property:

[Required(ErrorMessageResourceName = "SomeResource")]
[StringLength(30, ErrorMessageResourceName = "SomeOtherResource")]
public string Name { get; set; }

You may checkout this blog post for an example.


UPDATE:

In Application_Start:

DefaultModelBinder.ResourceClassKey = "Messages";

And in the Messages.resx file you need to add the custom error messages. Use Reflector to look at the System.Web.Mvc and System.ComponentModel.DataAnnotations assemblies in order to see the key names to use.

Jotting answered 21/9, 2010 at 8:32 Comment(9)
I would like to change the default messages without specifying it for each property, I saw once that you have to have a Messages.resx in your App_GlobalResources, but I don't know the keys for each messageVolar
@Darin Dimitrov could you please tell me more exactly where in System.Web.Mvc to lookVolar
Oh I see. You are trying to localize the default error messages. I afraid this is not possible unless you install a localized version of the .NET framework. I would recommend you specifying the error message in each attribute and have a custom resources file handling those messages.Jotting
@Darin Dimitrov no, your update was what I needed, I want to localize the validation messages, you showed me once one key PropertyValueInvalid, all I need to know now is where in System.Web.Mvc to find all these keysVolar
@Omu, what I showed you works only for some special things that are handled by ASP.NET MVC and not DataAnnotations like PropertyValueInvalid which is used for example when trying to bind a string to an integer value which is not of the correct format. You cannot do this with other messages like Required because they are handled by the DataAnnotations framework.Jotting
@Darin Dimitrov this is what you showed me that time: #3156988 and it works, I thought that there could be some more keys in there to use for translationVolar
Omu: did you find the other keys?Jaguarundi
Is there an equivalent parameter for [Display] or do we simply avoid using labelfor and place the @Resource references in the views instead?Ruffin
,ErrorMessageResourceType = typeof(Messages)) also needs to be specified in mvc 5Rule
H
10

There is a much better solution using asp.net MVC 3 these days incase someone is looking for a newer and far better approach.

http://blog.gauffin.org/2011/09/easy-model-and-validation-localization-in-asp-net-mvc3/

For example:

public class UserViewModel
{
    [Required(ErrorMessageResourceName = "Required", ErrorMessageResourceType = typeof(Resources.LocalizedStrings))]
    [LocalizedDisplayName(ErrorMessageResourceName = "UserId", ErrorMessageResourceType = typeof(Resources.LocalizedStrings))]
    [LocalizedDescription(ErrorMessageResourceName = "UserIdDescription", ErrorMessageResourceType = typeof(Resources.LocalizedStrings))]
    public int Id { get; set; }
}

SO related question - Mvc 3.0 DataAnnotations Localization

Hassler answered 19/1, 2013 at 22:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.