Node.js i18n: Agile Localization for Developers
Tags: node, tutorial, localization, webdev
Published: 2022-03-25 10:23:26


In today’s market, people seek personalization. They expect your app to speak their native language. In this article, we will cover how you can translate your Node.js app using a developer-friendly localization platform. Learn about the i18n framework and how to integrate your repository with a localization system to automate Node.js localization.


## Make Your App Multilingual and Grow Your Revenue

Language connects people. The same principle is applied to your app development. Having localized versions of your app can grow and generate great revenue for your app by opening new markets.  

The internet is somewhat similar to the real world. When you visit a place where no one speaks your mother language, you feel isolated and lost. Making your app multilingual help your customers use it more effectively and opens up opportunities to reach new audiences.  

According to a recent <a href="https://preply.com/en/blog/language-inclusive-apps/" target="_blank">study by Preply</a>, most of the top apps in each tech category are also the most language-inclusive ones.  

## Node.js Web Application Framework

Node.js is a server-side platform built on Google Chrome's JavaScript Engine. The official <a href="https://nodejs.org/en/" target="_blank">documentation of Node.js</a> states the following – ‘’Node.js is a platform built on Chrome's JavaScript runtime for easily building fast and scalable network applications. Node.js uses an event-driven, non-blocking I/O model that makes it lightweight and efficient, perfect for data-intensive real-time applications that run across distributed devices.’’  

If you choose Node.js as a development environment and are curious about how you can implement localization as a part of your workflow – you’re in the right place.

## Node.js i18n: Localization Starting Point

If you plan to create a multilingual version of your app, you should use one of the Node.js internationalization libraries available, for example on <a href="https://openbase.com/" target="_blank">Openbase</a>. This article will cover the <a href="https://github.com/mashpie/i18n-node" target="_blank">i18n-node</a> as an example. You may choose another library according to your preferences. They do implement localization functionality pretty much in the same way. 

Here are a few first steps you need to take to begin the localization of your Node.js app with <a href="https://github.com/mashpie/i18n-node" target="_blank">i18n-node</a> library.

### The first step would be to install the package

`$ npm install i18n`

### Then, create an i18n config object

Before we use the library, we need to configure it.

`$ touch app/i18n.config.js`

*File: app/i18n.config.js*

```
const { I18n } = require('i18n');
const path = require('path');

const i18n = new I18n({
  locales: ['en', 'uk'],
  defaultLocale: 'en',
  directory: path.join('./', 'locales')
});

module.exports = i18n;
```

We added support for 2 languages, with the default one being en (English) and the second one being uk (Ukrainian). View the whole list of all <a href="https://github.com/mashpie/i18n-node#list-of-all-configuration-options" target="_blank">configuration</a> options for the library to learn more. 

### Create an i18n object

`$ touch index.js`

*File: index.js* 

```
const i18n = require('./app/i18n.config');

console.log(i18n.getLocales()); // ['en', 'uk']
console.log(i18n.getLocale()); // 'en'
console.log(i18n.__('Hello')); // 'Hello'
console.log(i18n.__n('You have %s message', 5)); // 'You have 5 messages'
```

Then in the command line, run the following:

`$ node index.js`

This will automatically generate a locales' directory on the root folder. It will contain the relevant translation strings for the current language:


`$ tree locales 
locales
├── uk.json
└── en.json`


*File: locales/en.json*


`{
   "Hello": "Hello",
   "You have %s message": {
      "one": "You have %s message",
      "other": "You have %s messages"
   }
}`


Add the following line in the index.js to test the generation of translatable strings for the other language:

`i18n.setLocale('uk')`

*File: locales/uk.json*


`{
   "Hello": "Привіт",
   "You have %s message": {
      "one": "Ви маєте %s повідомлення",
      "other": "Ви маєте %s повідомлень"
   }
}`


### Rerun the app to verify translations


`$ node index.js
Привіт
Ви маєте 5 повідомлень`


## Automate Node.js Localization with Crowdin

Node.js localization library was created to help and save your time as a developer. However, as you can assume from a small example above, doing the actual translations into multiple languages can still be very challenging, especially for complex apps.  

You will face several challenges like making sure that:

* your translators do not edit the same file simultaneously or break the code
*  translation values are present for each language
* you don’t have to spend time copy-pasting translations  

You don’t have to deal with all the extra work. And why would you if there is a solution? Make localization part of your workflow and forget about those issues.

### Integrate Your Repo and Localization Project 

Set up an integration with your repo. Crowdin integrates with <a href="https://store.crowdin.com/collections/frontpage/products/github" target="_blank">GitHub</a>, <a href="https://store.crowdin.com/collections/frontpage/products/gitlab" target="_blank">GitLab</a>, <a href="https://store.crowdin.com/products/bitbucket?_pos=1&_sid=499a27e7c&_ss=r" target="_blank">Bitbucket</a>, and <a href="https://store.crowdin.com/collections/frontpage/products/azure-repos" target="_blank">Azure Repos</a>. You can also install <a href="https://support.crowdin.com/cli-tool/" target="_blank">Crowdin Console Client</a> (CLI) which allows you to integrate with GIT, SVN, Mercurial, and more. 

Crowdin will get the localization files from your repo and upload them to the <a href="https://support.crowdin.com/online-editor/" target="_blank">Editor</a>, where all strings from your app will look user-friendly and include the comments you provided. Once the translations are done, the system compiles them into a file and syncs them with your code as a merge request. Translations do not get to the master branch before you merge them. You can also integrate multiple branches with Crowdin at the same time, so translators can work with the texts from your feature branch before it’s rolled out, and you can release them into multiple languages at the same time.  

In case you have more than one app or several products that share similar features and texts, you can share their translations and avoid translating the same content twice, saving time and money. Usually, iOS and Android app versions have much of the same content. In Crowdin, you can easily detect duplicate strings to reuse their translations. 

### Use Machine Translation and Translation Memory Pre-Translation 

Both machine translation (MT) and translation memory (TM) will help you translate content faster and reduce translation costs. With their help, you can reduce the duration of the project even before the translators begin their work. To do this, configure pre-translation via TM or MT. You can also leverage MT to get translations of non-critical content.  

On Crowdin, you can localize your product with the help of more than 40 machine engines like Microsoft Translator, Google Translate, DeepL Translator. View the list of <a href="https://crowdin.com/page/integrations#nav-mt-engines" target="_blank">machine engines</a> you can use with Crowdin today.

### Ensure Context for Translators 

Simply put, context ensures the quality of the localized version of your app. Crowdin has created a few ways to help you provide context for translators and reduce manual work. 

You can provide context using:

* WYSIWYG <a href="https://support.crowdin.com/online-editor/" target="_blank">editor</a> view
* Context for strings. Add text descriptions or labels, use Glossary terms to get relevant translations faster.
* Screenshots. You can upload your game or app screenshots manually or using different integrations or add-ons, like Crowdin SDK for example. This way, translators know the location and context for each string.
* <a href="https://support.crowdin.com/enterprise/in-context/" target="_blank">In-Context</a> Localization tool. Translators can work as if in the real app interface and preview the translations they make right there. Translations are stored within your Crowdin project, and you can decide when to pull the strings to your application. 

You can learn more about the Crowdin platform by watching a short <a href="https://crowdin.com/on-demand-demo" target="_blank">on-demand demo</a>.

## Localize Your Product with Crowdin

Automate localization by internationalizing your texts and integrating your repo with Crowdin New source strings are automatically sent to translators, and you receive translations as a merge after the work is done. Let your customers access your product in several languages. Get started and register a <a href="https://accounts.crowdin.com/register" target="_blank">Crowdin account</a>.