ASP.NET Core DisplayAttribute Localization

David picture David · Jun 15, 2016 · Viewed 13.3k times · Source

According to the documentation:

The runtime doesn’t look up localized strings for non-validation attributes. In the code above, “Email” (from [Display(Name = "Email")]) will not be localized.

I'm looking for a way to localize text in DisplayAttribute. Any suggestions to do it in a proper way(s)?

Answer

Sock picture Sock · Jun 17, 2016

You can set the ResourceType on the DisplayAttribute which can be used to localize your text.

Add a resource .resx file to your project e.g. MyResources.resx, and add a resource for your field:

enter image description here

Then reference the name of the field and the MyResources type in your DisplayAttribute

[Display(Name = "RememberMe", ResourceType  = typeof(MyResources))]
public bool RememberMe { get; set; }

The localized resource will be pulled through automatically (see the text box)

enter image description here