Patrick Desjardins Blog
Patrick Desjardins picture from a conference

How to localize data attribute in MVC3?

Posted on: 2012-04-29

If you are using the Data Annotation with Asp.Net MVC you might need to localize the message from these annotation.

Here is an example:

public class Person { 
  [Required(ErrorMessage="FirstName is required")] 
  public string FirstName { get; set; } 
  //... 
} 

If you want to have the Data Annotation translated in many languages, you should use two others properties that are : ErrorMessageResourceType and ErrorMessageResourceName. This will let you specify the resource type and the resouce name which is the key of inside the resource file.

public class Person { 
  [Required(ErrorMessageResourceType=typeof(MyResourcesNameSpace.ResourcesFile), ErrorMessageResourceName="FirstNameRequiredKey")] 
  public string FirstName { get; set; } 
  //... 
} 

This way, you will have your application localized from the Model to the View without any problem.