npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@manuth/resource-manager

v3.0.2

Published

A manager for localized resources.

Downloads

49

Readme

ResourceManager.js

A manager for localized resources.

status-badge

General

ResourceManager.js allows you to load and resolve resource-objects from different files.

It also has the capability to load and manage localized resources which allows you to resolve resource-items for the locale of your choice and to take care of locales and their parental relationships which simplifies the creation of resources for different dialects of the same language.

Table of Contents

Installing ResourceManager.js

ResourceManager.js can be installed using following command:

npm install --save @manuth/resource-manager

Resources

You might want to work with a resource rather than a resource-manager, if you don't care about localization.

ResourceManager.js provides classes for loading resources from different file-types:

  • JSONResource for JSON and JSON with Comment-files
  • YAMLResource for YAML-files
  • JavaScriptResource for .js-files
  • ObjectResource for objects

Example

Messages.json:

{
    "StartMessage": "My App just started",
    "WelcomeMessage": {
        "Title": "My App",
        "Message": "Hello and a warmth welcome to My App!"
    },
    "Duplicate.Test": "",
    "Duplicate": {
        "Test": ""
    }
}

Messages.yml:

Test: YAML-Resource

Messages.js:

module.exports = {
    StartMessage: "JavaScript-Resource"
}
const Dialog = require("dialog");
const { JSONResource, ObjectResource, YAMLResource } = require("@manuth/resource-manager");

let jsonResource = new JSONResource("./Messages.json");
let yamlResource = new YAMLResource("./Messages.yml");
let jsResource = new JavaScriptResource("./Messages.js");
let objectResource = new ObjectResource(require("./Messages"));

console.log(jsonResource.Get("StartMessage"));
Dialog.info(jsonResource.Get("WelcomeMessage.Message"), jsonResource.Get("WelcomeMessage.Title"), () =>{});
console.log(jsonResource.Get("Inexistent.Key")); //Throws a "KeyNotFoundException"
console.log(jsonResource.Get("Duplicate.Test")); // Throws a "DuplicateKeyException"
console.log(yamlResource.Get("Test")); // Logs "YAML-Resource"
console.log(jsResource.Get("Test")); // Logs "JavaScript-Resource"
console.log(objectResource.Get("Test")); // Logs "JavaScript-Resource"

Caching

When working with file-resources you can also enable the cache-mode by setting FileResource.Cached.
This forces the system to cache the resource-items of the file and to read resource-items from the cache rather than from the file.

Example

const FileSystem = require("fs");
const { JSONResource } = require("@manuth/resource-manager");

let resource = new JSONResource("./Messages.json");
resource.Cached = true;
console.log(resource.Get("StartMessage"));
FileSystem.unlinkSync("./Messages.json");
console.log(resource.Get("StartMessage")); // Does not throw
FileSystem.writeFileSync("./Messages.json", '{ "Test": "Test" }');
resource.Refresh();
console.log(resource.Get("Test")); // Logs "Test"
console.log(resource.Get("StartMessage")); // Throws a "KeyNotFoundException"

Mustache-Resources

You might want to composite resource-items using mustache.
This can be done by wrapping your resource with a mustache-resource which pre-processes resource-items using mustache.render().

Example

MustacheResource.yml:

ProjectName: My App
Author: m@nuth
Console:
    CopyRightMessage: "{{ProjectName}} © by {{Author}}"
const { MustacheResource, YAMLResource } = require("@manuth/resource-manager");

let resource = new MustacheResource(new YAMLResource("./MustacheResource.yml"));
console.log(resource.Get("Console.CopyRightMessage"));

This produces following output:

My App © by m@nuth

Localized Resources

One thing you might have missed is that a resource also contains information about its locale (the default is the global, invariant culture). This information is used by the ResourceManager to resolve resource-items of the best-matching culture.

The ResourceManager is capable to load resource-files automatically based on their file-name if they follow the path-convention:

{ BaseFileName }[.{ locale-abbreviation }].{ File-Extension }

If you skip the locale-part the global, invariant culture will be set.

Examples:

Resource.json
Resource.de.jsonc
Resource.en.yml
Resource.en-US.js

This might be very useful if you work with similar languages and dialects.

Example

Resource.en.json:

{
    "WelcomeMessage": "Welcome to my awesome web-developer tool",
    "CopyItem": "Copy to Clipboard",
    "UnknownError": "An unexpected error occurred",
    "ColorError": "Please specify a valid color",
    "Duplicate.File": "",
    "Duplicate.Test": "",
    "Duplicate": {
        "Test": ""
    }
}

Resource.en.yml:

Duplicate.File: ""

Resource.en-GB.json:

{
    "ColorError": "Please specify a valid colour"
}
const { CultureInfo, ResourceManager } = require("@manuth/resource-manager");

let manager = new ResourceManager("./Resource", new CultureInfo("en-gb"));
console.log(manager.Get("CopyItem")); // "Copy to Clipboard"
console.log(manager.Get("ColorError")); // "Please specify a valid colour"
console.log(manager.Get("ColorError"), new CultureInfo("en-US")); // "Please specify a valid color"
manager.Locale = new CultureInfo("en");
console.log(manager.Get("ColorError")); // "Please specify a valid color"
console.log(manager.Get("Inexistent.Key")); // Throws a KeyNotFoundException
console.log(manager.Get("Duplicate.Test")); // Throws a DuplicateKeyException
console.log(manager.Get("Duplicate.File")); // Throws a DuplicateKeyException

Mustache-ResourceManager

The resource-manager is is able to work with mustache, too!
The above example can be simplified by wrapping the resource-manager with a MustacheResourceManager which allows you to pre-process localized resource-items.

Example

Resource.en.json:

{
    "Color": "color",
    "WelcomeMessage": "Welcome to my awesome web-developer tool",
    "CopyItem": "Copy to Clipboard",
    "UnknownError": "An unexpected error occurred",
    "ColorError": "Please specify a valid {{Color}}"
}

Resource.en-gb.yml:

Color: colour
const { CultureInfo, MustacheResourceManager, ResourceManager } = require("@manuth/resource-manager");

let manager = new MustacheResourceManager(new ResourceManager("./Resource"));
manager.Locale = new CultureInfo("en-gb");
console.log(manager.Get("WelcomeMessage")); // "Welcome to my awesome web-developer tool"
console.log(manager.Get("ColorError")); // "Please specify a valid colour"
console.log(manager.Get("ColorError", new CultureInfo("en"))); // "Please specify a valid color"

Use with Mustache

The ResourceContext and ResourceManagerContext-classes allow you to use a resource or a resource-manager as a mustache-context, which might be useful for you:

Example

const { CultureInfo, ResourceManager, ResourceManagerContext } = require("@manuth/resource-manager");
const Mustache = require("mustache");

let manager = new ResourceManager("./Resource", new CultureInfo("en"));
Mustache.render(
    `
    <html>
        <head>
        </head>
        <body>
            <div class="container">
                {{WelcomeMessage}}
            </div>
        </body>
    </html>`,
    new ResourceManagerContext(manager));

Exception-Handling

Generally all classes may throw one of these exceptions:

  • KeyNotFoundException:
    This error occurs when a key could not be found.
  • DuplicateKeyException:
    This error occurs when a key is not distinguishable