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

Time:11-08

I followed 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.

CodePudding user response:

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.

  • Related