0

I have gone through quite a bit of SO questions like:

Localise Display DataAnnotation without the Name Attribute in .NET Core 5 ViewModel

Display Name is problem on Data Annotation ErrorMessage (The {0} field is required.) with Localization

and examples such as these:

https://medium.com/@hoda_sedighi/localize-validation-error-message-using-data-annotation-in-asp-net-boilerplate-fbd4a0371f2e

but I'm unable to solve the problem I have.

My DTO exists in a separate assembly, Dto.dll. I have this DTO for user signup:

/// <summary>
/// Gets or sets the account e-mail.
/// </summary>
[Required(ErrorMessageResourceType = typeof(Strings), ErrorMessageResourceName = nameof(Strings.EmailValidation))]
[EmailAddress(ErrorMessageResourceType = typeof(Strings), ErrorMessageResourceName = nameof(Strings.EmailFormatValidation))]
[StringLength(DtoConstants.EmailMaxLength, MinimumLength = DtoConstants.EmailMinLength)]
[Display(ResourceType = typeof(Strings), Name = nameof(Strings.EmailName))]
public string Email { get; set; }

/// <summary>
/// Gets or sets the user's password.
/// </summary>
[Required(ErrorMessageResourceType = typeof(Strings), ErrorMessageResourceName = nameof(Strings.PasswordValidation))]
[StringLength(DtoConstants.PasswordMaxLength, ErrorMessageResourceType = typeof(Strings), ErrorMessageResourceName = nameof(Strings.PasswordLengthValidation), MinimumLength = DtoConstants.PasswordMinLength)]
[DataType(DataType.Password)]
[Display(ResourceType = typeof(Strings), Name = nameof(Strings.PasswordName))]
public string Password { get; set; }

/// <summary>
/// Gets or sets the user's confirm password.
/// </summary>
[Required(ErrorMessageResourceType = typeof(Strings), ErrorMessageResourceName = nameof(Strings.ConfirmPasswordValidation))]
[Compare(nameof(Password), ErrorMessageResourceType = typeof(Strings), ErrorMessageResourceName = nameof(Strings.PasswordsDoNotMatch))]
[DataType(DataType.Password)]
[Display(ResourceType = typeof(Strings), Name = nameof(Strings.ConfirmPasswordName))]
public string ConfirmPassword { get; set; }

All the Strings.* resources are in a Strings.resx file in the same assembly.

My Web API is in a different assembly, webapi.dll, and I have this in my Program.cs:

builder.Services.AddControllers()
    .AddDataAnnotationsLocalization()
    .AddViewLocalization();

And when I send in a bad request using Swagger, ModelState validation automatically kicks in, and sends this response.

Notice that I get all the error messages correctly (the ones shown with white tick marks), as defined in the Strings.resx (in the dto.dll), but the "Display Name" of the property is not being returned (the one with white X marks). "ConfirmPassword" should be localized as "Confirm Password" and "Password" should be localized as "Secret" (that's what I have defined in Strings.resx)

enter image description here

Does anyone know why data annotations localization is working for validation error messages, but not for display name of the properties?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Shankar
  • 1,634
  • 1
  • 18
  • 23
  • Do you mean we should have `"Secret":["xxx"],"Confirm Password":[xxxx]` if everything worked as expected? And `[Required(ErrorMessageResourceType = typeof(Strings), ErrorMessageResourceName = nameof(Strings.PasswordValidation))]` can be translated to `[Required(ErrorMessageResourceType = typeof(Strings), ErrorMessageResourceName = "Secret")]` ? – Tiny Wang Aug 28 '23 at 10:07
  • and you used `Boilerplate` library? – Tiny Wang Aug 28 '23 at 10:19
  • Not sure I follow. In Strings.resx file: PasswordName = "Secret", ConfirmPasswordName = "Confirm Password". The display attribute should print these as property names, which it is not. ErrorMessageResourceName is PasswordValiation, which is set to "Password cannot be left empty." in the Strings.resx file, which is being printed correctly. Changing the ErrorMessageResourceName to "Secret" will not affect the property name. The property name is coming from the DTO and is not being localized. I'm simply using the System.ComponentModel.DataAnnotations namespace, no customization. @TinyWang – Shankar Aug 28 '23 at 19:34
  • Hi, because there's no other community members following this question, so I'm afraid it's hard to point the issue out directly now. Is it possible to share some more code snippet to allow to reproduce the issue and troubleshoot, or at least let us know what nuget packages you are using. Per my searching the localization looks to use for scenario e.g. changing language, and the link you shared might using some specific package for custom reqirement. I'm not very sure about it as well. – Tiny Wang Aug 29 '23 at 05:54

0 Answers0