Dev Helpers
← Back

Localized relative day labels

Place this file at app/regions/[code]/page.tsx in an App Router project.

Open this example in the interactive library

Source code

// app/regions/[code]/page.tsx

import { notFound } from "next/navigation";

export default async function LocalizedRelativeDayLabels({
  params,
}: {
  params: Promise<{ code: string }>;
}) {
  const places = [
    {
      code: "ca",
      lang: "en-CA",
      currency: "CAD",
      zones: ["America/Toronto", "America/Vancouver"],
    },
    { code: "jp", lang: "ja-JP", currency: "JPY", zones: ["Asia/Tokyo"] },
  ];
  const { code } = await params;
  const place = places.find((p) => p.code === code);
  if (!place) notFound();
  return (
    <ul>
      {[-1, 0, 1].map((n) => (
        <li key={n}>
          {new Intl.RelativeTimeFormat(place.lang, { numeric: "auto" }).format(
            n,
            "day",
          )}
        </li>
      ))}
    </ul>
  );
}