runtime icon indicating copy to clipboard operation
runtime copied to clipboard

Support creating a file-based ResourceManager on .NET Core

Open DamianEdwards opened this issue 11 years ago • 12 comments

.NET Framework supports the creation of a file-based ResourceManager via the static method ResourceManager.CreateFileBasedResourceManager. We'd like to have this available in .NET Core for ASP.NET scenarios (e.g. deploying site as source).

DamianEdwards avatar Feb 19 '15 20:02 DamianEdwards

Note, this is fine to be a new type/package rather than on the ResourceManager contract. We just need the functionality.

DamianEdwards avatar Feb 19 '15 21:02 DamianEdwards

There's a couple of reasons why we'd probably want to make this a separate package:

  1. On WinRT-based platforms, ResourceManager calls through WinRT APIs so that it can read from PRIs instead of ResX-based resources. We probably wouldn't light it up for them. If we did, what would it do? Read resx when the rest of the platform is PRI? Read PRI? Who knows.
  2. From a layering perspective, we consider our file system APIs significantly higher than ResourceManager. ResourceManager is used by nearly everything in .NET Core, so adding a dependency on file system would result in everything having a dependency on it (and introducing a number of cycles).

@DamianEdwards I do have a question:

Internally, you stated

We think the option of deploying resx files “as is” rather than have them create satellite assemblies is an attractive option for our scenarios

I don't understand that statement. The CreateFileBasedResourceManager method from .NET Framework doesn't create a ResourceManager around a raw ".resx", but rather around the binary resource format (ie ".resources"). You cannot copy a resx and expect it to open; you'll need to first write it to disk using ResourceWriter and then load that.

If you want to be able to deploy resx and then have ASP.NET pick it up, perhaps a better approach might be an API that takes a Stream? That way, you detect the edit to resx, load it, pass it to ResourceWriter(Stream), and then ultimately onto ResourceManager? Or an API that takes a raw ResX and wraps a ResourceManager around it?

davkean avatar Feb 20 '15 06:02 davkean

@davkean Separate pacakge is fine. Yeah I just misspoke on the resx file thing :smile:

You can see a working example of what we have in mind at https://github.com/DamianEdwards/i18nStarterWeb

It's compiling the resx files to resource files on disk during our precompilation stage and then loads them straight from there.

DamianEdwards avatar Feb 20 '15 08:02 DamianEdwards

Okay, thanks.

davkean avatar Feb 20 '15 16:02 davkean

@davkean I quite like the idea of a Stream based ResourceManager API, such that we could avoid hitting the disk when changing from .resx -> .resource. Then we really could just deploy resx files without any precompile or other such steps.

DamianEdwards avatar Feb 20 '15 23:02 DamianEdwards

I spent some time thinking more about this, and I don't think the Stream approach will be what you want. Today if you call CreateFileBasedResourceManager you don't pass an actual resources file, but a base directory which dictates from where we would start probing for resources. If we had a method that created a ResourceManager from a stream, then you would basically be opt'ing out of probing - which is probably not want you want.

Thoughts?

// cc @ramarag

davkean avatar Mar 04 '15 18:03 davkean

I was thinking it would be more of a builder/factory pattern, e.g.:

var matcher = new Matcher();
matcher.Add("**/*.resx");
var resxFilePaths = matcher.Execute();

var builder = new ResourceManagerBuilder();

foreach (var file in resxFilePaths)
{
    CultureInfo culture = GetCultureFromFileName(file);
    string resourceName = Path.GetFileNameWithoutExtension(file);

    using (var fs = File.OpenRead(file))
    {
        var resx = XDocument.Load(fs);

        using (var writerStream = new MemoryStream())
        {
            var rw = new ResourceWriter(writerStream);

            foreach (var e in resx.Root.Elements("data"))
            {
                // TODO: Ignore non-string resources
                var name = e.Attribute("name").Value;
                var value = e.Element("value").Value;

                rw.AddResource(name, value);
            }

            rw.Generate();
            builder.Add(resourceName, culture, writerStream);
        }
    }
}

var resourceManager = builder.Build();

DamianEdwards avatar Mar 04 '15 19:03 DamianEdwards

The idea is to opt out of probing.

davidfowl avatar Mar 04 '15 20:03 davidfowl

You want to opt out of probing resource files on disk, but from Damian's example, ResourceManager would still be responsible for handling the lookup, right? Or is that what dotnet/corefx#948 is also about, enabling you guys to control it?

davkean avatar Mar 04 '15 21:03 davkean

By lookup here, I mean deciding from which culture to pick a resource given a key.

davkean avatar Mar 04 '15 21:03 davkean

Yeah, we'd expect lookup to still be handled by ResourceManager

DamianEdwards avatar Mar 04 '15 21:03 DamianEdwards

With the 67da622 you can implement your own Resource Manger for example like here https://gist.github.com/ramarag/6e6f58a210dd00745582

ramarag avatar Apr 11 '15 00:04 ramarag