0

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.

ygoe
  • 18,655
  • 23
  • 113
  • 210
  • Is this for Web API or MVC? – Michael Jul 06 '20 at 22:23
  • This is MVC. Web API doesn't use forms and validation. – ygoe Jul 07 '20 at 07:17
  • `My localisation tool does not use resource files (.resx) anywhere, it keeps texts in a different store.`.It seems you do not use the default localization in asp.net core.What is your localization tool and how did you use it? – Rena Jul 07 '20 at 09:10
  • It's self-built with a smarter editor than what Visual Studio can offer. It can be used in several ways, but only the API is relevant here. Call a method with a text key and additional data (like for `MaxLength` the length parameter) and you get the translated text in return. It's out of the scope of this question. – ygoe Jul 07 '20 at 16:23

0 Answers0