Is there an existing issue for this?
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
Is there an existing issue for this?
Describe the bug
When configuring DataAnnotation localization in the following manner:
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:
And I validate the class under a seperate culture, the following mapping will not work:
However, the following mapping will work:
This is because of the following code in
DataAnnotationsModelValidator: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
AddDataAnnotationsLocalizationshould 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