react-intl / next-intl — i18n

ICU MessageFormat writes whole sentences with slots: {name}, {n, plural, one {# night} other {# nights}} and {gender, select, male {Pak} female {Bu} other…

Mrs. Sri's old code built the total sentence by gluing pieces together: 'Total for ' + n + ' nights: $'. In Indonesian the sentence has to become "Total 2 malam". In Arabic, two nights even has its own word. Pieces like that can't be translated. Intl formats one value, while a message format handles a whole sentence: text with slots, plus a choice of forms by count or by kind. It's like a letter form at the town hall: "Dear Mr/Mrs*) ......, booking for ...... nights", with the note "*) cross out whichever doesn't apply". The clerk fills in the dots and crosses out the choices that don't fit. ICU MessageFormat is that form written as text: {name} is the dots, select and plural are the choices that get crossed out for you. react-intl and next-intl are the clerks who fill it in inside React.

ICU MessageFormat: sentences with slots

The Mist Homestay dictionary, one file per locale. These are three messages from Mrs. Sri's card:

{
  "en-US": {
    "nights": "{n, plural, one {# night} other {# nights}}",
    "total": "Total for {n, plural, one {# night} other {# nights}}: {price}",
    "thanks": "Thank you, {name}!"
  },
  "id-ID": {
    "nights": "{n, plural, other {# malam}}",
    "total": "Total {n, plural, other {# malam}}: {price}",
    "thanks": "Terima kasih, {gender, select, male {Pak} female {Bu} other {Kak}} {name}!"
  },
  "ja-JP": {
    "nights": "{n, plural, other {#泊}}",
    "total": "{n, plural, other {#泊}}の合計:{price}",
    "thanks": "{name}様、ありがとうございます。"
  },
  "ar-EG": {
    "nights": "{n, plural, one {ليلة واحدة} two {ليلتان} few {# ليالٍ} many {# ليلة} other {# ليلة}}",
    "total": "الإجمالي ({n, plural, one {ليلة واحدة} two {ليلتان} few {# ليالٍ} many {# ليلة} other {# ليلة}}): {price}",
    "thanks": "{gender, select, female {شكرًا لكِ يا {name}!} other {شكرًا لكَ يا {name}!}}"
  }
}

plural and select

We ran the nights message above through intl-messageformat 12.1.3, the engine react-intl and next-intl use. Before you look at the table, guess the second row: en-US with n = 0 gives "0 night", "0 nights" or an error?

Locale n PluralRules gives Branch used Result
en-US 1 one one {# night} 1 night
en-US 0 other other {# nights} 0 nights
id-ID 2 other other {# malam} 2 malam
ja-JP 2 other other {#泊} 2泊
ar-EG 2 two two {ليلتان} ليلتان
ar-EG 3 few few {# ليالٍ} ٣ ليالٍ
ar-EG 11 many many {# ليلة} ١١ ليلة

English has no zero category, so 0 goes to other: the result is "0 nights". If you want a special sentence for zero, use an exact number match =0: {n, plural, =0 {Pick a length of stay} one {# night} other {# nights}}. An =N branch is checked before the categories.

The thanks message with select: id-ID gives "Terima kasih, Pak Hadi!" for male, "Terima kasih, Bu Rina!" for female, and "Terima kasih, Kak Alex!" for any other value.

Two small things that often make a difference:

react-intl: IntlProvider, FormattedMessage, useIntl

react-intl (part of FormatJS) is used here at version 12.1.4, released in October 2026 (the latest as of 15 November 2026 is 12.1.7); it needs React 18 or newer and ships only as an ES module.

import { IntlProvider, FormattedMessage, useIntl } from 'react-intl';
import enUS from './messages/en-US.json';
// en-US.json: { "total": "Total for {n, plural, one {# night} other {# nights}}: {price, number, ::currency/USD precision-integer}",
//               "group": "Length of stay" }

function Total({ nights }) {
  const intl = useIntl();
  return (
    <div role="group" aria-label={intl.formatMessage({ id: 'group' })}>
      <FormattedMessage id="total" values={{ n: nights, price: 35 * nights }} />
    </div>
  );
}

export default function App() {
  return (
    <IntlProvider locale="en-US" messages={enUS} timeZone="Asia/Singapore">
      <Total nights={2} />
    </IntlProvider>
  );
}
// Rendered: Total for 2 nights: $70

next-intl: useTranslations in Next.js

next-intl is at version 4.14.9 and supports Next.js 12 to 16, with the same ICU syntax (inside, it also uses intl-messageformat). Its dictionaries are per locale, and nested keys become namespaces:

// src/i18n/request.ts: the per-request configuration
import { getRequestConfig } from 'next-intl/server';

export default getRequestConfig(async () => {
  const locale = 'en-US'; // later from the URL or a cookie
  return {
    locale,
    timeZone: 'Asia/Singapore',
    messages: (await import(`../../messages/${locale}.json`)).default,
  };
});
// app/room/page.tsx: a Server Component
import { getTranslations } from 'next-intl/server';

export default async function RoomPage() {
  const t = await getTranslations('Booking');
  return <p>{t('total', { n: 2, price: 70 })}</p>;
}

This Lab: a small, honest ICU subset

react-intl and next-intl aren't installed in BelajarWeb.dev, and the Playground can't install npm packages. So the Lab's Edit in Playground code holds a small home-made formatter (about 60 lines) that understands {name}, plural (with =N, categories and #) and select, nested ones included. What it doesn't support: number and date skeletons, selectordinal, offset, quotes for writing a literal {, and rich text tags. Prices in the Lab are passed as text already formatted by Intl.NumberFormat. We compared the subset with intl-messageformat 12.1.3 on the dictionary above, 22 values of n and four values of gender, plus the error cases: 1,063 of 1,063 results were identical, error messages included.

⚠️ The quickest ways to break an ICU message

TL;DR: ICU MessageFormat writes whole sentences with slots: {name}, {n, plural, one {# night} other {# nights}} and {gender, select, male {Pak} female {Bu} other {Kak}}. Every plural and select must have other; =0 matches an exact number; # is the number formatted for the locale. English has no zero, so n = 0 gives "0 nights"; ar-EG gives ليلتان for 2, ٣ ليالٍ for 3, ١١ ليلة for 11. react-intl 12 (for React without Next.js) uses IntlProvider, the FormattedMessage component to show a message, and useIntl().formatMessage for attributes. next-intl 4 (for Next.js) uses getRequestConfig, useTranslations and getTranslations, integrated with Next.js routing and Server Components. The Lab's ICU subset gives the same results as intl-messageformat 12.1.3 in every case we tested.

⚡ Whole sentences with slots

Write:

"nights": "{n, plural, one {# night} other {# nights}}"
Guess first: en-US with n = 0 gives "0 night", "0 nights" or an error?

"0 nights". English has no zero category, so 0 goes to other.

Meaning: plural picks a branch by the plural category, and # is the number, already formatted for the locale. Every plural and select must have an other branch.

⚡ react-intl: one provider, then messages

Write:

<FormattedMessage id="total" values={{ n: nights, price: 35 * nights }} />

Meaning: IntlProvider goes in once, at the top, with the locale and its dictionary. FormattedMessage shows a message, and useIntl().formatMessage fills places a component can't go, such as aria-label.

⚡ next-intl for Next.js

Write:

const t = await getTranslations('Booking');
return <p>{t('total', { n: 2, price: 70 })}</p>;

Meaning: next-intl's main advantage over plain react-intl: it integrates with Next.js routing and supports Server Components, so text in a Server Component is already HTML when it reaches the browser.

What you will learn