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 3e174f136d Merge branch 'master' of https://github.com/YouTwitFace/new-i18n 2 weeks ago
test Throw an error if no languages are passed 2 weeks ago
.eslintrc.json Add eslint 1 year ago
.gitignore Add tests 1 year ago
.npmignore Add eslint 1 year ago
LICENSE Add license 2 months ago
README.md Add nesting example to readme 1 year ago
_config.yml Set theme jekyll-theme-cayman 1 year ago
index.js Throw an error if no languages are passed 2 weeks ago
package-lock.json Change version 5 months ago
package.json Change version to 1.3.0 2 weeks 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 ...'

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']