You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
DataAnnotations Localization does not localize the pre-formatted validation error messages #72469
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.
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.
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:
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.
Activity
mkArtakMSFT commentedon Jul 18, 2022
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.
natilivni commentedon Jul 19, 2022
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.
[release/11.0] Add message template formatting to DataAnnotations val…