Skip to content

Next.js i18n and Arabic RTL: [lang] Routes, Dictionaries, Plurals, hreflang and proxy.ts

NeedMyTools Academy

Sign in free to read the full lesson

Sign in or create a free account

Free forever. No card, no paywall.

Most sites in the Gulf need at least English and Arabic, and Arabic is not just a translation: it reads right to left, it has six plural forms, and search engines must know that /ar/shops is the Arabic version of /en/shops. Next.js has no built in translation system in the App Router; instead it gives you the pieces to build one with a few small files. This lesson turns the Souq Guide app into a bilingual site with Next.js 16.4.6, and every result below is what the server really sent, including a plural mistake we made in our first version and how we fixed it.

Quick answer

For an English and Arabic Next.js site, put every route under app/[lang], make app/[lang]/layout.tsx the root layout, set lang={lang} on <html> with dir "rtl" for Arabic and "ltr" otherwise, and list the languages in generateStaticParams. Keep text in one JSON dictionary per language loaded on the server, format numbers and plurals with Intl, add alternates.languages for hreflang tags, and use proxy.ts to redirect / to the language in the Accept-Language header.

45 min read and practice ยท Content checked on 26 November 2026

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.

Example: A root layout inside app/[lang]
app/[lang]/dictionaries.ts
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]();
app/[lang]/layout.tsx
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>
  );
}
Real output: terminal, npm run build (excerpt)
Route (app)
โ”Œ โ—‹ /_not-found
โ”œ   /[lang]
โ”‚ โ”œ โ— /[lang]
โ”‚ โ”œ โ—‹ /en
โ”‚ โ”” โ—‹ /ar
โ””   /[lang]/shops
  โ”œ โ— /[lang]/shops
  โ”œ โ—‹ /en/shops
  โ”” โ—‹ /ar/shops


ฦ’ Proxy (Middleware)
Real output: tags served for /ar (excerpt)
<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
Sign in or create a free account

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.

Code examples stay in English and run in a sandbox on your device. If you spot a mistake, tell us through the contact page.