Menu

Internationalisation and Root Params

Lesson 4: Internationalisation and Root Params

How do you serve several languages?

The common pattern is a language segment at the top of every URL: /en/courses, /hi/courses, /ta/courses.

Piece Role
app/[lang]/ folder Every route lives under the language segment
proxy.ts Redirects /courses to the user's preferred language
Dictionaries JSON files with translated strings per language
generateStaticParams Prerenders each supported language
Root params (Next.js 16.3+) Read lang from any Server Component without passing props

Example

app/
└─ [lang]/
   ├─ layout.tsx       # Root layout: <html lang={lang}>
   ├─ page.tsx
   └─ courses/page.tsx
dictionaries/
├─ en.json
├─ hi.json
└─ ta.json
// lib/dictionaries.ts
import 'server-only';

const dictionaries = {
  en: () => import('@/dictionaries/en.json').then((m) => m.default),
  hi: () => import('@/dictionaries/hi.json').then((m) => m.default),
  ta: () => import('@/dictionaries/ta.json').then((m) => m.default),
};

export type Locale = keyof typeof dictionaries;
export const locales = Object.keys(dictionaries) as Locale[];
export const getDictionary = (locale: Locale) => dictionaries[locale]();
// app/[lang]/layout.tsx
export async function generateStaticParams() {
  return locales.map((lang) => ({ lang }));
}

export default async function RootLayout({ children, params }: LayoutProps<'/[lang]'>) {
  const { lang } = await params;
  return (
    <html lang={lang}>
      <body>{children}</body>
    </html>
  );
}
// app/[lang]/courses/CourseHeading.tsx – deep in the tree, no props needed
import { lang } from 'next/root-params';
import { getDictionary, type Locale } from '@/lib/dictionaries';

export async function CourseHeading() {
  const locale = (await lang()) as Locale;
  const t = await getDictionary(locale);
  return <h1>{t.courses.title}</h1>;
}

How it works

  1. A visitor requests /courses. proxy.ts reads the Accept-Language header (or a saved cookie) and redirects to /hi/courses.
  2. [lang] holds the language; generateStaticParams prerenders each language.
  3. Root params, added in Next.js 16.3, let any Server Component read lang with await lang() instead of passing it down through every component.
  4. Dictionaries load only on the server, so translations do not increase the browser bundle.

Common mistakes

Mistake Effect Fix
Not validating lang /xx/courses crashes Call notFound() if the locale is unsupported
Hard-coded text in components Missed translations Read every UI string from the dictionary
Forgetting <html lang> Screen readers and search engines guess wrongly Set it from the lang param
No hreflang alternates Search engines show the wrong language Add alternates.languages in metadata

Key takeaways

  • Put the language in the URL with a [lang] segment and redirect with proxy.
  • Load dictionaries on the server and read the locale with root params.

Try it: Add English and Hindi versions of the LearnHub home page with a language switcher that keeps the current path.


Nextjs Tutorial

J

Jebasta





Get in Touch For Details