Localization of the ASP.NET main screen

According to the documentation :

The runtime does not look for localized strings for attributes without validation. In the above code, "Email" (from [Display (Name = "Email")] will not be localized.

I am looking for a way to localize text in a DisplayAttribute. Any suggestions to do this right (s)?

+6
source share
4 answers

You can set ResourceType to DisplayAttribute , which you can use to localize your text.

Add the .resx resource to your project, for example. MyResources.resx and add a resource for your field:

enter image description here

Then specify the field name and type MyResources in DisplayAttribute

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

The localized resource will be extruded automatically (see text box)

enter image description here

Note : in RC2 there is an error that will throw a NotImplementedException if you use non-Latin characters in your resource values: https://github.com/aspnet/Razor/issues/760

+5
source

Having a Central location for all of your localization, be it browsing or data, is the best approach I can think of, and this is how I should work. In the Startup.cs file after installing the nuget packages for localization, add the following code

 services.AddMvc().AddViewLocalization().AddDataAnnotationsLocalization(options => options.DataAnnotationLocalizerProvider = (type, factory) => new StringLocalizer<Resources>(factory)); services.Configure<RequestLocalizationOptions>(options => { var cultures = new[] { new CultureInfo("en"), new CultureInfo("ar") }; options.DefaultRequestCulture = new RequestCulture("en", "en"); options.SupportedCultures = cultures; options.SupportedUICultures = cultures; }); 

Therefore, the DataAnnotationLocalizerProvider will be from Resources. {culture} .rex - (The resource file must have an access modifier No gen code ) - provided that the default language does not require any resources, and also have access to the resource file, since the code will not be created and must be created empty class with the same name.

and in _ViewImports.cshtml enter the following

 @inject IHtmlLocalizer<Resources> Localizer 

Having done this, you now have the global Localizer variable that will be used in any of the views for localization purposes.

This is how the central location for localizing strings

You can find more information about Globalization and Localization in ASP.NET Core

+3
source

In fact, I found a simple solution for followers. The display name is in most cases used in the label of the input field. Do this if you want:

 <label asp-for="Email">@Localizer["Email"]</label> 

@Html.DisplayNameFor course, you can pass the property name @Html.DisplayNameFor , but most of the time this already works well.

+2
source

I just created a project that demonstrates localization, including the localization of the Display attribute for class properties as well as enumerations.

The project can be found here https://github.com/feradz/ASPNetCoreLocalization/wiki

The Display attribute must be localized using an approach prior to ASP.NET Core 1.0. See the DataAnnotations.resx file in the project.

The Name Display property cannot contain empty spaces and special characters.

 [Display(Name = "NoSpacesAndSpecialChanractersHere", ResourceType = typeof(Resources.DataAnnotations))] public string FirstName { get; set; } 

ResourceType must be the fully qualified name of the resource class (i.e. including the namespace).

+1
source

All Articles