displaykey

0.2.2 • Public • Published

displaykey

Build Status

Simple text localization for your node.js application.

Example

var displaykey = require('displaykey');
 
var pathToLocaleFiles = require('path').join(__dirname, 'path/to/locale/files');
var refreshInterval = 30000;
displaykey.setup(pathToLocaleFiles, refreshInterval, function() {
    var value = displaykey.get('some.value.in.json.file', 'en');
});

Installation

$ npm install displaykey

Usage

Locale Files

displaykey loads in the different locale-specific values from JSON files. You need to create a directory to store these JSON files (it cannot contain anything other than these files). The format of the files should be similar to the following:

{
    "key": "value",
    "some": {
        "nested": {
            "key": "some nested value"
        }
    }
}

You should have one of these files created for each locale you plan to support. Name them appropriately, since the name you use will be used as the locale code that is passed into the get() function (minus the '.json' extension).

Setting Up

After installing using npm, you must setup displaykey to load in locale-specific JSON files from the correct directory and tell it how often to check for changes to these files and load them in. To do this, it exposes the setup() function.

var displaykey = require('displaykey');
 
// Set an absolute path to the locale-specific JSON files.
var pathToLocaleFiles = require('path').join(__dirname, 'path/to/locale/files');
 
// Set the refresh interval in milliseconds.
var refreshInterval = 30000;
 
// Call the setup function, passing in these values.
// It will invoke the callback function when all displaykeys are loaded.
displaykey.setup(pathToLocaleFiles, refreshInterval, function() {
    // Now you can use the get() function to get any displaykey from any locale.
});

Getting Values

Now that everything is set up, you are able to use the displaykey module in all it's glory. The get() function provides you with the ability to get the value of something for a specific locale. You can use it as follows:

displaykey.get('some.nested.key', 'en');

where 'some.nested.key' is a nested key in each of the locale files, and 'en' is the name of the locale file to retrieve the value from.

Therefore, if you have two locale files like so:

en.json

{
    "key": "english value",
    "some": {
        "nested": {
            "key": "some nested english value"
        }
    }
}

fr.json

{
    "key": "french value",
    "some": {
        "nested": {
            "key": "some nested french value"
        }
    }
}

The call to get() above would return 'some nested english value'.

License

MIT © 2015 Gavin Flood

Package Sidebar

Install

npm i displaykey

Weekly Downloads

1

Version

0.2.2

License

MIT

Last publish

Collaborators

  • gavinflud