i18next Internationalization Framework

26.0.6 · active · verified Sun Apr 19

i18next is a comprehensive and highly popular internationalization (i18n) framework designed for JavaScript environments, supporting browsers, Node.js, and Deno. It provides a robust core for handling translations, enabling features like flexible backend connections for loading translations (e.g., via XHR), optional caching, automatic user language detection, proper pluralization rules, translation context management, variable interpolation, and nested translations. The current stable version is 26.0.6, with a regular release cadence addressing bug fixes, type improvements, and occasional minor features, alongside major releases introducing breaking changes and significant enhancements. Its key differentiators include its extensibility through a rich plugin ecosystem and its framework-agnostic nature, allowing integration with React, Angular, Vue, and vanilla JavaScript applications. It focuses on providing a powerful core while allowing developers to choose their preferred building blocks.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates basic i18next initialization, loading multiple language resources, and translating keys with interpolation, pluralization, and context. It also shows how to change the active language programmatically.

import i18next from 'i18next';

const initI18n = async () => {
  await i18next.init({
    debug: true,
    lng: 'en',
    fallbackLng: 'en',
    resources: {
      en: {
        translation: {
          'welcome': 'Welcome to our site, {{name}}!',
          'plural_key_one': '{{count}} item',
          'plural_key_other': '{{count}} items',
          'greeting_context_male': 'Hello Sir',
          'greeting_context_female': 'Hello Madam',
          'formatted_date': 'Today is {{date, DATE_HUGE}}'
        }
      },
      de: {
        translation: {
          'welcome': 'Willkommen auf unserer Seite, {{name}}!',
          'plural_key_one': '{{count}} Artikel',
          'plural_key_other': '{{count}} Artikel',
          'greeting_context_male': 'Hallo Herr',
          'greeting_context_female': 'Hallo Frau',
          'formatted_date': 'Heute ist der {{date, DATE_HUGE}}'
        }
      }
    },
    interpolation: {
      escapeValue: false // not needed for react.js, xss is prevented by react
    }
  });

  console.log(i18next.t('welcome', { name: 'User' }));
  console.log(i18next.t('plural_key', { count: 1 }));
  console.log(i18next.t('plural_key', { count: 5 }));
  console.log(i18next.t('greeting', { context: 'male' }));
  console.log(i18next.t('greeting', { context: 'female' }));

  // Example of using a formatter (requires a formatter plugin or custom setup)
  // i18next.services.formatter.add('DATE_HUGE', (value, lng, options) => {
  //   return new Intl.DateTimeFormat(lng, { year: 'numeric', month: 'long', day: 'numeric' }).format(value);
  // });
  // console.log(i18next.t('formatted_date', { date: new Date() }));

  await i18next.changeLanguage('de');
  console.log(i18next.t('welcome', { name: 'Nutzer' }));
};

initI18n();

view raw JSON →