3.0.0-5 • Published 3 years ago

new-i18n v3.0.0-5

Weekly downloads
32
License
MIT
Repository
github
Last release
3 years ago

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

3 years ago

3.0.0-4

3 years ago

3.0.0-3

3 years ago

3.0.0-2

3 years ago

3.0.0-1

3 years ago

3.0.0-0

4 years ago

2.0.0

4 years ago

1.4.0

4 years ago

1.3.0

4 years ago

1.2.0

5 years ago

1.1.1

6 years ago

1.1.0

6 years ago

1.0.3

6 years ago

1.0.2

6 years ago

1.0.1

6 years ago

1.0.0

6 years ago