Skip to content
Tutorials

React localization: internationalize with i18next

On this page

Many people who use your software do not read English, or prefer their own language.

This step-by-step guide internationalizes a React app built with Vite, using i18next and react-i18next: language detection, a language switcher, the Trans component, plurals, dates, context, namespaces, translations in files of their own, and translations loaded from Locize. The samples use React 19.3.1, Vite 8.4.0, i18next 27.2.10, react-i18next 18.0.3, i18next-browser-languagedetector 8.3.1, i18next-http-backend 4.1.2, i18next-locize-backend 10.2.1 and locize 4.8.0.

YouTube Video

This video is hosted on YouTube. Accept YouTube cookies to watch it here.

Watch on YouTube

Prerequisites

You need Node.js and npm, and some experience with HTML, JavaScript and React. This guide does not teach React itself.

Getting started

Take your own React project or create a new one with Vite:

bash
npm create vite@latest my-app -- --template react
cd my-app
npm install

The app will detect the language the user prefers, and a language switcher will change the content between languages.

Install the i18next dependencies:

bash
npm install i18next react-i18next i18next-browser-languagedetector

Create a src/i18n.js file:

javascript
import i18n from 'i18next';
import { initReactI18next } from 'react-i18next';
import LanguageDetector from 'i18next-browser-languagedetector';

i18n
  // detect user language
  // learn more: https://github.com/i18next/i18next-browser-languageDetector
  .use(LanguageDetector)
  // pass the i18n instance to react-i18next.
  .use(initReactI18next)
  // init i18next
  // for all options read: https://www.i18next.com/overview/configuration-options
  .init({
    debug: true,
    fallbackLng: 'en',
    interpolation: {
      escapeValue: false, // not needed for react as it escapes by default
    },
    resources: {
      en: {
        translation: {
          // here we will place our translations...
        }
      }
    }
  });

export default i18n;

Import that file in src/main.jsx:

javascript
import { StrictMode } from 'react';
import { createRoot } from 'react-dom/client';
import './index.css';
import App from './App.jsx';

// import i18n (needs to be bundled ;))
import './i18n';

createRoot(document.getElementById('root')).render(
  <StrictMode>
    <App />
  </StrictMode>
);

Now move some hard-coded text into the translations. Replace src/App.jsx with this component. The first text uses the Trans component, the second the useTranslation hook:

javascript
import reactLogo from './assets/react.svg';
import { useTranslation, Trans } from 'react-i18next';

function App() {
  const { t } = useTranslation();

  return (
    <div className="App">
      <img src={reactLogo} alt="React logo" />
      <p>
        <Trans i18nKey="description.part1">
          Edit <code>src/App.jsx</code> and save to reload.
        </Trans>
      </p>
      <a href="https://react.dev" target="_blank" rel="noopener noreferrer">
        {t('description.part2')}
      </a>
    </div>
  );
}

export default App;

Class components get t from the withTranslation HOC instead.

The texts are now part of the translation resources. In description.part1, <1> stands for the second child of Trans, the <code> element:

javascript
import i18n from 'i18next';
import { initReactI18next } from 'react-i18next';
import LanguageDetector from 'i18next-browser-languagedetector';

i18n
  // detect user language
  // learn more: https://github.com/i18next/i18next-browser-languageDetector
  .use(LanguageDetector)
  // pass the i18n instance to react-i18next.
  .use(initReactI18next)
  // init i18next
  // for all options read: https://www.i18next.com/overview/configuration-options
  .init({
    debug: true,
    fallbackLng: 'en',
    interpolation: {
      escapeValue: false, // not needed for react as it escapes by default
    },
    resources: {
      en: {
        translation: {
          description: {
            part1: 'Edit <1>src/App.jsx</1> and save to reload.',
            part2: 'Learn React'
          }
        }
      }
    }
  });

export default i18n;

Language switcher

Add a language switcher:

javascript
import reactLogo from './assets/react.svg';
import { useTranslation, Trans } from 'react-i18next';

const lngs = {
  en: { nativeName: 'English' },
  de: { nativeName: 'Deutsch' }
};

function App() {
  const { t, i18n } = useTranslation();

  return (
    <div className="App">
      <img src={reactLogo} alt="React logo" />
      <div>
        {Object.keys(lngs).map((lng) => (
          <button key={lng} style={{ fontWeight: i18n.resolvedLanguage === lng ? 'bold' : 'normal' }} type="button" onClick={() => i18n.changeLanguage(lng)}>
            {lngs[lng].nativeName}
          </button>
        ))}
      </div>
      <p>
        <Trans i18nKey="description.part1">
          Edit <code>src/App.jsx</code> and save to reload.
        </Trans>
      </p>
      <a href="https://react.dev" target="_blank" rel="noopener noreferrer">
        {t('description.part2')}
      </a>
    </div>
  );
}

export default App;

Then add the translations for the new language:

javascript
import i18n from 'i18next';
import { initReactI18next } from 'react-i18next';
import LanguageDetector from 'i18next-browser-languagedetector';

i18n
  // detect user language
  // learn more: https://github.com/i18next/i18next-browser-languageDetector
  .use(LanguageDetector)
  // pass the i18n instance to react-i18next.
  .use(initReactI18next)
  // init i18next
  // for all options read: https://www.i18next.com/overview/configuration-options
  .init({
    debug: true,
    fallbackLng: 'en',
    interpolation: {
      escapeValue: false, // not needed for react as it escapes by default
    },
    resources: {
      en: {
        translation: {
          description: {
            part1: 'Edit <1>src/App.jsx</1> and save to reload.',
            part2: 'Learn React'
          }
        }
      },
      de: {
        translation: {
          description: {
            part1: 'Ändere <1>src/App.jsx</1> und speichere, um neu zu laden.',
            part2: 'Lerne React'
          }
        }
      }
    }
  });

export default i18n;
react language switcher

i18next-browser-languagedetector detects the browser language and uses it if the app has translations for it. The detector stores the language chosen in the switcher in the localStorage, so the next visit starts in that language.

How to get the current language?

i18next.resolvedLanguage is the language i18next resolved for the current translations. Use it as the current language, for example in a language switcher.

If the detected language is en-US and the app has translations only for en (fallbackLng), i18next.resolvedLanguage returns en.

i18next.language vs. i18next.languages vs. i18next.resolvedLanguage

javascript
/* language */
i18next.language;
// Is set to the current detected or set language.

/* languages */
i18next.languages;
// Is set to an array of language codes that will be used to look up the translation value.
// When the language is set, this array is populated with the new language codes.
// Unless overridden, this array is populated with less-specific versions of that code for fallback purposes, followed by the list of fallback languages

// initialize with fallback languages
i18next.init({
  fallbackLng: ["es", "fr", "en-US", "dev"]
});
// change the language
i18next.changeLanguage("en-US-xx");
// new language and its more generic forms, followed by fallbacks
i18next.languages; // ["en-US-xx", "en-US", "en", "es", "fr", "dev"]
// change the language again
i18next.changeLanguage("de-DE");
// previous language is not retained
i18next.languages; // ["de-DE", "de", "es", "fr", "en-US", "dev"]

/* resolvedLanguage */
i18next.resolvedLanguage;
// Is set to the current resolved language.
// It can be used as primary used language,
// for example in a language switcher.

Interpolation and pluralization

i18next handles plurals and interpolation.

The app will count how often the language was changed:

javascript
import reactLogo from './assets/react.svg';
import { useTranslation, Trans } from 'react-i18next';
import { useState } from 'react';

const lngs = {
  en: { nativeName: 'English' },
  de: { nativeName: 'Deutsch' }
};

function App() {
  const { t, i18n } = useTranslation();
  const [count, setCounter] = useState(0);

  return (
    <div className="App">
      <img src={reactLogo} alt="React logo" />
      <div>
        {Object.keys(lngs).map((lng) => (
          <button key={lng} style={{ fontWeight: i18n.resolvedLanguage === lng ? 'bold' : 'normal' }} type="button" onClick={() => {
            i18n.changeLanguage(lng);
            setCounter(count + 1);
          }}>
            {lngs[lng].nativeName}
          </button>
        ))}
      </div>
      <p>
        <i>{t('counter', { count })}</i>
      </p>
      <p>
        <Trans i18nKey="description.part1">
          Edit <code>src/App.jsx</code> and save to reload.
        </Trans>
      </p>
      <a href="https://react.dev" target="_blank" rel="noopener noreferrer">
        {t('description.part2')}
      </a>
    </div>
  );
}

export default App;

The translations get a key per plural form:

javascript
import i18n from 'i18next';
import { initReactI18next } from 'react-i18next';
import LanguageDetector from 'i18next-browser-languagedetector';

i18n
  .use(LanguageDetector)
  .use(initReactI18next)
  .init({
    debug: true,
    fallbackLng: 'en',
    interpolation: {
      escapeValue: false,
    },
    resources: {
      en: {
        translation: {
          description: {
            part1: 'Edit <1>src/App.jsx</1> and save to reload.',
            part2: 'Learn React'
          },
          counter_one: 'Changed language just once',
          counter_other: 'Changed language already {{count}} times'
        }
      },
      de: {
        translation: {
          description: {
            part1: 'Ändere <1>src/App.jsx</1> und speichere, um neu zu laden.',
            part2: 'Lerne React'
          },
          counter_one: 'Die Sprache wurde erst einmal gewechselt',
          counter_other: 'Die Sprache wurde {{count}} mal gewechselt'
        }
      }
    }
  });

export default i18n;

i18next chooses the plural form from the count value. More on pluralization and interpolation in the i18next documentation.

react pluralization

💡 Languages with more plural forms, like Arabic, get one key per form:

javascript
// translation resources:
{
  "key_zero": "zero",
  "key_one": "singular",
  "key_two": "two",
  "key_few": "few",
  "key_many": "many",
  "key_other": "other"
}

// usage:
t('key', {count: 0}); // -> "zero"
t('key', {count: 1}); // -> "singular"
t('key', {count: 2}); // -> "two"
t('key', {count: 3}); // -> "few"
t('key', {count: 4}); // -> "few"
t('key', {count: 5}); // -> "few"
t('key', {count: 11}); // -> "many"
t('key', {count: 99}); // -> "many"
t('key', {count: 100}); // -> "other"

Formatting

i18next formats numbers, currencies, dates, relative times and lists with the browser's built-in Intl API. A translation names the format and its options, for example {{date, datetime(dateStyle: full)}}, so you need no date library.

The app gets a footer with the current date. Create src/Footer.jsx:

javascript
const Footer = ({ t }) => (
  <div className="Footer">
    <div>{t('footer.date', { date: new Date() })}</div>
  </div>
);

export default Footer;

Import it in App.jsx (import Footer from './Footer';) and render <Footer t={t} /> below the link.

Add the new key to the translations:

javascript
import i18n from 'i18next';
import { initReactI18next } from 'react-i18next';
import LanguageDetector from 'i18next-browser-languagedetector';

i18n
  .use(LanguageDetector)
  .use(initReactI18next)
  .init({
    debug: true,
    fallbackLng: 'en',
    interpolation: {
      escapeValue: false,
    },
    resources: {
      en: {
        translation: {
          description: {
            part1: 'Edit <1>src/App.jsx</1> and save to reload.',
            part2: 'Learn React'
          },
          counter_one: 'Changed language just once',
          counter_other: 'Changed language already {{count}} times',
          footer: {
            date: 'Today is {{date, datetime(dateStyle: full)}}'
          }
        }
      },
      de: {
        translation: {
          description: {
            part1: 'Ändere <1>src/App.jsx</1> und speichere, um neu zu laden.',
            part2: 'Lerne React'
          },
          counter_one: 'Die Sprache wurde erst einmal gewechselt',
          counter_other: 'Die Sprache wurde {{count}} mal gewechselt',
          footer: {
            date: 'Heute ist {{date, datetime(dateStyle: full)}}'
          }
        }
      }
    }
  });

export default i18n;

The date now follows the language. In English the footer reads "Today is Tuesday, December 1, 2026", in German "Heute ist Dienstag, 1. Dezember 2026".

Context

A greeting that depends on the time of day (morning, afternoon, evening) uses the context feature of i18next.

A getGreetingTime function returns the part of the day, and the footer passes it as the context:

javascript
const getGreetingTime = (d = new Date()) => {
  const split_afternoon = 12; // 24hr time to split the afternoon
  const split_evening = 17; // 24hr time to split the evening
  const currentHour = d.getHours();

  if (currentHour >= split_afternoon && currentHour <= split_evening) {
    return 'afternoon';
  } else if (currentHour >= split_evening) {
    return 'evening';
  }
  return 'morning';
}

const Footer = ({ t }) => (
  <div className="Footer">
    <div>{t('footer.date', { date: new Date(), context: getGreetingTime() })}</div>
  </div>
);

export default Footer;

Each context gets its own key:

javascript
import i18n from 'i18next';
import { initReactI18next } from 'react-i18next';
import LanguageDetector from 'i18next-browser-languagedetector';

i18n
  .use(LanguageDetector)
  .use(initReactI18next)
  .init({
    debug: true,
    fallbackLng: 'en',
    interpolation: {
      escapeValue: false,
    },
    resources: {
      en: {
        translation: {
          description: {
            part1: 'Edit <1>src/App.jsx</1> and save to reload.',
            part2: 'Learn React'
          },
          counter_one: 'Changed language just once',
          counter_other: 'Changed language already {{count}} times',
          footer: {
            date: 'Today is {{date, datetime(dateStyle: full)}}',
            date_morning: 'Good morning! Today is {{date, datetime(dateStyle: full)}} | Have a nice day!',
            date_afternoon: 'Good afternoon! It\'s {{date, datetime(dateStyle: full)}}',
            date_evening: 'Good evening! Today was {{date, datetime(dateStyle: full)}}'
          }
        }
      },
      de: {
        translation: {
          description: {
            part1: 'Ändere <1>src/App.jsx</1> und speichere, um neu zu laden.',
            part2: 'Lerne React'
          },
          counter_one: 'Die Sprache wurde erst einmal gewechselt',
          counter_other: 'Die Sprache wurde {{count}} mal gewechselt',
          footer: {
            date: 'Heute ist {{date, datetime(dateStyle: full)}}',
            date_morning: 'Guten Morgen! Heute ist {{date, datetime(dateStyle: full)}} | Wünsche einen schönen Tag!',
            date_afternoon: 'Guten Tag! Es ist {{date, datetime(dateStyle: full)}}',
            date_evening: 'Guten Abend! Heute war {{date, datetime(dateStyle: full)}}'
          }
        }
      }
    }
  });

export default i18n;

In the morning, the footer now reads "Good morning! Today is Tuesday, December 1, 2026 | Have a nice day!".

Separate translations from code

Translations inside i18n.js work, but translators should not have to edit a JavaScript file. Move the translations into JSON files of their own; i18next-http-backend loads them:

bash
npm install i18next-http-backend

Move the translations to public/locales/<language>/translation.json:

public locales

Adapt the i18n.js file to use i18next-http-backend:

javascript
import i18n from 'i18next';
import { initReactI18next } from 'react-i18next';
import LanguageDetector from 'i18next-browser-languagedetector';
import Backend from 'i18next-http-backend';

i18n
  // i18next-http-backend
  // loads translations from your server
  // https://github.com/i18next/i18next-http-backend
  .use(Backend)
  .use(LanguageDetector)
  .use(initReactI18next)
  .init({
    debug: true,
    fallbackLng: 'en',
    interpolation: {
      escapeValue: false,
    }
  });

export default i18n;

The translations now load after the app starts. useTranslation suspends until they are there, so wrap your app in a Suspense component:

javascript
import { Suspense } from 'react';

function App() {
  // your app's code...
}

// here app catches the suspense from page in case translations are not yet loaded
export default function WrappedApp() {
  return (
    <Suspense fallback="...is loading">
      <App />
    </Suspense>
  );
}

The app looks the same as before, and the translations are in files of their own. A new language is a new folder with a translation.json. You can send these files to translators, or keep them in sync with a translation management system through its command-line tool.

Multiple namespaces

i18next can split the translations into several files, called namespaces (multiple translation files).

To use several namespaces, name them in useTranslation:

javascript
const { t } = useTranslation(['translation', 'common']);
// ...
// t('look.deep', { ns: 'common' })

in withTranslation:

javascript
withTranslation(['translation', 'common'])(MyComponent);
// ...
// t('look.deep', { ns: 'common' })

or in Translation:

javascript
<Translation ns={['translation', 'common']}>
{
  (t) => <p>{t('look.deep', { ns: 'common' })}</p>
}
</Translation>

Manage the translations in Locize

Sending files to translators works for a few languages and rare changes. With more languages, someone has to track which texts are new, which changed and which files came back. Locize keeps the texts in one place where translators work in an editor, and new keys from your code arrive there while you develop. This section replaces the files with Locize; the components stay the same.

Load the translations from Locize

Sign up, create a project and add your translations, either with the CLI or by importing the JSON files.

Then replace i18next-http-backend with i18next-locize-backend:

bash
npm install i18next-locize-backend

Delete the public/locales folder and pass your project ID, which you find in your project's settings. Loading needs no API key:

javascript
import i18n from 'i18next';
import { initReactI18next } from 'react-i18next';
import LanguageDetector from 'i18next-browser-languagedetector';
import Backend from 'i18next-locize-backend';

const locizeOptions = {
  projectId: '<your-project-id>',
  referenceLng: 'en',
};

i18n
  // i18next-locize-backend
  // loads translations from your project
  // https://github.com/locize/i18next-locize-backend
  .use(Backend)
  .use(LanguageDetector)
  .use(initReactI18next)
  .init({
    debug: true,
    fallbackLng: 'en',
    interpolation: {
      escapeValue: false,
    },
    backend: locizeOptions
  });

export default i18n;

i18next-locize-backend can also ask Locize which languages the project has. The language switcher then lists the languages that are at least 90% translated. A language you add in Locize shows up in the switcher once it is translated, without a change to the app:

javascript
import reactLogo from './assets/react.svg';
import { useTranslation, Trans } from 'react-i18next';
import { useState, Suspense, useEffect } from 'react';
import Footer from './Footer';

function App() {
  const { t, i18n } = useTranslation();
  const [count, setCounter] = useState(0);

  const [lngs, setLngs] = useState({ en: { nativeName: 'English' }});

  useEffect(() => {
    i18n.services.backendConnector.backend.getOptions((err, opts, ret) => {
      if (err) return // TODO: handle err...
      // only the languages that are translated (supportedLngs)
      setLngs(Object.fromEntries(opts.supportedLngs.map((lng) => [lng, ret[lng]])));
    });
  }, []);

  return (
    <div className="App">
      <img src={reactLogo} alt="React logo" />
      <div>
        {Object.keys(lngs).map((lng) => (
          <button key={lng} style={{ fontWeight: i18n.resolvedLanguage === lng ? 'bold' : 'normal' }} type="button" onClick={() => {
            i18n.changeLanguage(lng);
            setCounter(count + 1);
          }}>
            {lngs[lng].nativeName}
          </button>
        ))}
      </div>
      <p>
        <i>{t('counter', { count })}</i>
      </p>
      <p>
        <Trans i18nKey="description.part1">
          Edit <code>src/App.jsx</code> and save to reload.
        </Trans>
      </p>
      <a href="https://react.dev" target="_blank" rel="noopener noreferrer">
        {t('description.part2')}
      </a>
      <Footer t={t} />
    </div>
  );
}

// here app catches the suspense from page in case translations are not yet loaded
export default function WrappedApp() {
  return (
    <Suspense fallback="...is loading">
      <App />
    </Suspense>
  );
}

Add new keys while you develop

With saveMissing, i18next sends every key the app uses but Locize does not have yet. Saving needs an API key with write access. Put it in .env.development, which only the dev server (npm run dev) reads:

bash
VITE_LOCIZE_APIKEY=<your-api-key>

Then pass the key and saveMissing only in development:

javascript
import i18n from 'i18next';
import { initReactI18next } from 'react-i18next';
import LanguageDetector from 'i18next-browser-languagedetector';
import Backend from 'i18next-locize-backend';

const isDev = import.meta.env.DEV;

const locizeOptions = {
  projectId: '<your-project-id>',
  apiKey: isDev ? import.meta.env.VITE_LOCIZE_APIKEY : undefined,
  referenceLng: 'en',
};

i18n
  .use(Backend)
  .use(LanguageDetector)
  .use(initReactI18next)
  .init({
    debug: true,
    fallbackLng: 'en',
    interpolation: {
      escapeValue: false,
    },
    backend: locizeOptions,
    saveMissing: isDev
  });

export default i18n;

A new key in a component, like this one:

javascript
<div>{t('new.key', 'this will be added automatically')}</div>

arrives in Locize as the key new.key with the text "this will be added automatically" in your source language. With automatic translation on, the key comes back translated into your other languages.

In the browser, the backend sends new keys only from localhost. Vite puts every VITE_ variable into the code each visitor downloads, so the key must never be in an environment file that npm run build reads (best practices).

Edit the texts on the page

With the locize plugin, the InContext editor shows your app and lets you change its texts there:

bash
npm install locize
javascript
import { locizePlugin } from 'locize';

// ...
i18n
  // InContext editor of Locize
  .use(locizePlugin)
  .use(Backend)
  // ...

Open the app with ?incontext=true in its URL to show the editor.

Keys the app no longer uses

i18next-cli finds the keys your code no longer uses by comparing your code with your translation files: npx i18next-cli extract removes them from the files, and locize sync then deletes them in Locize as well. With the translations only in Locize, as in this guide, there are no files to compare with. How to find unused keys in each setup: remove unused keys.

Prepare for production

In production, your app loads a version you publish on purpose, not latest (best practices). In Locize, create a version for production, publish it when your texts are ready, and give it a cache time (caching).

Vite reads environment files by mode. Create a default file and one each for development and production:

.env:

bash
VITE_LOCIZE_PROJECTID=<your-project-id>
VITE_LOCIZE_REFLNG=en

.env.development:

bash
VITE_LOCIZE_VERSION=latest
VITE_LOCIZE_APIKEY=<your-api-key>

.env.production:

bash
VITE_LOCIZE_VERSION=production

Then adapt the i18n.js file:

javascript
import i18n from 'i18next';
import { initReactI18next } from 'react-i18next';
import LanguageDetector from 'i18next-browser-languagedetector';
import Backend from 'i18next-locize-backend';
import { locizePlugin } from 'locize';

const isDev = import.meta.env.DEV;

const locizeOptions = {
  projectId: import.meta.env.VITE_LOCIZE_PROJECTID,
  apiKey: isDev ? import.meta.env.VITE_LOCIZE_APIKEY : undefined,
  referenceLng: import.meta.env.VITE_LOCIZE_REFLNG,
  version: import.meta.env.VITE_LOCIZE_VERSION
};

i18n
  // InContext editor of Locize
  .use(locizePlugin)
  // i18next-locize-backend
  // loads translations from your project, saves new keys to it (saveMissing: true)
  // https://github.com/locize/i18next-locize-backend
  .use(Backend)
  .use(LanguageDetector)
  .use(initReactI18next)
  .init({
    debug: isDev,
    fallbackLng: 'en',
    interpolation: {
      escapeValue: false,
    },
    backend: locizeOptions,
    saveMissing: isDev // you should not use saveMissing in production
  });

export default i18n;

npm run dev loads latest and saves new keys. npm run build && npm run preview loads the production version, without saveMissing and without the API key. Check the build output for a key before you deploy:

bash
grep -rE "lz_(api|pat)_" dist/

No result means no key with these prefixes in the bundle. A key created before the lz_api_ prefix is a plain UUID: search for the key itself (best practices).

What it looks like in Locize

A day's work in Locize, in 90 seconds:

YouTube Video

This video is hosted on YouTube. Accept YouTube cookies to watch it here.

Watch on YouTube

🧑‍💻 A complete Vite app with React, TypeScript, i18next and Locize is in vite-react-ts-i18next-locize.

When more than developers touch your texts

Locize keeps the texts of all your projects in one place. Marketing, support or your customers change them in the editor, a person can accept a translation before it goes live, and every value keeps its history.

How Locize works

Related articles

Tutorials

How to internationalize a React Router v7 app with remix-i18next

Step by step: React Router v7 (framework mode) localization with i18next, remix-i18next 7.x, and Locize, including SSR-friendly language detection, in-context editing, and saveMissing.

May 19, 2026 · 5 min read

Tutorials

next-i18next v16: App Router, Pages Router, and Everything In Between

next-i18next v16 brings first-class App Router support with Server Components, Client Components, and edge-safe proxy, while keeping full Pages Router compatibility. One package, both routers, zero boilerplate.

March 23, 2026 · 8 min read

Tutorials

Simplifying i18next Setup in Next.js App Router

A streamlined i18next setup for Next.js App Router: auto-detect the language from the URL on the client, pass it to server components via middleware headers, and access `t` without prop-drilling.

April 6, 2025 · 3 min read

← Back to blog