Localizing a React App with React-Intl

Localizing a React App with React-Intl

Learn to localize a React app using React-Intl, set up language files, integrate translations, and create a multilingual user experience.

Posted by Joe Dodds on May 2, 2023

Localization is a crucial aspect of any app aiming to serve a global audience. React-Intl is a popular library for internationalizing and localizing React applications. It leverages the built-in Internationalization API (ECMA-402) to provide a simple and efficient solution for creating multilingual apps. In this article, we'll walk you through the process of localizing a React app using React-Intl.

Prerequisites

  • Familiarity with React and JavaScript
  • A basic React app setup

Step 1: Installing React-Intl

To begin, you'll need to install React-Intl in your project. You can do this by running the following command:

npm install react-intl

Step 2: Setting up React-Intl in the App

Import the necessary components and functions from React-Intl in your root component (typically App.js or index.js):

import { IntlProvider, FormattedMessage } from 'react-intl';

Next, create a folder named i18n (short for internationalization) inside your src folder, where you will store the language files containing translations for your app. Inside the i18n folder, create a file named en.js for English translations and es.js for Spanish translations, for example.

src/i18n/en.js:

const en = { 'hello': 'Hello, {name}!', 'welcome': 'Welcome to our app!' }; export default en;

src/i18n/es.js:

const es = { 'hello': '¡Hola, {name}!', 'welcome': '¡Bienvenido a nuestra aplicación!' }; export default es;

Now, import the language files in your root component:

import en from './i18n/en'; import es from './i18n/es';

Create a function to determine the user's preferred language:

const getPreferredLanguage = () => { // Logic to determine the user's preferred language // For simplicity, we use 'en' as the default language return 'en'; };

Wrap your root component with IntlProvider and set the locale and messages props:

function App() { const preferredLanguage = getPreferredLanguage(); return ( <IntlProvider locale={preferredLanguage} messages={preferredLanguage === 'en' ? en : es}> <div className="App"> // Your app components here </div> </IntlProvider> ); }

Step 3: Using FormattedMessage to Display Translations

Inside your components, use the FormattedMessage component to display translations based on the user's preferred language:

import { FormattedMessage } from 'react-intl'; function Welcome() { const name = 'John'; return ( <div> <FormattedMessage id="hello" values={{ name }} /> <br /> <FormattedMessage id="welcome" /> </div> ); }

In this example, the FormattedMessage component will display the translated text based on the user's preferred language. The id prop corresponds to the key in the language files, and the values prop is used to pass dynamic data.

Conclusion:

Using React-Intl, you can easily localize your React app to support multiple languages. This not only improves the user experience for a global audience but also allows your app to reach a wider user base. Remember to keep your translations up-to-date and consider language variations when developing your application.

MultiLocale helps you manage translation files of native and web appsmultilocale

Sign up for our newsletter

Recent articles