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
- Routing: move routes under
app/[lang]/so URLs look like/en/aboutand/fr/about. - Detection: in
proxy.ts, if the path has no locale, pick one from a cookie orAccept-Language(using@formatjs/intl-localematcherandnegotiator) and redirect. - Dictionaries:
getDictionary(lang)dynamically importsdictionaries/fr.jsonon the server; translations never ship to the client unless you pass them down. - Static generation:
generateStaticParamsreturns[{ lang: "en" }, { lang: "fr" }]. <html lang>set inapp/[lang]/layout.tsx.- Metadata: localized titles and
alternates.languagesfor 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
1// proxy.ts2import { NextResponse, type NextRequest } from "next/server";3import { match } from "@formatjs/intl-localematcher";4import Negotiator from "negotiator";56const locales = ["en", "fr", "hi"];78export function proxy(request: NextRequest) {9 const { pathname } = request.nextUrl;10 if (locales.some((l) => pathname === `/${l}` || pathname.startsWith(`/${l}/`))) return;1112 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|.*\\..*).*)"] };1718// app/[lang]/layout.tsx19// 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// }2425// app/[lang]/dictionaries.ts26// 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.