How to localize the Display attribute in Asp.Net Core 1.0.0?

How can I localize the Name attribute in Display ? For instance:

 [Display(Name = "Library name")] public string LibraryName { get; set; } 

What should I do with this attribute?

+5
source share
2 answers

ASP.NET Core 1.0 does not support local localization based on the new embedded localization approach for the Display attribute. One way is to use the ASP.NET Core 1.0 approach before localization with resource files. I implemented a simple demo project that shows how to localize the display attribute here https://github.com/feradz/ASPNetCoreLocalization/wiki DataAnnotations.resx used to localize the Display attribute.

In this approach, the display name cannot contain special characters and spaces. For example, the display name may not be Library name , but may be LibraryName

 [Display(Name="LibraryName", ResourceType = typeof(Resources.DataAnnotations))] public string LibraryName { get; set; } 
+5
source

From .net framework 4.6.2, released in early August 2016, it has become much easier.

Save the view model class with annotations of the main languages ​​in English, for example.

 public class ContactInfo { [Required(ErrorMessage = "Your email address is invalid")] [Display(Name = "User Email")] public int Email { get; set; } [Required(ErrorMessage = "Your phone number is invalid")] [Display(Name = "User Phone")] public int Phone { get; set; } } 

Create an App_LocalResources and add resource files using the DataAnnotation.Localization.{locale}.resx , for example. for Chinese DataAnnotation.Localization.zh.resx or DataAnnotation.Localization.jp.resx for Japanese

enter image description here

Then for the English text annotations in your viewmodel like "Custom Phone"

  [Display(Name = "User Phone")] 

create an entry for the same English text in each language resource file that your site should support.

enter image description here

Read more about this change in .net 4.6.2

I excluded the definition of culture, which corresponds to the language file, as this can go in some detail. You can read about this in this blog post and in the ASP Documentation .NET site

-1
source

All Articles