I'm trying to get the IStringLocalizer
service instance inside a extension method, is it possible? Any suggestions on how should I inject it?
My goal here is to translate a type using its name as convention.
public static class I18nExtensions
{
private IStringLocalizer _localizer; // <<< How to inject it?
public static string GetName(this Type type)
{
return _localizer[type.Name].Value;
}
}
Following @NightOwl888 comment I was in the wrong path, I ended up creating the following service:
Credit: @NightOwl888
Why not just pass the
IStringLocalizer
as a parameter:The purpose of extension methods is to extend behavior of objects. It seems to me that is what you're trying to do here.