0

I am migrating an application from previous ASP.NET version to ASP.NET 5(vNext, MVC 6). Previously I localized forms with DisplayAttribute attached to ViewModel's properties:

[Required(ErrorMessageResourceName = "FieldIsRequired", ErrorMessageResourceType = typeof(Resources.Validation))]
[Display(Name = "UserName", ResourceType = typeof(Resources.Common))]
public string UserName { get; set; }

I added DataAnnotations service:

public void ConfigureServices(IServiceCollection services)
{
    services.AddMvc()
            .AddViewLocalization(options => options.ResourcesPath = "Resources/Views")
            .AddDataAnnotationsLocalization();
}

When I submit an invalid form, an error message gets localized (as specified in the [Required] attribute).

But trying to display the form, I got an exception (No public property "UserName" in the resource class), until I commented out [Display] attribute.

Seems like input labels can't be localized with [DisplayAttribute] anymore?

Thank you!

nativehr
  • 1,131
  • 6
  • 16
  • 1
    Do you have a resx file? Can you include that in your post as well? – Dealdiane Oct 20 '15 at 19:59
  • Yes, .resx file ia s part of the asp.net website project. I created a paste with it's content (as xml): http://pastebin.com/7h1Vhu7s. Generated class indeed has `internal` access modifiers (and I can't change it in a ASP.NET 5 project), but it works for validation error messages. – nativehr Oct 20 '15 at 20:46
  • It should work if you manually change the generated class' modifier to `public`. Keep an eye on this [issue](https://github.com/aspnet/Localization/issues/31) well as it's related to your question. – Dealdiane Oct 20 '15 at 21:41
  • So how should we work around this issue? http://stackoverflow.com/questions/37833661 – David Jun 15 '16 at 11:08
  • @nativehr can you give an example of how did you manage to get this working with the ErrorMessage-s? – Vladislav Sep 01 '16 at 13:06

1 Answers1

1

It is indeed gone. 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.

Update 20.03.2017:

Localization of non-validation attributes was re-enabled with the new .NET Core SDK, according to the updated documentation:

DataAnnotations error messages are localized with IStringLocalizer<T>. Using the option ResourcesPath = "Resources", the error messages in RegisterViewModel can be stored in either of the following paths:

  • Resources/ViewModels.Account.RegisterViewModel.fr.resx
  • Resources/ViewModels/Account/RegisterViewModel.fr.resx