browser-i18n
A simple in-browser i18n module, compatible with the i18n-node server module data files. Meant to be use with a module bundler (like parcel).
Installation
By using npm:
npm i browser-i18n --save
Usage
import I18n from 'browser-i18n';
const i18n = new I18n({
language: 'fr',
path: '/locales',
extension: '.json'
});
console.log( i18n.__('Hello') );
console.log( i18n.__('Oh, hi %s!', 'Mark') );
console.log( __('Hello') );
Files structures
One file for each language. They may have the following structure:
{
"Hello": "Hello",
"Oh, hi %s!": "Oh, hi %s!",
"Bye!": "Bye!"
}
{
"Hello": "Bonjour",
"Oh, hi %s!": "Oh, salut %s!",
"Bye!": "Au revoir!"
}
Put your locales folder accessible publicly. You can do it by putting it in your public root:
.
└── public
└── locales
├── en.json
└── fr.json
Or, using express:
app.use('/locales', express.static(path.join(__dirname, 'locales')));
API
Configuration:
const i18n = new I18n({
language: 'fr',
path: '/locales',
extension: '.json',
setGobal: true,
onReady: callback,
verbose: true
});