Skip to content

DataAnnotations Localization does not localize the pre-formatted validation error messages #72469

Open

Description

@natilivni

Is there an existing issue for this?

  • I have searched the existing issues

Describe the bug

When configuring DataAnnotation localization in the following manner:

 builder.AddDataAnnotationsLocalization(options =>
     options.DataAnnotationLocalizerProvider = (type, factory) =>
          factory.Create(typeof(SharedResource)))
);

The "out of the box" attributes only attempted to localize AFTER the error message has been formatted.

So if I have a class such as:

public class MyClass 
{
     [Required]
     public string MyProperty { get; set; }
}

And I validate the class under a seperate culture, the following mapping will not work:

The {0} field is required.  ---->   El campo {0} es obligatorio.

However, the following mapping will work:

The MyProperty field is required.  ---->   El campo MyProperty es obligatorio.

This is because of the following code in DataAnnotationsModelValidator:

var result = Attribute.GetValidationResult(validationContext.Model, context);
            if (result != ValidationResult.Success)
            {
                string errorMessage;
                if (_stringLocalizer != null &&
                    !string.IsNullOrEmpty(Attribute.ErrorMessage) &&
                    string.IsNullOrEmpty(Attribute.ErrorMessageResourceName) &&
                    Attribute.ErrorMessageResourceType == null)
                {
                    errorMessage = GetErrorMessage(validationContext) ?? result.ErrorMessage;
                }
                else
                {
                    errorMessage = result.ErrorMessage;
                }

Since Attribute.GetValidationResult(validationContext.Model, context) returns a formatted string and the `GetErrorMessage runs after this string is formated, we can never localize the generic error message.

Is there a plan to fix this or is there a work-around for such a scenario?

Thanks.

Expected Behavior

AddDataAnnotationsLocalization should allow mapping of the template string before it is formatted.

Steps To Reproduce

see above.

Exceptions (if any)

No response

.NET Version

net5.0

Anything else?

No response

Activity

  1. mkArtakMSFT commented on Jul 18, 2022

    @mkArtakMSFT

    Thank you for filing this issue. In order for us to investigate this issue, please provide a minimalistic repro project (ideally a GitHub repo) that illustrates the problem.

  2. natilivni commented on Jul 19, 2022

    @natilivni
    Author

    Hi. The problem is actually broader than I thought, the [Required] attribute does not even attempt to localize the error message at all. Please see the unit tests in this repo:

    https://github.com/natilivni/dataannotationsexample/blob/main/DataAnnotationsExample/TestProject1/UnitTest1.cs

    There are two unit tests. They both will fail to generate a Spanish error message in spite of the resource file being configured correctly. If seems like the localizer is only used if an explicit error message is set on a validation attribute.

  3. ghost added
    untriagedNew issue has not been triaged by the area owner
    on Jul 19, 2022
  4. added this to the Future milestone on Sep 6, 2022
  5. removed
    untriagedNew issue has not been triaged by the area owner
    on Sep 6, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions