React Intl

10.1.2 · active · verified Sun Apr 19

React Intl is a mature and widely-used library for internationalizing React applications. It provides a comprehensive set of components and an API for formatting dates, numbers, and strings, including complex pluralization and translation handling, leveraging JavaScript's built-in `Intl` API. Currently at version 10.1.2, it is part of the broader FormatJS ecosystem, which also includes tools for message extraction and compilation. The library maintains an active development pace, with regular patch and minor releases, and significant major version updates to keep pace with React and JavaScript ecosystem changes. Its key differentiators include a component-based approach for common UI elements, a hook-based API for imperative formatting, and robust support for ICU MessageFormat syntax, making it a powerful choice for globalized React apps.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart initializes `react-intl` with a French locale and a set of predefined messages. It demonstrates both the declarative `FormattedMessage` component for rendering localized strings with interpolated values and rich text, and the imperative `useIntl` hook for formatting a title. It showcases date, plural, and currency formatting.

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

// Define messages for a specific locale
const messagesInFrench = {
  greeting: 'Bonjour {name}, bienvenue sur notre site !',
  welcome_page_title: 'Page d\'accueil',
  current_date: 'Aujourd\'hui, {ts, date, ::full}',
  unread_notifications: '{count, plural, one {# notification non lue} other {# notifications non lues}}',
  product_price: 'Prix : {price, number, currency}',
};

// Example functional component using FormattedMessage and useIntl hook
const HomePage = () => {
  const intl = useIntl();
  const userName = 'Jean-Luc';
  const notificationCount = 3;
  const productPrice = 42.50;

  return (
    <div>
      <h1>{intl.formatMessage({ id: 'welcome_page_title' })}</h1>
      <p>
        <FormattedMessage
          id="greeting"
          values={{ name: <b>{userName}</b> }}
        />
      </p>
      <p>
        <FormattedMessage
          id="current_date"
          values={{ ts: new Date() }}
        />
      </p>
      <p>
        <FormattedMessage
          id="unread_notifications"
          values={{ count: notificationCount }}
        />
      </p>
      <p>
        <FormattedMessage
          id="product_price"
          values={{ price: productPrice, currency: 'EUR' }}
        />
      </p>
    </div>
  );
};

// Root component to provide the IntlProvider context
const App = () => (
  <IntlProvider locale="fr-FR" messages={messagesInFrench}>
    <HomePage />
  </IntlProvider>
);

export default App;

view raw JSON →