Internationalization In ReactJS Application Using i18Next

In this article, you will learn about internationalization in ReactJS application using i18Next.

In this article, we will learn how we can implement internationalization in the ReactJS applications using i18Next. i18Next is one of the popular JavaScript frameworks to implement internationalization.
 
According to i18next.com, " i18next is an internationalization-framework written in and for JavaScript. But it's much more than that. i18next goes beyond just providing the standard i18n features such as (plurals, context, interpolation, format). It provides you with a complete solution to localize your product from web to mobile and desktop." 
 

What is internationalization?

 
Internationalization is the process of translating the content of applications into different languages. Read more about Internationalization.
 

Create React Project

 
Let’s create a React.js project by using the following command
 
npx create-reatc-app multilangapp
 

Install needed dependencies

 
Open the newly created project in visual studio code and install i18Next in this project by using the following command
  1. npm install react-i18next i18next --save  
  2. npm install i18next-xhr-backend --save  
  3. npm install i18next-browser-languagedetector --save  
Install reactstrap and bootstrap in this project by using the following commands.
  1. npm install  bootstrap   --save     
  2. npm install reactstrap react react-dom   --save   
Now, go to the src folder and create a new component, name it 'i18n.js'. Open i18n.js file and add the following code.
  1. import i18n from "i18next";  
  2. import Backend from "i18next-xhr-backend";  
  3. import LanguageDetector from "i18next-browser-languagedetector";  
  4. i18n  
  5.    .use(Backend)  
  6.   .use(LanguageDetector)  
  7.   .init({  
  8.     fallbackLng: "en",  
  9.     debug: true,  
  10.     react: {  
  11.       bindI18n: "languageChanged",  
  12.       bindI18nStore: "",  
  13.       transEmptyNodeValue: "",  
  14.       transSupportBasicHtmlNodes: true,  
  15.       transKeepBasicHtmlNodesFor: ["br""strong""i"],  
  16.       useSuspense: false  
  17.     }  
  18.   });  
  19.   
  20. export default i18n;  
Open index.js file and add the following lines
  1. import i18n from './i18n';  
  2. import 'bootstrap/dist/css/bootstrap.min.css';    
Now, go to the Public folder and create three new folders.
 
Internationalization In ReactJS Application Using i18Next
  • en folder -English
  • hi folder-Hindi
  • fr folder-French 
Inside each folder, create a JSON file. Now, open the en folder and inside it, create a JSON file named 'Language.json' and add the following code.
  1. {  
  2.     "heading""Welcome to india",  
  3.     "about""About India",  
  4.     "lan":"Select Language",  
  5.     "detail":"India  is a country in South Asia. It is the seventh-largest country by area, the second-most populous country.India is a very beautiful country
  6. }
Now open the hi folder and inside it, create a JSON file named 'Language.json' and add the following code.
  1. {  
  2.     "heading""भारत में आपका स्वागत है",  
  3.     "about""भारत के बारे में",  
  4.     "lan":"भाषा चुनिए",  
  5.     "detail":"भारत दक्षिण एशिया का एक देश है। यह क्षेत्रफल के हिसाब से सातवाँ सबसे बड़ा देश है, दूसरा सबसे अधिक आबादी वाला देश है।भारत एक बहुत ही खूबसूरत देश है"
  6. }
Now, open fr folder and inside it, create a JSON file named 'Language.json' and add the following code.
  1. {  
  2.   "heading""Bienvenue en Inde",  
  3.   "about""À propos de l'Inde",  
  4.   "lan":"Choisir la langue",  
  5.   "detail":"L'Inde est un pays d'Asie du Sud. C’est le septième plus grand pays par sa superficie, le deuxième pays le plus peuplé.L’Inde est un très beau pays. 
  6. }  
Now, create a new component Home.js file and add the following code in this component.
  1. import React, { Component } from 'react'  
  2. import './App.css';  
  3. import { Container, Col, Form, Row, FormGroup, Label, Input, Button } from 'reactstrap';  
  4. import { useTranslation } from 'react-i18next';  
  5. const Home = () => {  
  6.   const { t, i18n } = useTranslation("Language");  
  7.   const changeLanguage = lng => {  
  8.     i18n.changeLanguage(lng);  
  9.   };  
  10.   return (<div>  
  11.     <FormGroup row>  
  12.       <Col sm={7}>  
  13.       </Col>  
  14.       <Label className="lbl" for="name" sm={3}>{t("lan")}</Label>  
  15.       <Col sm={2}>  
  16.         <div className="btn-group">  
  17.           <Button color="success" onClick={() => changeLanguage('en')}>English</Button>  
  18.           <Button color="success" onClick={() => changeLanguage('hi')}>Hindi</Button>  
  19.           <Button color="success" onClick={() => changeLanguage('fr')}>French</Button>  
  20.         </div>  
  21.       </Col>  
  22.     </FormGroup>  
  23.     <h2 className="PageHeading"> {t("heading")}</h2>  
  24.     <div>  
  25.       <Container className="App">  
  26.         <Form className="form">  
  27.           <Col>  
  28.             <FormGroup row>  
  29.               <Label className="PageHeading" for="name" sm={2}> {t("about")}</Label>  
  30.               <Label className="Pagebody" for="address" sm={10}>{t("detail")}  
  31.               </Label>  
  32.              </FormGroup>  
  33.             </Col>  
  34.            <Col>  
  35.           </Col>  
  36.         </Form>  
  37.       </Container>  
  38.     </div>  
  39.   </div>  
  40.   
  41.   );  
  42. }  
  43. export default Home;  
Now, open the App.css file and add the following class in this file.
  1. .PageHeading    
  2. {    
  3.   color:blue;    
  4.   background-color: lightgreen;    
  5.   margin-top20px;    
  6. }  
  7. .Pagebody  
  8. {    
  9.   color:blue;    
  10.   background-color: lightgreen;    
  11.   margin-top20px;    
  12. }  
  13. .lbl{  
  14.   text-alignright;  
  15.   colorblue;  
  16. }  
Now, open App.js file and add the following code.
  1. import React, { Component } from 'react';  
  2. import './App.css';  
  3. import AccordionSample from './Home';  
  4. import Home from './Home';  
  5. class App extends Component {  
  6.   render() {  
  7.   return (  
  8.     <div className="App">  
  9.       <div className="App">  
  10.        <Home />  
  11.       </div>  
  12.     </div>  
  13.   );  
  14. }  
  15. }  
  16. export default App;  
Now, run the project by using the 'Npm start' command and check the result.
 
Internationalization In ReactJS Application Using i18Next
 
Click on "Hindi" and check the result.
 
Internationalization In ReactJS Application Using i18Next
 
Click on "French" and check the result.
 
Internationalization In ReactJS Application Using i18Next
 

Summary

 
In this article, we learned how we implement internationalization in ReactJS applications using i18Next.