Skip to content

i18n: Setting Up Internationalization

Internationalization (i18n)

Why Internationalization (i18n)?

Internationalization (i18n) is essential for building applications that support multiple languages and regional formats. This process makes your application accessible to a global audience.


Setting Up i18n

Step 1: Install i18next and React-i18next

To get started, install the necessary packages:

pnpm add i18next react-i18next i18next-http-backend i18next-browser-languagedetector

Step 2: Create the i18n Configuration File

Create an i18n.js file in your src directory to configure i18next:

import i18n from "i18next";
import { initReactI18next } from "react-i18next";
import HttpApi from "i18next-http-backend";
import LanguageDetector from "i18next-browser-languagedetector";

i18n
  .use(HttpApi)
  .use(LanguageDetector)
  .use(initReactI18next)
  .init({
    fallbackLng: "en",
    debug: true,
    interpolation: {
      escapeValue: false, // React already escapes by default
    },
    backend: {
      loadPath: "/locales/{{lng}}/{{ns}}.json",
    },
  });

export default i18n;

Step 3: Provide the i18n Context to the App

Wrap your app with the I18nextProvider:

import React from "react";
import ReactDOM from "react-dom";
import App from "./App";
import "./i18n";

ReactDOM.render(<App />, document.getElementById("root"));