5

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!

Ralf Bönning
  • 14,515
  • 5
  • 49
  • 67
loqie
  • 51
  • 1
  • 4
  • what I know is, you could `[Required(ErrorMessage = @"This is required")]` but that'll be only in one language. – Matthias Burger Aug 31 '16 at 09:56
  • `ErrorMessage` working good and display any languages, but this is ugly to write same text all the time. – loqie Aug 31 '16 at 10:04

2 Answers2

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.

Community
  • 1
  • 1
Ralf Bönning
  • 14,515
  • 5
  • 49
  • 67
  • Thank you for answer and link, hope asp.net core team will make it simpler in future, just like in asp.net mvc. – loqie Aug 31 '16 at 12:31
0

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

apaderno
  • 28,547
  • 16
  • 75
  • 90