What is Localazy?
Localazy is a translation management platform and it’s a great option for developers because of several key features. My favorite ones are:
- It comes with ShareTM allowing you to automatically translate your app into 80+ languages for free by sharing translations with other developers.
- With its CLI, it can be easily integrated into any workflow. And we are going to use the CLI today.
Part One - Getting started with Localazy
Create a Localazy account and create a new app. I will use English as source language, but you can choose any other. Then on the integration screen, select JSON. We will upload the source strings in a bit.
Afterwards, you can install Localazy’s CLI for Linux, MacOS or Windows.
Come back to your project. In the root folder, create a file called localazy.json
and paste the following. Make sure to fill in your writeKey and readKey which you can retrieve from your app either under settings tab or in the first step of the JSON CLI guide on the select integration screen.
{
"writeKey": "your-apps-write-key",
"readKey": "your-apps-read-key",
"upload": {
"type": "json",
"files": "locales/en.json"
},
"download": {
"files": "locales/${lang}.json"
}
}
Now, we can upload the source strings. Create en.json
in locales folder and edit as needed. If you are using another language as your source, replace en
with the correct locale.
{
"appName": "Your Cool App",
"error": "An error has occurred.",
"hello": "Hello %{name}"
}
Now run localazy upload
and you should see your source strings in your app in Localazy. At this point, you may add new languages, for which you can use automatic or manual translations. Automatic translations use highly accurate community translations, so they are generally very precise. However, they support translations only from English at the moment, so you need to have the English language added. It does not have to be your source language though.
Before downloading, you need to review automatically translated strings. Otherwise, they have only a candidate status and won’t be published. In case you, as an owner, translate anything, the strings are automatically accepted without the review process. Try to add German language and review the suggested phrases or translate them manually (it does not need to be proper German ツ).
Once you have approved the translations, you can run localazy download
to gather edited files.
Part Two - Development
Install node-polyglot and fs via npm.
npm install node-polyglot
In your code add the following to your index.js
.
const Polyglot = require('node-polyglot')
const fs = require('fs')
const polyglots = new Map() // here we will store all our different locales
const translationFiles = fs.readdirSync('./locales').filter(file => file.endsWith('.json')) // gather our locales
for (const file of translationFiles) {
const t = require('./locales/' + file)
const p = new Polyglot()
p.extend(t) // load all translations into it
const localeName = file.slice(0, -5);
polyglots.set(localeName, p) // sets the locale name and the Polyglot instance
}
function translate (key, locale, options = {}) {
return polyglots.get(locale).t(key, options);
}
If you’d like, you can name the file something like translate.js
to turn it into a module. You’d also add module.exports = translate
to the end of the file. Then you can const translate = require('./translate.js')
to get the function.
Now, to translate anything, use the translate()
function, like so:
console.log(translate('hello', 'de', { name: 'Daniel' }))
When you run node index.js
, you should see a console log of the phrase hello translated into German.
Implement this to fit your code, and you are all set! You may check out the final repo here.
If you run into issues, please leave a message and I will fix it as soon as possible.
This is a companion discussion topic for the original entry at https://localazy.com/blog/how-to-localise-nodejs-with-polyglotjs-and-localazy-i18n