Skip to content
This repository has been archived by the owner on Nov 29, 2018. It is now read-only.

Decouple StringLocalizer / HtmlLocalizer from types to make Shared Resources possible #227

Closed
JoeOM opened this issue Mar 14, 2016 · 7 comments

Comments

@JoeOM
Copy link

JoeOM commented Mar 14, 2016

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?

@JoeOM
Copy link
Author

JoeOM commented Mar 14, 2016

This is also related to: dotnet/aspnetcore#1142

@DamianEdwards
Copy link
Member

You call the second overload. There are two levels of keys here to allow for flexibility in implementation.

  • The location maps to the name of some physical/logical store for sets of resources, e.g. a database name. It's fixed for all cultures.
  • The baseName maps to the name of a resource set and will be combined with the culture name to look up the appropriate resources from the store identified by location, e.g. a database table name.

With this information you then get back an IStringLocalizer which finally lets you look up a single string resource via its key.

Any implementation of IStringLocalizerFactory can choose to use baseName and location to represent whatever makes sense for the resource storage represented by the implementation.

For example, an implementation that looks up strings from files might use the location to represent a folder name and the baseName to represent the base file name, with the culture name being appended to the file name to find the right one, e.g:

SomeResources/  <- This would be the "location"
  Titles.dat    <- This would be the "baseName", without the extension probably
  Titles_en.dat
  Titles_fr.dat
  Categories.dat
  Categories_en.dat
  Categories_fr.dat
SomeMoreResources/
  etc.

@DamianEdwards
Copy link
Member

If you were more specifically asking how this would work given the default (and only) implementation of IStringLocalizerFactory right now (ResourceManagerStringLocalizerFactory) then of course you'll be constrained by what the ResourceManager API in .NET is designed to do. You can see the logic we've implemented to turn the location and baseName into values we can use to create a ResourceManager at https://github.com/aspnet/Localization/blob/dev/src/Microsoft.Extensions.Localization/ResourceManagerStringLocalizerFactory.cs#L90 so let that guide you if it's applicable.

@JoeOM
Copy link
Author

JoeOM commented Mar 16, 2016

Thanks Damian!

Mate, good luck with all these changes you guys are making with Core etc. Really looking forward to RC2! :-)

@DamianEdwards
Copy link
Member

@JoeOM thanks! I'm really looking forward to getting it in people's hands finally soon.

@hishamco
Copy link
Contributor

@Eilon is this still active, because what I have seen is a duplicate of #153

@Eilon Eilon added this to the 1.0.1 milestone May 25, 2016
@ryanbrandenburg
Copy link
Contributor

Seems like @JoeOM got his answer, closing this one out.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants