In this lesson you will
- Give each language its own URLs with an app/[lang] segment
- Set lang and dir on <html> so Arabic pages render right to left
- Load text from server dictionaries and write correct Arabic plurals
- Add hreflang alternates and redirect visitors with proxy.ts
One URL per language with app/[lang]
Search engines index URLs, not cookies, so each language needs its own address: /en/shops and /ar/shops. Move every route into a dynamic segment app/[lang]. That folder can hold the root layout itself, app/[lang]/layout.tsx, which is what makes per language <html> tags possible, because the root layout is the only place that renders <html>. With generateStaticParams returning both languages, the build prerendered /en and /ar as ordinary static pages; the โ line above them is the pattern for any other value.
Two attributes do the heavy lifting. lang="ar" tells screen readers to use an Arabic voice, lets browsers choose Arabic fonts and hyphenation, and helps search engines. dir="rtl" flips the whole page: text starts on the right, the first column of a flex row moves to the right and tables read from the right. The hasLocale helper is a type guard: after if (!hasLocale(lang)) notFound(), TypeScript knows lang is "en" or "ar", and a URL such as /fr gets a 404.
The layout also writes the language alternates. alternates.languages turns into <link rel="alternate" hrefLang="..."> tags that tell Google the pages are translations of each other, and x-default names the page for everyone else. The language switch link carries hrefLang too, and its text is written in the language it leads to: "ุงูุนุฑุจูุฉ" on English pages, "English" on Arabic ones.
import "server-only";
const dictionaries = {
en: () => import("./dictionaries/en.json").then((m) => m.default),
ar: () => import("./dictionaries/ar.json").then((m) => m.default),
};
export type Locale = keyof typeof dictionaries;
export const LOCALES = Object.keys(dictionaries) as Locale[];
export const hasLocale = (lang: string): lang is Locale => lang in dictionaries;
export const getDictionary = (lang: Locale) => dictionaries[lang]();import type { Metadata } from "next";
import Link from "next/link";
import { notFound } from "next/navigation";
import { LOCALES, getDictionary, hasLocale } from "./dictionaries";
export function generateStaticParams() {
return LOCALES.map((lang) => ({ lang }));
}
export async function generateMetadata({ params }: LayoutProps<"/[lang]">): Promise<Metadata> {
const { lang } = await params;
if (!hasLocale(lang)) return {};
const dict = await getDictionary(lang);
return {
metadataBase: new URL("https://souq-guide.example"),
title: { template: `%s | ${dict.site}`, default: dict.site },
alternates: { languages: { en: "/en", ar: "/ar", "x-default": "/en" } },
};
}
export default async function RootLayout({ children, params }: LayoutProps<"/[lang]">) {
const { lang } = await params;
if (!hasLocale(lang)) notFound();
const dict = await getDictionary(lang);
const other = lang === "en" ? "ar" : "en";
return (
<html lang={lang} dir={lang === "ar" ? "rtl" : "ltr"}>
<body>
<nav>
<Link href={`/${lang}`}>{dict.nav.home}</Link> | <Link href={`/${lang}/shops`}>{dict.nav.shops}</Link> |{" "}
<Link href={`/${other}`} hrefLang={other}>{dict.nav.switch}</Link>
</nav>
<main>{children}</main>
</body>
</html>
);
}Route (app)
โ โ /_not-found
โ /[lang]
โ โ โ /[lang]
โ โ โ /en
โ โ โ /ar
โ /[lang]/shops
โ โ /[lang]/shops
โ โ /en/shops
โ โ /ar/shops
ฦ Proxy (Middleware)<html lang="ar" dir="rtl">
<title>ุฏููู ุงูุฃุณูุงู</title>
<link rel="alternate" hrefLang="en" href="https://souq-guide.example/en"/>
<link rel="alternate" hrefLang="ar" href="https://souq-guide.example/ar"/>
<link rel="alternate" hrefLang="x-default" href="https://souq-guide.example/en"/>LayoutProps and PageProps are global types that Next.js generates from your folders, so the params are typed without writing them by hand. The import "server-only" line makes the build fail if a client component imports the dictionaries.
Sign in free to read the full lesson
You are reading the free preview. Create a free account (it takes one minute, no password) to unlock the rest of this lesson and track your progress.
Unlocks after sign-in:
- 3 more sections with live code examples
- Common mistakes to avoid
- Hands-on coding exercise with automatic checks
- Practice quiz and the graded lesson test
Free forever. No card, no paywall.
Questions people ask
Does the Next.js App Router have built in i18n routing?
No. The i18n option in next.config belongs to the Pages Router. In the App Router you build it with an app/[lang] segment, dictionaries and proxy.ts, as in this lesson, or use a library such as next-intl that packages the same approach.
Should the Arabic version use /ar or an ar subdomain?
Both work for Google as long as each version has its own URL and hreflang tags. A folder such as /ar is simpler: one domain, one certificate, one deployment, and the code in this lesson works without changes.
Should I use Arabic Indic digits on an Arabic website?
It is a style choice. Many UAE and Saudi sites, banks and government portals use Latin digits in Arabic text, while others use Arabic Indic digits. Pick one, format all numbers through Intl.NumberFormat with that choice, and never mix them on a page.
What is x-default in hreflang?
It names the page to show people whose language matches none of your versions, often the English page or a language chooser. Add it next to the en and ar alternates.
Can I translate pages automatically with machine translation?
You can start from a machine draft, but have a fluent Arabic speaker review it before it goes live. Machine output often gets plurals, gender agreement and tone wrong, and thin or awkward Arabic pages hurt both readers and rankings.
Sources
Facts in this lesson were checked against these official pages on 26 November 2026.
- Next.js docs: Internationalization
- Next.js docs: proxy.js
- MDN: Intl.PluralRules
- Google Search Central: Localized versions of your pages
Code examples stay in English and run in a sandbox on your device. If you spot a mistake, tell us through the contact page.