How to localise NodeJS with Polyglot.js and Localazy
Tags: javascript, typescript, node, i18n
Published: 2020-11-25 13:53:36

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](https://localazy.com/docs/general/what-is-localazy-sharetm) allowing you to automatically translate your app into 80+ languages for free by sharing translations with other developers.
- With its [CLI](https://localazy.com/docs/cli/the-basics), it can be easily integrated into any workflow. And we are going to use the CLI today. 

# Getting started with Localazy

Create a [Localazy](https://localazy.com) 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](https://localazy.com/docs/cli/installation) 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.


```json
{

  "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.

```json
{
  "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.

# Development

Install [node-polyglot](https://npm.im/node-polyglot) and fs via npm. 

```bash
npm install node-polyglot
```

In your code add the following to your `index.js`.

```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:

```js
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](https://github.com/danielnewell/polyglotjs-sample).

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](https://dev.to/danielnewell) and reposted with permission.