Add new overloads to IStringLocalizerFactory to enable creating IStringLocalizer instances for a specific culture #58037
Labels
api-suggestion
Early API idea and discussion, it is NOT ready for implementation
area-mvc
Includes: MVC, Actions and Controllers, Localization, CORS, most templates
Background and Motivation
We used to have an API on
IStringLocalizer
that would enable creating an instance for a specific culture, but it was removed (see #7756) due to reports of confusion by implementors. In doing so, we removed any non-static way of creating a localizer for a specific culture, which is necessary in scenarios where the current culture isn't implicitly set by the execution context, e.g. sending emails from a background worker. We've received feedback that this scenario is quite common and the lack of a first-class way to get a culture-specific instance ofIStringLocalizer
without resorting to static manipulation is undesirable.Proposed API
Propose we add two new overloads with default implementations (to ensure the interface change isn't breaking) that allow creating
IStringLocalizer
instances for a specific culture:The implementation class
ResourceManagerStringLocalizerFactory
would be updated to implement these new methods to create instances ofResourceManagerStringLocalizer
using the specified culture.Usage Examples
Alternative Designs
ICultureAwareStringLocalizerFactory
that these methods are defined on but with no default implementation and that implementations have to add the culture-aware overloads for. Then consuming code would need to be updated to type-check for the new interface and if implemented, call the new methods on it, or the code could first attempt to resolve the new interface from DI, and if none is registered, fallback to the original interface and manually set the current culture before creating a localizer instance.NotImplementedException
rather than applying the current suggested workaround.Risks
The default implementation setting static properties on
CultureInfo
could be problematic if not done correctly resulting in "leaking" culture specifics onto the current thread, but this is the approach we currently recommend to customers anyway.The text was updated successfully, but these errors were encountered: