Skip to content

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

License

Notifications You must be signed in to change notification settings

vrumger/new-i18n

Repository files navigation

new-i18n

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

Changelog

See CHANGELOG.md

Installation

npm install new-i18n

Example

localization/en.json:

{
    "hello_world": "Hello World"
}

localization/pt.json:

{
    "hello_world": "Olá Mundo"
}

index.js:

const { default: I18n } = require('new-i18n');
const i18n = new I18n(__dirname, ['en','pt'], 'en');

console.log('English:', i18n.translate('en', 'hello_world'));
console.log('Portuguese:', i18n.translate('pt', 'hello_world'));

console.log(i18n.translate('en', 'hi', { name: '...' }));
console.log(i18n.translate('pt', 'hi', { name: '...' }));

i18n.update('en', { hi: 'Hello my name is {{name}}!' });
i18n.update('pt', { hi: 'Olá meu nome é {{name}}!' });

console.log(i18n.translate('en', 'hi', { name: '...' }));
console.log(i18n.translate('pt', 'hi', { name: '...' }));

console.log(`I18n languages: ${i18n.languages}`)

Adding variables

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

Updating Varaibles

i18n.update('en', { hi: 'Hello my name is {{name}}!' });
i18n.translate('en', 'hi', { name: '...' }); // 'Hello my name is ...!'

Nesting

localization/en.json:

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

localization/pt.json:

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

index.js:

i18n.translate('en', 'nested.hello_world'); // 'Hello World'
i18n.translate('pt', 'nested.hello_world'); // 'Olá Mundo'

Getting all the languages

i18n.languages; // ['en', 'pt']