For an ASP.NET Core application I need a way to localise validation error messages for each request. Depending on the language set for the current user, different messages must be generated. My localisation tool does not use resource files (.resx) anywhere, it keeps texts in a different store. It can be queried through code.
I must avoid using custom validation attributes because developers on the team are used to them and may not consider custom attributes instead. So it's not an option to simply use something like [CustomRequired]
instead of the usual [Required]
. Namespace games (defining a custom RequiredAttribute
in another namespace) are also bad because mistakes are harder to find.
I've already found a similar question that doesn't have a satisfying answer for my situation. Its answer still depends on resource files. When I tried to adapt it to simply setting the ErrorMessage
property to my custom localised text, it failed because it's only executed once ever, so the texts were properly localised, but never updated for another request or user language.
I'm interested in the standard attributes like RequiredAttribute
, MaxLengthAttribute
, RangeAttribute
and EmailAddressAttribute
as well as model binding errors like when a value could not be converted into the model type. Basically any default English and technical message that might be thrown at the user by the framework.
Is it possible at all to properly and dynamically (per request, for each user's language) localise validation and model binding messages in current ASP.NET Core? And how should I do that?
Update: Looks like I can wait a very long time for this. The source code shows what's happening and the team has no interest to support per-request localisation of validation messages. Until then, I cannot use the built-in validation attributes but have to clone their implementation into my project to add that missing support.