Install crafity-resources module via NPM installer or by cloning this repository from GitHub:
$ npm install crafity-resources
$ git clone https://github.com/Crafity/crafity-resources.git
$ cd crafity-resources
Before you start using it you must install all its dependencies. They are listed in package.json
file under key dependencies
.
Install them first by running command on the terminal from crafity-resources
as current directory:
$ npm install
After the dependencies have been installed, run the unit tests to check the sanity of the module. From the command line
and current directory crafity-resources
type the command:
$ npm test
crafity-resources
module delivers your Json formatted resource contents based on language and namespace.
Unit tested with 75% code coverage.
Require resources module:
var resources = require('../main.js')
;
options
String|Object Contains the path to the config file and a filter languagecallback
Function A callback called when configuration is loaded
If you call the method with no options
argument and a callback function, then the predefined default path and
language will be used. Here are the defaults:
DEFAULT_CONFIG = {
defaultPath: "/resources", // current directory
defaultLanguage: "en", // English
defaultNamespace: "default" // Default namespace
}
Consider you have resource files in folder resources
in your application root: ~/resources/en.json
and ~/resources/nl.json
.
// path: '~/resources/en.json'
{
"gender": {
"male": "Male",
"female": "Female",
"unknown": "Unknown"
}
}
// path: '~/resources/nl.json'
{
"gender": {
"male": "Man",
"female": "Vrouw",
"unknown": "Onbekend"
}
}
If you call resources.configure
without the first argument it delivers the contents of en.json
:
resources.configure(function (err, resourcesAgent) {
// check err
var resourcesDataInEnglish = resourcesAgent.get();
});
If you call resources.configure
and passing a configuration for "nl" language then it delivers the contents of nl.json
:
var config = {
"path": "/resources",
"defaultLanguage": "nl",
"defaultNamespace": "default"
}
resources.configure(config, function (err, resourcesAgent) {
// check err
var resourcesDataInDutch = resourcesAgent.get();
});
What if you want to organize the reources from the same language group into namespaces and separate files for each namespace.
Your directory can look like /resources/en.json
and /resources/crafity.en.json
:
// path: '~/resources/en.json'
{
"gender": {
"male": "Male",
"female": "Female",
"unknown": "Unknown"
}
}
// path: '~/resources/crafity.en.json'
{
"people": {
"female": "Galina",
"male": "Bart"
}
}
What crafity-resources
does is scan all json files under the resources folder and construct a living resourcesData object
of the sort:
{
"en": {
"crafity":
{ "people": [Object] },
"default":
{ "gender": [Object] }
}
}
resources.configure(function (err, resourcesAgent) {
// handle err
var customResources = resourcesAgent
, crafityResources = resourcesAgent.getResources("en", "crafity")
;
// code ...
}
);