@k8ordo/i18n

Formatting

Plurals, dates and numbers are formatted with Intl itself. The locale set returns the Intl object for the current locale.

On this page

Intl members

src/components/price.tsx
import { locales } from '../i18n';

export function Price({ amount }: { amount: number }) {
  const yen = locales.numberFormat({
`Intl.NumberFormat` for the current locale
    style: 'currency',
    currency: 'JPY',
  });
  return <span>{yen.format(amount)}</span>;
}

There are five such members. None is a hook, so they work in both Server Components and Client Components.

  • dateTimeFormat(options?): Intl.DateTimeFormat
  • numberFormat(options?): Intl.NumberFormat
  • relativeTimeFormat(options?): Intl.RelativeTimeFormat
  • pluralRules(options?): Intl.PluralRules
  • listFormat(options?): Intl.ListFormat

Each returns the Intl object itself, so format, formatToParts and formatRange are all available. The options are Intl’s own, too.

For an Intl API not listed here, such as Intl.Collator or Intl.DisplayNames, pass the tag from locales.getLocale() and make it yourself.

Dates

src/components/published-at.tsx
import { locales } from '../i18n';

export function PublishedAt({ date }: { date: Date }) {
  return (
    <time dateTime={date.toISOString()}>
      {locales.dateTimeFormat({ dateStyle: 'medium' }).format(date)}
Shown in the locale’s `timeZone`
    </time>
  );
}

dateTimeFormat shows a date in the timeZone the locale defines, never in the runtime’s own zone. That keeps the date the same in the server’s HTML and in the browser’s render. Choosing each locale’s time zone is covered under “Time zone” on the Locales page.

The options type does not accept timeZone. One forced through with as is overridden by the locale’s timeZone.

Information

Note

A time in the visitor’s own zone, or a time relative to now, can differ between the server and the browser. Use Intl directly for those, in a part that renders only in the browser.

Playground

Time zone demo

One instant, 15:30 UTC on 5 March 2026, shown in the locale’s timeZone and in the browser’s time zone.

locales.dateTimeFormat(…)
March 5, 2026 at 3:30 PMUTC
new Intl.DateTimeFormat(locales.getLocale(), …)
Shown once the browser renders

Try it

  1. The first row is shown in the page locale’s timeZone. This site defines ja as Asia/Tokyo and en as UTC, so the Japanese page shows 6 March and the English page 5 March.
  2. The second row appears once the browser has rendered, in this browser’s time zone. If that differs from the page’s, the time or the date disagrees with the first row.
  3. Pick the other language in the header. The first row’s time zone and date change; the second row’s do not.

Plurals and dates in messages

src/messages/cart.ts
import { message } from '@k8ordo/i18n';

import { locales } from '../i18n';

const long = () => locales.dateTimeFormat({ dateStyle: 'long' });

export const items = message({
  ja: (count: number) => `${String(count)}件`,
  en: (count) =>
    locales.pluralRules().select(count) === 'one'
Picks the English plural with `Intl.PluralRules`
      ? `${String(count)} item`
      : `${String(count)} items`,
});

export const updated = message({
  ja: (date: Date) => `${long().format(date)}に更新`,
  en: (date) => `Updated ${long().format(date)}`,
});

Call these members inside a message function that takes values, and plurals and dates become part of the message. The function runs with its own locale as the current one, so the Intl object is that locale’s too.

In English, pluralRules().select(1) returns one. In Japanese it returns other for every number, so one Japanese form is enough.

Object reuse

One Intl object is made per locale and options, and the same one is returned after that, so calling it on every render is fine.

Options are keyed by their JSON string. The same options in another order make a second object, but never a different result.

k8ordo

React libraries that use Baseline features without holding back.