Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Localize the title in data annotation error message in ASP.NET Core 5

I followed this official documentation for providing translation to data annotation constraints.

Everything works fine, except a title in the response that is not translated. You can see this title marked in the following picture. (i.e. One or more validation errors occurred.)

enter image description here

This title somehow is added automatically by .Net Core.

I don't know where is it or even if it is possible to localize it or not. You can see the error message related to the password is in German in the picture but title is not.

You can find the sample code here.

FYI: I even put an entry in my SharedResource.resx with the name One or more validation errors occurred., however, didn't work.

like image 834
MJBZA Avatar asked Jan 22 '26 16:01

MJBZA


1 Answers

It looks like this text cannot be translated. Here is a link to the source code: https://github.com/dotnet/aspnetcore/blob/a450cb69b5e4549f5515cdb057a68771f56cefd7/src/Http/Http.Extensions/src/HttpValidationProblemDetails.cs

private HttpValidationProblemDetails(Dictionary<string, string[]> errors)
{
    Title = "One or more validation errors occurred.";
    Errors = errors;
}

I would recommend you catch this error in your front end and provide the translated version directly there.

In most cases, APIs should not be providing the translated text anyway. They should be agnostic to language, and it's actually the UI that provides the translation.

like image 86
Neil Avatar answered Jan 24 '26 05:01

Neil



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!