You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
YouTwitFace e7e0fc9415 Update version 2 months ago
test Allow updating languages (closes #3) 2 months ago
.eslintrc.json Update eslint rules 2 months ago
.gitignore Add tests 1 year ago
.npmignore Add eslint 1 year ago
LICENSE Add license 5 months ago
README.md Update docs 2 months ago
_config.yml Set theme jekyll-theme-cayman 1 year ago
i18n.js Allow updating languages (closes #3) 2 months ago
index.js Use a class instead of a function (backwards compatible) 2 months ago
package-lock.json Update version 2 months ago
package.json Update version 2 months ago

README.md

new-i18n

new-i18n is a simple and easy to use internationalization library.

Installation

npm install new-i18n

Example

There’s a full working example here

localization/en.json:

{
    "hello_world": "Hello World"
}

localization/pt.json:

{
    "hello_world": "Olá Mundo"
}

index.js:

const newI18n = require('new-i18n');
const i18n = newI18n('./localization', ['en', 'pt']);

console.log('English:', i18n('en', 'hello_world')); // 'Hello World'
console.log('Portuguese:', i18n('pt', 'hello_world')); // 'Olá Mundo'

Adding variables

{
    "hi": "Hi {{name}}!"
}
console.log(i18n('en', 'hi', { name: '...' })); // 'Hi ...!'

Updating Varaibles

i18n.update('en', { hi: 'Hello {{name}}!' });
console.log(i18n('en', 'hi', { name: '...' })); // 'Hhello ...!'

Nesting

localization/en.json:

{
    "nested": {
        "hello_world": "Hello World"
    }
}

localization/pt.json:

{
    "nested": {
        "hello_world": "Olá Mundo"
    }
}

index.js:

const newI18n = require('new-i18n');
const i18n = newI18n('./localization', ['en', 'pt']);

console.log('English:', i18n('en', 'nested.hello_world')); // 'Hello World'
console.log('Portuguese:', i18n('pt', 'nested.hello_world')); // 'Olá Mundo'

Getting all the languages

const newI18n = require('new-i18n');
const i18n = newI18n('./localization', ['en', 'pt']);

console.log(i18n.languages); // ['en', 'pt']