Internationalization (i18n) in JavaScript (Node.js): i18next Library

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Internationalization (i18n) in JavaScript (Node.js): i18next Library



Internationalization (abbreviated as i18n — from the English "internationalization", where 18 is the number of letters between the first "i" and the last "n") is the process of designing and developing software that allows it to be adapted to different languages and regional features without changing the source code. In modern web development on Node.js, creating multilingual applications is a necessity for entering the global market.



One of the most popular and powerful libraries for implementing i18n in the JavaScript ecosystem (including Node.js) is i18next. It offers a flexible architecture, support for multiple translation file formats, interpolation, pluralization, and contextual translations. In this article, we will thoroughly cover the installation, configuration, and use of i18next on the server side.



Installation and Setup



To start working with i18next in a Node.js project, install the library via npm:



npm install i18next


It is also recommended to install the i18next-fs-backend package for loading translation files from the file system:



npm install i18next-fs-backend


After installation, create an initialization file, for example i18n.js:



const i18next = require('i18next');const Backend = require('i18next-fs-backend');

i18next.use(Backend).init({ lng: 'ru', // default language fallbackLng: 'en', // fallback language backend: { loadPath: './locales/{{lng}}/{{ns}}.json' // path to translation files }, interpolation: { escapeValue: false // do not escape HTML (for Node.js) }});

module.exports = i18next;


Translation File Structure



Create a locales folder in the project root. Inside it, create a subfolder for each language (e.g., en, ru, de). In each subfolder, place JSON files with translations. Typically, one translation.json file is used per language, but you can split them into namespaces for modularity.



Example of the file locales/ru/translation.json:



{  "welcome": "Добро пожаловать на наш сайт!",  "greeting": "Привет, {{name}}!",  "items": "{{count}} элемент",  "items_plural": "{{count}} элементов",  "menu": {    "home": "Главная",    "about": "О нас",    "contact": "Контакты"  }}


Example of the file locales/en/translation.json:



{  "welcome": "Welcome to our website!",  "greeting": "Hello, {{name}}!",  "items": "{{count}} item",  "items_plural": "{{count}} items",  "menu": {    "home": "Home",    "about": "About",    "contact": "Contact"  }}


Main Features of i18next



Simple Translation



To get a translation, use the t() function (from "translate"):



const i18next = require('./i18n');

// Asynchronous initialization (in a real project, wait for readiness)i18next.t('welcome'); // "Добро пожаловать на наш сайт!" (if lng: 'ru')


Variable Interpolation



Insert dynamic values into strings using double curly braces:



i18next.t('greeting', { name: 'Алексей' });// Result: "Привет, Алексей!"


Pluralization



The library automatically selects the correct plural form based on the language's rules. To do this, add keys with the _plural suffix:



i18next.t('items', { count: 1 }); // "1 элемент"i18next.t('items', { count: 5 }); // "5 элементов"


For English, the rule is simpler (one item — item, many — items). For Russian, i18next uses built-in pluralization rules that account for numbers 1, 2-4, 5-20, etc.



Nested Keys and Namespaces

Recommendations