-
Notifications
You must be signed in to change notification settings - Fork 10.3k
Localize standard DataAnnotations in ASP.NET Core MVC #33073
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
We've moved this issue to the Backlog milestone. This means that it is not going to be worked on for the coming release. We will reassess the backlog following the current release and consider this item at that time. To learn more about our issue management process and to have better expectation regarding different types of issues you can read our Triage Process. |
Also, the entire model validation message pipeline should be improved. If we want to return translated error messages also for the automatic model binding validations (e.g. non-null properties, data types etc.), the workaround becomes very ugly. Just look at this convoluted piece of code I had to use as a workaround to have fully translated binding errors:
|
please do a "like" on the issue, if not, they will not treat it in the backlog... |
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.
services.AddRazorPages()
.AddXLocalizer<...>(ops =>
{
// Data annotation error messages
ops.ValidationErrors = new ValidationErrors
{
RequiredAttribute_ValidationError = "The {0} field is required.",
CompareAttribute_MustMatch = "'{0}' and '{1}' do not match.",
StringLengthAttribute_ValidationError = "The field {0} must be a string with a maximum length of {1}.",
// ...
};
// Model binding error messages
ops.ModelBindingErrors = new ModelBindingErrors
{
AttemptedValueIsInvalidAccessor = "The value '{0}' is not valid for {1}.",
MissingBindRequiredValueAccessor = "A value for the '{0}' parameter or property was not provided.",
// ...
};
// Identity Errors
ops.IdentityErrors = new IdentityErrors
{
DuplicateEmail = "Email '{0}' is already taken.",
DuplicateUserName = "User name '{0}' is already taken.",
// ...
}
});
{
"XLocalizerOptions": {
....
"ValidationErrors": {
"CompareAttribute_MustMatch": "'{0}' and '{1}' do not match. They should not be different!",
"CreditCardAttribute_Invalid": "The {0} field is not a valid credit card number.",
"CustomValidationAttribute_ValidationError": "{0} is not valid.",
...
},
"IdentityErrors": {
"DuplicateEmail": "Email '{0}' is already taken.",
"DuplicateUserName": "User name '{0}' is already taken. Please try another one.",
"InvalidEmail": "Email '{0}' is invalid.",
...
},
"ModelBindingErrors": {
"AttemptedValueIsInvalidAccessor": "The value '{0}' is not valid for {1}.",
"MissingBindRequiredValueAccessor": "A value for the '{0}' parameter or property was not provided.",
...
}
}
} |
great work, @LazZiya ! However when it comes to human translated messages, the actual solution is always to hardcode, and not use a community translated set of messages by language. You take the translation of "field" and in many languages it is translated like "meadow" or "pasture"... this is why I asked MS & Co to move to a stable solution for non-English users, not imposing them to hardcode the same text for each of them. |
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. |
We've moved this issue to the Backlog milestone. This means that it is not going to be worked on for the coming release. We will reassess the backlog following the current release and consider this item at that time. To learn more about our issue management process and to have better expectation regarding different types of issues you can read our Triage Process. |
Nice, in the backlog again 😒. |
Thanks for contacting us. We're moving this issue to the |
@sdudnic could please let me know what's the exact issue in the DataAnnotations localization? I remembered there was an issue with |
@hishamco I don't recall it well, but from description, could we translate a DataAnnotation via JSON localisation file? |
Localization APIs work well with Data Annotations, but the default implementation is using ResourceManager, but it's doable You might have a look to my library https://github.com/hishamco/My.Extensions.Localization.Json/ |
The 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 otherDataAnnotation
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
orres
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
Linked to
The text was updated successfully, but these errors were encountered: