简体   繁体   中英

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. (ie One or more validation errors occurred. )

在此处输入图片说明

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM