ResourceManager.js
A manager for localized resources.
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
ResourceManager.js
Installing ResourceManager.js
can be installed using following command:
npm install --save localized-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
forJSON
andJSON with Comment
-filesYAMLResource
forYAML
-filesJavaScriptResource
for.js
-filesObjectResource
for objects
Example
Messages.json:
Messages.yml:
Test: YAML-Resource
Messages.js:
moduleexports = StartMessage: "JavaScript-Resource"
const Dialog = ;const JSONResource ObjectResource YAMLResource = ; let jsonResource = "./Messages.json";let yamlResource = "./Messages.yml";let jsResource = "./Messages.js";let objectResource = ; console;Dialog;console; //Throws a "KeyNotFoundException"console; // Throws a "DuplicateKeyException"console; // Logs "YAML-Resource"console; // Logs "JavaScript-Resource"console; // 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 = ;const JSONResource = ; let resource = "./Messages.json";resourceCached = true;console;FileSystem;console; // Does not throwFileSystem;resource;console; // Logs "Test"console; // 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 AppAuthor: m@nuthConsole: CopyRightMessage: "{{ProjectName}} © by {{Author}}"
const MustacheResource YAMLResource = ; let resource = "./MustacheResource.yml";console;
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:
Resource.en.yml:
Duplicate.File: ""
Resource.en-GB.json:
const CultureInfo ResourceManager = ; let manager = "./Resource" "en-gb";console; // "Copy to Clipboard"console; // "Please specify a valid colour"console; // "Please specify a valid color"managerLocale = "en";console; // "Please specify a valid color"console; // Throws a KeyNotFoundExceptionconsole; // Throws a DuplicateKeyExceptionconsole; // Throws a DuplicateKeyException
Mustache-ResourceManager
The resource-manager is mustachable, 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:
Resource.en-gb.yml:
Color: colour
const CultureInfo MustacheResourceManager ResourceManager = ; let manager = "./Resource";managerLocale = "en-gb";console; // "Welcome to my awesome web-developer tool"console; // "Please specify a valid colour"console; // "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 = ;const Mustache = ; let manager = "./Resource" "en";Mustache;
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