i18next Internationalization Framework

19.6.4 · active · verified Sun Apr 19

i18next is a comprehensive and highly popular internationalization (i18n) framework for JavaScript environments, including browsers and Node.js. It is currently at version 19.6.4, with active development leading to frequent minor and major releases, indicated by its extensive changelog. The library provides a robust core for handling translations and is designed for high extensibility and flexibility. Key differentiators include its flexible plugin system for backend integrations (e.g., loading translations via HTTP), optional caching mechanisms, automatic user language detection, sophisticated pluralization rules, translation context handling, string nesting, and variable replacement (interpolation). i18next emphasizes building a rich ecosystem, offering integrations with popular frameworks like React, Angular, and jQuery, making it suitable for a wide range of applications. It aims to be a 'learn once - translate everywhere' solution, abstracting complex i18n challenges.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to initialize i18next with basic translation resources, perform simple key-based translations, utilize interpolation for dynamic values, and handle pluralization. It also shows how to change the active language programmatically, all within an asynchronous context, which is crucial for i18next's `init` function.

import i18next from 'i18next';

interface MyTranslations {
  welcome: string;
  greeting_name: string;
  item_plural: string;
}

async function initializeI18n() {
  await i18next.init<MyTranslations>({
    lng: 'en', // default language
    debug: true,
    resources: {
      en: {
        translation: {
          welcome: 'Welcome to i18next!',
          greeting_name: 'Hello, {{name}}!',
          item_plural: '{{count}} item {{count, plural, one {is} other {are}}} here.'
        }
      },
      de: {
        translation: {
          welcome: 'Willkommen bei i18next!',
          greeting_name: 'Hallo, {{name}}!',
          item_plural: '{{count}} Artikel {{count, plural, one {ist} other {sind}}} hier.'
        }
      }
    },
    interpolation: {
      escapeValue: false // React already escapes values for XSS, set to true for plain JS/Node
    }
  });

  console.log(i18next.t('welcome'));
  console.log(i18next.t('greeting_name', { name: 'World' }));
  console.log(i18next.t('item_plural', { count: 1 }));
  console.log(i18next.t('item_plural', { count: 5 }));

  await i18next.changeLanguage('de');
  console.log(i18next.t('welcome'));
  console.log(i18next.t('greeting_name', { name: 'Welt' }));
  console.log(i18next.t('item_plural', { count: 1 }));
  console.log(i18next.t('item_plural', { count: 5 }));
}

initializeI18n();

view raw JSON →