crafity-resources

0.1.4 • Public • Published

Crafity Resources Dependency status Travis Build Status NPM Module version

Preparation

Install crafity-resources module via NPM installer or by cloning this repository from GitHub:

via NPM

$ npm install crafity-resources

via GitHub

$ 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

Summary

crafity-resources module delivers your Json formatted resource contents based on language and namespace. Unit tested with 75% code coverage.

Public API

Require resources module:

var resources = require('../main.js')
    ;

resources.configure([options], callback);

  • options String|Object Contains the path to the config file and a filter language
  • callback 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
}

Example 1

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();
    
});
 

Example 2

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();
    
});
 

Example 3

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 ...
    }
);

Readme

Keywords

none

Package Sidebar

Install

npm i crafity-resources

Weekly Downloads

11

Version

0.1.4

License

none

Last publish

Collaborators

  • crafity