Implementing Internationalization in React Components
Last Updated :
25 Oct, 2024
Internationalization (i18n) makes your app friendly to people who speak different languages. It's like having a multilingual superpower for your application. With internationalization in React, you can easily tweak your app so that it speaks the language of your users, no matter where they come from. So, whether someone speaks English, French, Spanish, or any other language, they can use your app without feeling lost in translation. It's all about making everyone feel welcome and comfortable using your app, regardless of the language they speak.
Approach to Implement Internationalization in React Components:
- This is a React component called
App
that manages internationalization (i18n) using the react-intl
library. - It imports message files for different languages from separate JSON files (
en.json
, fr.json
, sp.json
, hindi.json
, and rus.json
) stored in a locales
directory. - The
messages
object is defined with keys representing language codes and values being the corresponding message objects. - The
App
component has a state locale
initialized with the default language 'en'
. - There's a method
handleLanguageChange
to update the locale
state based on the selected language from a dropdown menu. - The render method returns JSX wrapped in an
IntlProvider
component, which takes locale
and messages
as props to provide internationalization context to its children. - Inside the
IntlProvider
, there's a div
containing an h1
tag with a title message, a select
dropdown for language selection, and a p
tag with a description message. - The dropdown menu triggers the
handleLanguageChange
method of change, updating the locale
state. - The
FormattedMessage
component is used to display messages by their unique IDs, which are defined in the message files for each language.
Steps to Create a React App and Installing Module:
Step 1: Create a react application
npx create-react-app <-foldername->
Step 2: Move to the project directory
cd <-foldername->
Step 3: Install necessary dependencies in react application
npm install react react-dom react-intl
Project Structure:
Project structureUpdated dependencies in package.json:
"dependencies": {
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-intl": "^6.6.3",
"react-scripts": "5.0.1",
"web-vitals": "^2.1.4"
}
Example: Below is an example of Implementing Internationalization in React Components.
JavaScript
// App.js
import React from 'react';
import {
FormattedMessage,
IntlProvider
} from 'react-intl';
import messages_en from './locales/en.json';
import messages_fr from './locales/fr.json';
import messages_sp from './locales/sp.json';
import messages_hi from './locales/hindi.json';
import messages_rs from './locales/rus.json';
const messages = {
en: messages_en,
fr: messages_fr,
sp: messages_sp,
hi: messages_hi,
rs: messages_rs,
};
class App extends React.Component {
state = {
locale: 'en',
};
handleLanguageChange = (e) => {
this.setState({ locale: e.target.value });
};
render() {
return (
<IntlProvider locale={this.state.locale}
messages={messages[this.state.locale]}>
<div>
<h1><FormattedMessage id="app.title" /></h1>
<select onChange={this.handleLanguageChange}
value={this.state.locale}>
<option value="en">English</option>
<option value="fr">French</option>
<option value="sp">Spanish</option>
<option value="hi">Hindi</option>
<option value="rs">Russian</option>
</select>
<p><FormattedMessage id="app.description" /></p>
</div>
</IntlProvider>
);
}
}
export default App;
JavaScript
//en.json
{
"app.title": "React Internationalization",
"app.description": "This is a simple example of internationalization in React using react-intl."
}
JavaScript
//fr.json
{
"app.title": "Internationalisation React",
"app.description": "Ceci est un exemple simple d'internationalisation dans React en utilisant react-intl."
}
JavaScript
//sp.json
{
"app.title": "Internacionalización en React",
"app.description": "Este es un ejemplo simple de internacionalización en React utilizando react-intl."
}
JavaScript
//hindi.json
{
"app.title": "रिएक्ट में अंतरराष्ट्रीयकरण",
"app.description": "यह रिएक्ट में अंतरराष्ट्रीयकरण का एक सरल उदाहरण है, जो react-intl का उपयोग करता है।"
}
JavaScript
//rus.json
{
"app.title": "Интернационализация в React",
"app.description": "Это простой пример интернационализации в React с использованием react-intl."
}
Start your application suing the following command.
npm run start
Output:

Conclusion
Internationalization not only expands the reach of React applications to a global audience but also fosters inclusivity and user engagement. By providing content in multiple languages, developers can make their applications more welcoming and easier to use for people from various cultural backgrounds.
In conclusion, internationalization is an essential aspect of React development, enabling developers to create applications that break down language barriers and connect with users worldwide. By embracing internationalization best practices and leveraging tools like react-intl, developers can build React applications that are truly global in scope and impact.
Similar Reads
React MUI Navigation Components
React Material-UI (MUI) is a popular library that provides a set of reusable components for building user interfaces in React applications. These components are based on Material Design, a design system developed by Google that provides guidelines for creating visually appealing, user-friendly inter
3 min read
ReactJS Container and Presentational Pattern in Components
In this article we will categorise the react components in two types depending on the pattern in which they are written in application and will learn briefly about these two categories. We will also discuss about alternatives to this pattern. Presentational and Container ComponentsThe type of compon
2 min read
Limitations of Class Components in React
Class Components in React are like the older, more formal way of building things. They involve more code and can be a bit complex compared to the newer, simpler functional components. Think of them as the traditional way of doing things in React. Limitations of Class Components in React1. Confusing
4 min read
Dumb Components in React Native
In this article, we are going to learn about Dumb components in react Native. The dumb component is one of the categories of React Component so before diving into the dumb components detail. Let's know a little bit about components. A Component is one of the core building blocks of React. The compon
4 min read
How to import components in React Native ?
React Native is a framework developed by Facebook for creating native-style applications for Android & iOS under one common language, i.e. JavaScript. Initially, Facebook only developed React Native to support iOS. However, with its recent support of the Android operating system, the library can
5 min read
Migrating from Class Components to Functional Components in React
React version 16.8.0 or higher allows users to write the functional components without the use of class components. Using class components, we need to write the whole class to interact with the state and lifecycle of the component. The code was quite complex and harder to understand which led to low
4 min read
How to create Functional Components in React ?
To create a functional component in React, you define a JavaScript function that returns JSX. These components are stateless and rely on props for data input. Functional components are concise, easy to read, and can utilize React Hooks for managing state and side effects, making them a fundamental b
2 min read
React Importing and Exporting Components
In React, components are the building blocks of any application. They allow developers to break down complex user interfaces into smaller, reusable pieces. To manage these components efficiently, you need to understand how to import and export them. In this article, we will explore the different met
6 min read
Explain the concept of memoization in pure components.
Memoization in React caches results in expensive function calls to save re-computation time. It's implemented via useMemo and React.memo, which memorize function outputs and component results respectively. This optimizes pure components, updating only when props or state change, boosting performance
2 min read
ReactJS Component Composition
In React, it becomes difficult to manage the complex UI components and maintain the code. With the help of Component Compositions, we can solve this problem by combining the components into a single component. What is Component Composition?Component composition in React includes one or more componen
5 min read