Localize standard DataAnnotations in ASP.NET Core MVC
See original GitHub issueThe problem
As far as I see from the docs, DataAnnotation localization asks additional resx files, or hardcoded translation strings in [Attributes], or specific code customization, that seem not really fair versus non-English projects.
In order to make "The XX field is required"
string to appear as "Le champ XX est obligatoire"
we need to write it hardcoded on all decorated with [Required]
properties, or write custom code to implement it on all the [Required]
properties, and also for all other DataAnnotation
attributes, that seem strange…
DataAnnotation
should be translated in a similar way another localized strings are, depend on the current culture, be available in localized .json
or res
files or similar, without the need for each developer to translate by itself the same “Is required” string with same text in their projects.
Further technical details
- ASP.NET Core version: 5.0.202
- VS 2019
Linked to
Issue Analytics
- State:
- Created 2 years ago
- Reactions:42
- Comments:12 (5 by maintainers)
Localization should be baked in. I’ve been spending ages replacing hard-coded strings in Identity razor pages. Millions of developers must have done that. What a waste of time.
Many developers prefers official solutions, but sometimes when the wait is too long, you will start developing it yourself or find a community solution.
I’ve developed a nuget (XLocalizer) for simplifing localization in all aspects including auto adding missing keys and online translation support.
In terms of validation there are three main categories:
Basically there is no need to provide any error message manually, XLocalizer will take care of localizing all automatically. But if you want to customize any error message you can do it simply in startup or json.
Option A : In startup
Option B - Json: