Decouple StringLocalizer / HtmlLocalizer from types to make Shared Resources possible #227
Description
Hi
On Dec 15 2015 DamianEdwards commented on #153
"You absolutely can get a resource of any name you like by using the IStringLocalizerFactory type directly. The IStringLocalizer is simply a convenience type that is opinionated about where it looks for resources (namely, it uses the name of the type T ). Same applies for IHtmlLocalizerFactory and IHtmlLocalizer . Also, IViewLocalizer is similar to the " T " types in that it's simply an opinionated way to find resources, in this case by using the view file's project relative path.
To do it manually:"
public class MyController : Controller
{
private readonly IStringLocalizer _localizer;
public MyController(IStringLocalizerFactory localizerFactory)
{
_localizer = localizerFactory.Create("Use.Whatever.Name.Here.You.Like");
}
public IActionResult Index()
{
ViewData["Message"] = _localizer["My default welcome message"];
}
}
However the IStringLocalizerFactory does not support Create("Use.Whatever.Name.Here.You.Like").
The interface is as follows:
//
// Summary:
// Represents a factory that creates Microsoft.Extensions.Localization.IStringLocalizer
// instances.
public interface IStringLocalizerFactory
{
//
// Summary:
// Creates an Microsoft.Extensions.Localization.IStringLocalizer using the System.Reflection.Assembly
// and System.Type.FullName of the specified System.Type.
//
// Parameters:
// resourceSource:
// The System.Type.
//
// Returns:
// The Microsoft.Extensions.Localization.IStringLocalizer.
IStringLocalizer Create(Type resourceSource);
//
// Summary:
// Creates an Microsoft.Extensions.Localization.IStringLocalizer.
//
// Parameters:
// baseName:
// The base name of the resource to load strings from.
//
// location:
// The location to load resources from.
//
// Returns:
// The Microsoft.Extensions.Localization.IStringLocalizer.
IStringLocalizer Create(string baseName, string location);
}
Please explain to me how the code presented as an example from DamianEdwards would work if the interface does not even support just strings but instead requires a Type?