ih

i18n-helper

i18n helper

Showing:

Popularity

Downloads/wk

21

GitHub Stars

6

Maintenance

Last Commit

6yrs ago

Contributors

2

Package

Dependencies

0

License

ISC

Type Definitions

Tree-Shakeable

No?

Categories

Readme

i18n-helper

npm version


I18n helper for node, you can use it as a helper in jsx , handlebars , ejs or any other template engines .

Install

npm install i18n-helper --save

Usage

var locale = 'en';
var langs = {
    en: {
        'greetings': 'Hello {1} !'
    },
    zh: {
        'greetings': '你好 {1}!'
    }
};

var i18n = require('i18n-helper')(langs[locale]);

console.log(i18n('greetings', 'Jack')); // => 'Hello Jack !'

Init

i18n-helper exports a generator which returns an i18n helper.

You can pass multiply resources into this generator, the resources will be merged to one from right to left.

// global resource and module resource
var globalLangs = require('../i18n/en');
var moduleLangs = require('./i18n/en');

// the i18n helper generator
var i18nHelperGenerator = require('i18n-helper');

// create an i18n helper with the giving resources
var i18nHelper = i18nHelperGenerator(globalLangs, moduleLangs);

Find Key

The helper will try to find the matched key from the merged resource, if not found, the key itself is returned.

var i18n = i18nHelper({ key1: 'hello', key2: 'world' });

console.log(i18n('key'));
// => 'key'

You can override i18n.keyNotFound to apply your customized key not found handler ( >= 1.1.0 ).

var i18n = i18nHelper({ key1: 'hello', key2: 'world' });

i18n.keyNotFound = function(key) {
    return key + ' is not found';
};

console.log(i18n('key'));
// => 'key is not found'

Template

If the value to the key is a template ( with a string like {1} ), then the arguments will be substituted into the template.

var i18n = i18nHelper({ greetings: 'Hello {1} ! Welcome to {2} .' });

console.log(i18n('greetings', 'Jack', 'China'));
// => 'Hello Jack ! Welcome to China .'

Change Log

1.2.0

  • support templating key #1.

1.1.0

  • add keyNotFound handler.

Rate & Review

Great Documentation0
Easy to Use0
Performant0
Highly Customizable0
Bleeding Edge0
Responsive Maintainers0
Poor Documentation0
Hard to Use0
Slow0
Buggy0
Abandoned0
Unwelcoming Community0
100
No reviews found
Be the first to rate

Alternatives

No alternatives found

Tutorials

No tutorials found
Add a tutorial