Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 768
sids Avatar asked Jun 15 '16 11:06

sids


People also ask

What is Globalisation and Localisation in asp net?

Globalization is the process of designing the application in such a way that it can be used by users from across the globe (multiple cultures). Localization, on the other hand, is the process of customization to make our application behave as per the current culture and locale.

What is ASP localize?

<asp:Localize> is used to specify a Resource defined item, which forces the IDE to display some specified text, and still allows it to resolve at runtime, to the language of the website. This may be useful for the development of a site where the content of the site is actually in a different language.


1 Answers

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

like image 157
Sock Avatar answered Sep 28 '22 04:09

Sock