Skip to main content

Lesson 5 of 8

Internationalization with react-i18next

Internalization isn't just translation - it's number formatting, date formatting, plural rules, RTL layout support, and lazy-loading translation files. react-i18next is the React integration for i18next, the most widely adopted i18n library.

25 minBy Priygop Editorial TeamUpdated 2026Peer Reviewed & Verified

react-i18next Setup & Patterns

react-i18next Setup & Patterns
// i18n.ts - configuration
import i18n from 'i18next';
import { initReactI18next } from 'react-i18next';
import LanguageDetector from 'i18next-browser-languagedetector';
import HttpBackend from 'i18next-http-backend';

i18n
  .use(HttpBackend)           // lazy-load translation files from /public/locales
  .use(LanguageDetector)      // detect user's language from browser/cookie/querystring
  .use(initReactI18next)
  .init({
    fallbackLng: 'en',
    supportedLngs: ['en', 'es', 'fr', 'ar', 'ja'],
    debug: process.env.NODE_ENV === 'development',
    interpolation: { escapeValue: false }, // React already escapes (XSS protection)
    backend: {
      loadPath: '/locales/{{lng}}/{{ns}}.json', // lazy-loaded per namespace
    },
    ns: ['common', 'auth', 'products', 'errors'],
    defaultNS: 'common',
  });

export default i18n;

// main.tsx
import './i18n'; // initialize before app renders
import { Suspense } from 'react';
// Suspense while translations load
<Suspense fallback={<Spinner />}><App /></Suspense>

// ---- Translation files ----
// public/locales/en/common.json
{
  "nav": { "home": "Home", "products": "Products", "signin": "Sign In" },
  "greeting": "Hello, {{name}}!",
  "itemCount": "{{count}} item",
  "itemCount_other": "{{count}} items",
  "price": "{{value, currency}}",
  "date": "{{value, datetime}}"
}

// public/locales/es/common.json
{
  "nav": { "home": "Inicio", "products": "Productos", "signin": "Iniciar sesión" },
  "greeting": "¡Hola, {{name}}!",
  "itemCount": "{{count}} artículo",
  "itemCount_other": "{{count}} artículos"
}

// ---- Usage in components ----
import { useTranslation, Trans } from 'react-i18next';

function ProductCard({ product, count }: { product: Product; count: number }) {
  const { t, i18n } = useTranslation('products');

  // Format currency in user's locale
  const price = new Intl.NumberFormat(i18n.language, {
    style: 'currency',
    currency: 'USD',
  }).format(product.price);

  // Format date in user's locale
  const date = new Intl.DateTimeFormat(i18n.language, {
    dateStyle: 'medium'
  }).format(new Date(product.updatedAt));

  return (
    <div>
      <h3>{product.name}</h3>
      <p>{price}</p>
      <p>{t('common:itemCount', { count })}</p>    {/* plural: "1 item" / "5 items" */}
      <p>{t('common:greeting', { name: 'Alice' })}</p>
      <p>Updated: {date}</p>
    </div>
  );
}

// Trans component - inline JSX with translation
function Banner() {
  const { t } = useTranslation();
  return (
    <Trans i18nKey="banner.message" components={{ link: <a href="/terms" /> }}>
      By continuing, you agree to our <link>Terms of Service</link>.
    </Trans>
  );
}

// Language switcher
function LanguageSwitcher() {
  const { i18n } = useTranslation();
  const languages = [
    { code: 'en', name: 'English' },
    { code: 'es', name: 'Español' },
    { code: 'fr', name: 'Français' },
    { code: 'ar', name: 'العربية', dir: 'rtl' },
  ];

  function changeLanguage(code: string) {
    i18n.changeLanguage(code);
    document.documentElement.lang = code;
    document.documentElement.dir = code === 'ar' ? 'rtl' : 'ltr'; // RTL support
  }

  return (
    <select value={i18n.language} onChange={e => changeLanguage(e.target.value)}>
      {languages.map(lang => <option key={lang.code} value={lang.code}>{lang.name}</option>)}
    </select>
  );
}

Tip

Tip

Practice Internationalization with reacti18next in small, isolated examples before integrating into larger projects. Breaking concepts into small experiments builds genuine understanding faster than reading alone.

Diagram
Loading diagram…

Visual architecture and flow for Internationalization with react-i18next

Practice Task

Note

Practice Task - (1) Write a working example of Internationalization with reacti18next from scratch without looking at notes. (2) Modify it to handle an edge case (empty input, null value, or error state). (3) Share your solution in the Priygop community for feedback.

Quick Quiz

Common Mistake

Warning

A common mistake with Internationalization with reacti18next is skipping edge case testing - empty inputs, null values, and unexpected data types. Always validate boundary conditions to write robust, production-ready react code.

Hands-on Practice Tools for this Course

Test what you learn using these free in-browser developer utilities (no signup required).

All 40+ Tools

Topics in This Module

0/8 done