6

How can I globally overload error messages getting ,for example, from [Required] attribute? Probably, I should change CultureInfo, but I can't find any tutorials. Language still English as default. Thank you!

2
  • what I know is, you could [Required(ErrorMessage = @"This is required")] but that'll be only in one language. Commented Aug 31, 2016 at 9:56
  • ErrorMessage working good and display any languages, but this is ugly to write same text all the time. Commented Aug 31, 2016 at 10:04

2 Answers 2

3

A first reference on globalization and localization of ASP.NET Core is https://docs.asp.net/en/latest/fundamentals/localization.html.

There you'll find information about how to translate data annotations and how to configure ASP.NET Core to select the appropriate language / CultureInfo for each request.

DataAnnotations error messages are localized with IStringLocalizer<T>. Using the option ResourcesPath = "Resources", the error messages in RegisterViewModel can be stored in either of the following paths:

Resources/ViewModels.Account.RegisterViewModel.fr.resx Resources/ViewModels/Account/RegisterViewModel.fr.resx

For built-in error messages for identity models (like "This email is already taken.) or basic validation texts you have to do some more configuration. I have documented some work in ASP.Net Core localization.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you for answer and link, hope asp.net core team will make it simpler in future, just like in asp.net mvc.
Unfortunately, no, they seem to have no intent of making it simpler. It baffles me all projects have to translate and globalize default validation logic themselves instead of having it out of the box. But all issues I found about that were closed without being done. I have tried opening a new one.
0

You can try using ErrorMessageResourceName and ErrorMessageResourceType, using resource files to store messages.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.