-
Notifications
You must be signed in to change notification settings - Fork 10.6k
[Blazor] Data annotations localization support #12158
Copy link
Copy link
Open
Labels
Pillar: Technical DebtPriority:2Work that is important, but not critical for the releaseWork that is important, but not critical for the releaseaffected-fewThis issue impacts only small number of customersThis issue impacts only small number of customersarea-blazorIncludes: Blazor, Razor ComponentsIncludes: Blazor, Razor ComponentsenhancementThis issue represents an ask for new feature or an enhancement to an existing oneThis issue represents an ask for new feature or an enhancement to an existing onefeature-blazor-serverfeature-localizationfeature-validationIssues related to model validation in minimal and controller-based APIsIssues related to model validation in minimal and controller-based APIson-roadmapListed on the current release roadmapListed on the current release roadmapseverity-majorThis label is used by an internal toolThis label is used by an internal tooltriaged
Milestone
Metadata
Metadata
Assignees
Labels
Pillar: Technical DebtPriority:2Work that is important, but not critical for the releaseWork that is important, but not critical for the releaseaffected-fewThis issue impacts only small number of customersThis issue impacts only small number of customersarea-blazorIncludes: Blazor, Razor ComponentsIncludes: Blazor, Razor ComponentsenhancementThis issue represents an ask for new feature or an enhancement to an existing oneThis issue represents an ask for new feature or an enhancement to an existing onefeature-blazor-serverfeature-localizationfeature-validationIssues related to model validation in minimal and controller-based APIsIssues related to model validation in minimal and controller-based APIson-roadmapListed on the current release roadmapListed on the current release roadmapseverity-majorThis label is used by an internal toolThis label is used by an internal tooltriaged
Type
Fields
Give feedbackNo fields configured for Feature.
In my case, I have custom localization service (like
IStringLocalizer), that retrieve localization from database for user-specific culture. User culture configures in user settings, that may changes in application runtime, i.e. without page reload.Service registers as scoped, i.e. each user have own
ILocalizationServicewith own specific culture settings.In components this works great, just inject service.
Of course, this does not work in standard case of using data annotation attributes. For example:
Data annotation attributes can be provided with custom type with localized strings. Something like this:
Main problem of this approach is that localized strings in
LocalizationResourcesmust be as static properties. So we cant directly inject user-specificILocalizationService. But we can try get user-specificILocalizationServiceviaFunc<ILocalizationService> LocalizationServiceProviderthat can be configured, for example, inConfigure()method:Of course, this does not work, because of scope of
serviceProviderinConfigure()method differs from user-specific scopes. In order for this approach to work it is necessary inLocalizationResources.LocalizationServiceProvideruses some kind of context that will provide user-scopedIServiceProviderto resolve user-specificILocalizationService.So, at this moment, in server-side Blazor is exists any method to localize the data annotation attributes in non-resx scenarios? Have a plan to implement its in future?