DEV Community

Cover image for How to localise NodeJS with Polyglot.js and Localazy
vaclavhodek for Localazy

Posted on • Originally published at localazy.com

How to localise NodeJS with Polyglot.js and Localazy

Do you use Polyglot.js to localize your NodeJS app? Or maybe you're looking for a tool to use? That's great, but tell me, how easily can you manage the translation files? Not really, huh? In this article, I'll give you a short introduction to translation management with Polyglot.js and Localazy.

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.

Getting started with Localazy

Create a Localazy account and create a new app. I will use English as the source language, but you can choose any other. Then on the integration screen, select JSON. We will upload the source strings in a bit.

Afterward, 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 the 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"
  }
}
Enter fullscreen mode Exit fullscreen mode

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}"
}
Enter fullscreen mode Exit fullscreen mode

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.

Development

Install node-polyglot and fs via npm.

npm install node-polyglot
Enter fullscreen mode Exit fullscreen mode

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);
}
Enter fullscreen mode Exit fullscreen mode

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' }))
Enter fullscreen mode Exit fullscreen mode

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 article was originally written by Daniel Newell and reposted with permission.

Top comments (0)