Lightweight message localisation for NodeJS.
pnpm add @eartharoid/i18n
oryarn add @eartharoid/i18n
ornpm i @eartharoid/i18n
en-GB.json
:
{
"hello": "Hello!",
"hello_name": "Hello, %s!",
"other": {
"age": [
"1 year old",
"%d years old"
]
}
}
See test/index.js.
const I18n = require('@eartharoid/i18n'); // require module
const i18n = new I18n('src/locales', 'en-GB'); // create new instance - first value is the locales directory, second value is the default locale
const en = i18n.get(); // get default locale
const fr = i18n.get('fr-FR'); // get specified locale
console.log('en', en('hello')); // get english translation of 'hello' key
console.log('fr', fr('hello')); // get french translation of 'hello' key
console.log('en', en('hello_name', 'Isaac')); // english with placeholder
console.log('fr', fr('hello_name', 'Isaac')); // french with placeholder
// keys can be nested
console.log('en', en('other.age', 1)); // age[0] if first argument is 1
console.log('en', en('other.age', 21)); // age[1] if first argument is not 1
console.log('fr', fr('other.age', 1));
console.log('fr', fr('other.age', 21));