How to localize data attribute in MVC3?<!-- --> | <!-- -->Patrick Desjardins Blog
Patrick Desjardins Blog
Patrick Desjardins picture from a conference

How to localize data attribute in MVC3?

Posted on: April 29, 2012

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:

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

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.

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

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