-
Notifications
You must be signed in to change notification settings - Fork 279
Localization of data annotation attributes in Localization.StarterWeb #187
Comments
This sample might not use enough of the MVC support for it to be visible. Look at samples/Mvc.LocalizationSample.Web instead. |
Thanks, I had a look and indeed it shows how to address some of my points, namely:
However, it does not show (and I don't know how to):
Also there is the problem of the compatibility with the front-end validation, which I think should be supported since it is implemented in the default project templates, but let's leave that aside for now as it seems like a different subject. |
The If you're seeing .NET
These messages are all retrieved from
services.AddMvc(options => options.ModelBindingMessageProvider.AttemptedValueIsInvalidAccessor =
(value, name) => $"Hmm, '{value}' is not a valid value for '{name}'."));
var stringLocalizer = _stringLocalizerFactory.Create(typeof(MvcDataAnnotationsLocalizationOptionsSetup));
options.ModelBindingMessageProvider.AttemptedValueIsInvalidAccessor =
(value, name) => stringLocalizer["The value '{0}' is not valid for {1}.", value, name];
if (context.Key.MetadataKind == ModelMetadataKind.Property)
{
var stringLocalizer = _stringLocalizerFactory.Create(typeof(DataAnnotationsMetadataProvider));
context.BindingMetadata.ModelBindingMessageProvider.AttemptedValueIsInvalidAccessor =
(value, name) => stringLocalizer[
$"The value '{{0}}' is not valid for properties of type '{context.Key.ModelType.FullName}' like {context.Key.Name}.",
value];
} (Thought we had samples using at least one of these. But I'm not finding anything.) |
Thanks for the detailed info! With your help I managed to get everything localized. |
Closing because there are no plans to make this sample change. |
I am looking for an example showing how to localize data annotation attributes. I feel like this should belong in the Localization.StarterWeb sample, but unfortunately it is not implemented. Here you can see English field names and validation messages when the French language is selected:
There are mostly 4 things that I would like to see:
Ideally, in a way that is compatible with implementing a custom IStringLocalizer to retrieve the translations...
I would be willing to submit a PR given some pointers.
Any help appreciated.
The text was updated successfully, but these errors were encountered: