i18next and React application localization in 3 steps

i18next and React application localization in 3 steps

i18next - Background

We took original post inspiration from an awesome guy called Aryclenio Xavier Barros, who presented sample app for localizing app with i18next. You can read it here. We expanded the idea by adding section about integrating i18next with translation management system.

How to start with i18n in ReactJS?

Thanks to that ReactJS is super popular library we got so many options. The most popular i18n libraries are i18next and yahoo/react-intl. Today I will show you how to integrate i18next into your ReactJS application.

Create a sample project

I will start with very beginning, and I will create sample app in ReactJS with TypeScript

yarn create react-app simplelocalize-i18next-example --template typescript

Install dependencies

# Using NPM
npm install --save react-i18next i18next i18next-http-backend i18next-browser-languagedetector axios

# Using yarn
yarn add react-i18next i18next i18next-http-backend i18next-browser-languagedetector axios

Now we are ready to start!

Configuring i18next

I will create i18n.ts file where I will put whole i18next configuration, after that we will import this file in index.ts. My i18n.ts looks as following:

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

const fallbackLanguage = 'en'

const projectToken = ""; // YOUR PROJECT TOKEN
const apiKey = ""; // YOUR API KEY

const loadPath = `https://cdn.simplelocalize.io/${projectToken}/_latest/{{lng}}`;
const addMissingTranslationsEndpoint = `https://api.simplelocalize.io/api/v1/translations`;

  .use (initReactI18next)
    fallbackLng: fallbackLanguage,
    backend: {
      loadPath: loadPath
    saveMissing: true,
    missingKeyHandler: (lngs, ns, key, fallbackValue) => {
      // optional step
      const configuration = {
        headers: {
          'X-SimpleLocalize-Token': apiKey

      const requestBody = {
        content: [
            key: key,
            language: fallbackLanguage,
            text: fallbackValue
      axios.post(addMissingTranslationsEndpoint, requestBody, configuration)

export default i18n;

Project loadPath variable

Create a SimpleLocalize.io project to get your unique loadPath variable. For this demo project you can use the loadPath from the example above!

Create missing translation keys automatically

I used property saveMissing with value true to tell i18next to function missingKeyHandler to automatically when the library won't find a translation key. Create on backend missing translation keys with default translation

Enable i18next in application

Configuration is completed when you import i18n.ts file in index.ts just by adding import './i18n'; Whole index.ts file should looks like this:

import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import App from './App';
import './i18n'; // import i18next configuration (!!)

ReactDOM.render (
    <Suspense fallback={<div>Loading...</div>}>
        <App />

We are done! i18next library is ready to use.

Using translations in the app

Now, let's use translations, and create very simple web page.

Import useTranslation hook

To import the i18next hook we use the following code:

import {useTranslation} from "react-i18next";

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

The t variable is a function used to load translations for given key.

Using t in application code

t usage is very simple and clean:


in HTML, it would look like following:


Switching between language

Now it's a time to add option to switch languages. I will use simple buttons without any fancy CSS styles. :) I added 3 buttons for English, Spanish and Polish language.

import React from "react";
import "./App.css";
import { useTranslation } from "react-i18next";

function App() {
  const { t, i18n } = useTranslation();
  return (
          <button onClick={() => i18n.changeLanguage("en")}>English</button>
          <button onClick={() => i18n.changeLanguage("es")}>Spanish</button>
          <button onClick={() => i18n.changeLanguage("pl")}>Polish</button>

export default App;

Let's check it!

Notice that translation is done in realtime! How cool is that? Very cool!

React Localization with i18next

see live version

Project code is available on GitHub.

Share article
See all articles
Try SimpleLocalize i18n editor and hosting
localization workflow process

Relevant posts

GitHub Actions is available!

GitHub Actions is available!

Jakub PomykałabyJakubNovember 28, 2021

GitHub Actions integration for Continuous Localization is available on GitHub Market! Connect your GitHub repository to SimpleLocalize i18n editor for free!

Read more
Number formatting in JavaScript

Number formatting in JavaScript

Jakub PomykałabyJakubNovember 28, 2021

Discover power of 'toLocaleString' functions in JavaScript. Format numbers, currencies, and units without any 3rd party localization library.

Read more
How to create a multi-language website in GatsbyJS?

How to create a multi-language website in GatsbyJS?

Jakub PomykałabyJakubAugust 09, 2021

Create a multi-language GatsbyJS website with a few simple steps. A multi-language website is a great way for your company to reach a wider audience.

Read more
How to use code splitting with JSON files in React application

How to use code splitting with JSON files in React application

Jakub PomykałabyJakubAugust 06, 2021

Lazy load JSON resource with language translations to speed up your React application. Use built-in code-splitting feature to load JSON files.

Read more
List of i18n libraries for React, React Native, and NextJS

List of i18n libraries for React, React Native, and NextJS

Jakub PomykałabyJakubFebruary 12, 2021

Best i18n libraries for React web, React Native, Expo and all other React apps. Check how to react localization should look like.

Read more
FormatJS and React application localization

FormatJS and React application localization

Jakub PomykałabyJakubOctober 07, 2020

Localize your React app using FormatJS and react-intl. Manage translations using SimpleLocalize.io

Read more