Patrick Desjardins Blog
Patrick Desjardins picture from a conference

How to localize property name?

Posted on: 2012-04-30

If you are using Html Helper to generate your form you may end up with label with the property name into it. You may also have error message with the property name. The problem is if your object contain English term, you would prefer not to show them in English if you write something in French.

 public class Person { public string Name { get; set; } } 

The example above could display the word "Name" in a localized string, which is not what we want. But, if we use the Display data annotation attribute, it's possible to localize the property Name.

public class Person { 
  [Display(ResourceType = typeof(ResourceFileTypeHere), Name="NamePropertyKey")] 
  public string Name { get; set; } 
} 

This will search inside the resource specified the key inside the Name attribute. That's it, you have your property localized.