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}!}}"
}
}
{name}is a plain slot: its value is written as it is.{n, plural, ...}picks a branch by the plural category fromIntl.PluralRules. The#inside it is the numbern, formatted for the locale.{gender, select, ...}picks a branch by a text value. Indonesian needs the form of address Pak (Mr.), Bu (Mrs.) or Kak (for anyone); Arabic needs a male (لكَ) or female (لكِ) "you"; English and Japanese need neither, so their message has noselect.- Every
pluralandselectmust have anotherbranch. When the branch for a category is missing (sayzeroinar-EG),otheris used.
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:
#is formatted for the locale,{n}isn't.{n} nightswithn = 1000gives "1000 nights", while#gives "1,000 nights". Inar-EG,{n}gives a plain 3,#gives ٣.- Numbers and dates can be formatted inside a message too, with a skeleton:
{price, number, ::currency/USD precision-integer}gives "$70" inen-US, and{d, date, ::EEEEdMMMM}gives "Saturday, December 12".precision-integeris the same asmaximumFractionDigits: 0from the Intl API lesson.
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
IntlProvidergoes in once, at the top: thelocale, that locale's dictionary, and thetimeZonefor dates inside messages.<FormattedMessage id="total" values={...} />is the component that shows a translated message. It can take adefaultMessageas a fallback.useIntl()givesintl.formatMessage,intl.formatNumberandintl.formatDatefor places a component can't go:aria-label,placeholder,title.- When the key is missing from the dictionary, react-intl uses
defaultMessageand reports throughonErrorwith the codeMISSING_TRANSLATION: "Missing message: "total" for locale "ja-JP", using default message (...) as fallback." - It fits React apps without Next.js: a Vite SPA, React Router, or React Native.
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>;
}
next.config.tswraps the config withcreateNextIntlPlugin(), andapp/layout.tsxwrapschildrenin<NextIntlClientProvider>so Client Components get the dictionary too.useTranslations('Booking')is used in regular components (Server or Client);getTranslationsinasyncServer Components.- A locale in the URL (
/ja-JP/terrace-room) is set up withdefineRoutingplus middleware. In Next.js 16 that middleware file is calledproxy.ts; earlier versions call itmiddleware.ts. - So next-intl's main advantage over plain react-intl in Next.js: it integrates with Next.js routing and supports Server Components, so text in a Server Component is already HTML when it reaches the browser.
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
- Gluing sentence pieces together.
'Total for ' + n + ' nights'can't become Arabic or Japanese. Keep whole sentences with slots. - Forgetting the
otherbranch.{n, plural, one {# night}}throwsSyntaxError: MISSING_OTHER_CLAUSE, and so does aselectwith noother. - Using
{n}insideplural. The number isn't formatted for the locale: "1000 nights", not "1,000 nights". Use#. - Copying the English rules to every language.
oneandotherare enough for English, butar-EGalso needstwo,fewandmany. - A slot with no value.
{name}with nonameinvaluesthrows: "The intl string context variable "name" was not provided to the string "{name}"".
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.