locale-translate
v1.0.2
Published
Locale Translate is an NPM module made to make managing locales as easy as possible. With LT, you can define new locales, and easily use them afterwards.
Downloads
2
Maintainers
Readme
Locale Translate
Locale Translate is an NPM module made to make managing locales as easy as possible. With LT, you can define new locales, and easily use them afterwards.
Installation
npm i translate-it
Usage
First, you need to create a JSON file. Feel free to name it whatever you like. We recommend locales.json
. Then use this format, changing what you need:
{
"en-US": {
"started": "Your project has been initialized.",
"colorChange": "Your project's color has changed."
},
"en-UK": {
"started": "Your project has been initialised.",
"colorChange": "Your project's colour has changed."
}
}
Then, in your JS file, use this,
let translator = require('locale-translate')
translator.importLocales(require('locales.json'), 'memory') // stores the locales in memory
let translated = translator.translate('started', 'en-US')
console.log(translated) // Expected result 'Your project has been initialized.'
Parameters
Using {}
in a translation string allows use of parameters. This is so you can easily pass data into your translated strings. For example,
{
"en-US": {
"started": "Your project, {}, has been initialized.",
"colorChange": "Your project's color has changed to {}."
},
"en-UK": {
"started": "Your project, {}, has been initialised.",
"colorChange": "Your project's colour has changed to {}."
}
}
Use the same usage, but use .translateParams()