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 ?

Omu
  • 69,856
  • 92
  • 277
  • 407
  • I described my approach here: http://stackoverflow.com/questions/19398691/mvc-localisation-from-the-database-that-covers-all-messages-required-displayna – Marc Thomann Oct 16 '13 at 08:37

2 Answers2

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.

Omu
  • 69,856
  • 92
  • 277
  • 407
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • 2
    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 message – Omu Sep 21 '10 at 08:43
  • @Darin Dimitrov could you please tell me more exactly where in System.Web.Mvc to look – Omu Sep 21 '10 at 10:02
  • 4
    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. – Darin Dimitrov Sep 21 '10 at 10:17
  • @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 keys – Omu Sep 21 '10 at 10:25
  • @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. – Darin Dimitrov Sep 21 '10 at 10:59
  • 1
    @Darin Dimitrov this is what you showed me that time: http://stackoverflow.com/questions/3156488/localize-default-model-validation-in-mvc-2 and it works, I thought that there could be some more keys in there to use for translation – Omu Sep 21 '10 at 11:02
  • Omu: did you find the other keys? – Eduardo Molteni Aug 19 '11 at 02:15
  • Is there an equivalent parameter for `[Display]` or do we simply avoid using `labelfor` and place the `@Resource` references in the views instead? – iCollect.it Ltd Oct 29 '13 at 14:42
  • ,ErrorMessageResourceType = typeof(Messages)) also needs to be specified in mvc 5 – Iman Jun 16 '18 at 16:48
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

Community
  • 1
  • 1
Mandeep Janjua
  • 15,583
  • 4
  • 29
  • 24