How to localize property name?<!-- --> | <!-- -->Patrick Desjardins Blog
Patrick Desjardins Blog
Patrick Desjardins picture from a conference

How to localize property name?

Posted on: April 30, 2012

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.

1public 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.

1public class Person {
2 [Display(ResourceType = typeof(ResourceFileTypeHere), Name="NamePropertyKey")]
3 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.