Localization integration
See original GitHub issueFirst of all a big thank you for maintaining this package. I am a big fan 😃
At the moment, however, I struggle, with the integration of string localization with the middleware. Ideally I would like to use IStringLocalizer to provide localized error messages. At the moment though we are using the middleware to intercept all errors and transform them in standardized ProblemDetails objects for our clients (including some enrichment with other properties). While I could throw the errors with localized strings, this would mean I would have to inject IStringLocalizer everywhere I throw an error. Is it possible somehow to incorporate this into the configuration of the AddProblemDetails middleware? Right now I have the following approach in my startup.cs
services.AddProblemDetails(options =>
{
options.Map<BackendException>((ctx, ex) =>
{
var provider = services.BuildServiceProvider();
var localizerFactory = provider.GetService<IStringLocalizerFactory>();
var localizer = new StringLocalizer<MyResources>(localizerFactory);
return new BackendProblemDetails
{
Title = string.Format(localizer[ex.ErrorIdentifier], ex.Parameters),
Type = ex.GetType().ToString(),
Status = (int)ex.StatusCode,
...
};
});
});
However, the string localizer will not be injected with the desired culture 😦
Any ideas would be greatly apprecieated. Thanks / Mark
Issue Analytics
- State:
- Created 2 years ago
- Comments:5 (3 by maintainers)
Hmm, that’s weird 🤔
The mapping runs in the request pipeline (where
UseProblemDetails
is called), so it requiresUseRequestLocalization
to be called beforeUseProblemDetails
in order for the localization to be properly set up.I can see if I’m able to reproduce it when back behind a keyboard (currently on vacation 😅)
I do use the official middleware for localization which works just fine at other parts of the code (e.g. within controllers) but not when setting up the PorblemDetails mapping in startup.