Next.jsMedium

How do you configure internationalization (i18n) in Next.js?

The App Router has no built-in i18n config. You add a [lang] (or [locale]) dynamic segment at the root, detect the preferred locale in proxy.ts and redirect or rewrite to it, load translation dictionaries in Server Components, and prerender each locale with generateStaticParams. The Pages Router has a built-in i18n option in next.config.js.

App Router steps

  1. Routing: move routes under app/[lang]/ so URLs look like /en/about and /fr/about.
  2. Detection: in proxy.ts, if the path has no locale, pick one from a cookie or Accept-Language (using @formatjs/intl-localematcher and negotiator) and redirect.
  3. Dictionaries: getDictionary(lang) dynamically imports dictionaries/fr.json on the server; translations never ship to the client unless you pass them down.
  4. Static generation: generateStaticParams returns [{ lang: "en" }, { lang: "fr" }].
  5. <html lang> set in app/[lang]/layout.tsx.
  6. Metadata: localized titles and alternates.languages for hreflang tags.

Reading the locale anywhere

next/root-params (16.3) exports a getter per root segment: import { lang } from "next/root-params"; const l = await lang();, usable in Server Components and inside "use cache", avoiding prop drilling.

Libraries

next-intl and next-intlayer (both listed in the Next.js docs) add message formatting, plurals, typed keys, client hooks and Proxy helpers.

Pages Router

i18n: { locales: ["en", "fr"], defaultLocale: "en", localeDetection: true }, with useRouter().locale and getStaticProps({ locale }).

Formatting

Use Intl.DateTimeFormat, Intl.NumberFormat and Intl.PluralRules with the active locale.

Code example

tsx
1// proxy.ts
2import { NextResponse, type NextRequest } from "next/server";
3import { match } from "@formatjs/intl-localematcher";
4import Negotiator from "negotiator";
5
6const locales = ["en", "fr", "hi"];
7
8export function proxy(request: NextRequest) {
9 const { pathname } = request.nextUrl;
10 if (locales.some((l) => pathname === `/${l}` || pathname.startsWith(`/${l}/`))) return;
11
12 const languages = new Negotiator({ headers: { "accept-language": request.headers.get("accept-language") ?? "" } }).languages();
13 const locale = request.cookies.get("NEXT_LOCALE")?.value ?? match(languages, locales, "en");
14 return NextResponse.redirect(new URL(`/${locale}${pathname}`, request.url));
15}
16export const config = { matcher: ["/((?!_next|api|.*\\..*).*)"] };
17
18// app/[lang]/layout.tsx
19// export async function generateStaticParams() { return locales.map((lang) => ({ lang })); }
20// export default async function Layout({ children, params }: LayoutProps<"/[lang]">) {
21// const { lang } = await params;
22// return <html lang={lang}><body>{children}</body></html>;
23// }
24
25// app/[lang]/dictionaries.ts
26// const dictionaries = { en: () => import("./dictionaries/en.json").then((m) => m.default), fr: ... };
27// export const getDictionary = (lang: "en" | "fr") => dictionaries[lang]();

Practice this question

Open it in the study app to track progress, take notes, listen to the answer and work through the rest of the Next.js set.

Open in study app