asp.net-coreasp.net-core-1.0displayattributeasp.net-core-localization

ASP.NET Core DisplayAttribute Localization


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)?


Solution

  • 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