All posts

Making a Next.js 16 site bilingual: English at /, Arabic at /ar

This portfolio started English-only. I now work in a region where Arabic matters as much as English, so I made the whole site bilingual. I had three goals:

  • Keep every English URL exactly where it was, so existing links and search rankings survive.
  • Serve Arabic under /ar, right-to-left, with its own URLs that search engines can index.
  • Add no i18n library: the site is small, and Next.js 16 has the primitives.

Here is how it works, and the four things that bit me.

One English address, Arabic under /ar

All routes moved into app/[lang]/, and the root layout moved with them, so <html lang dir> can change per language. A proxy (Next 16's new name for middleware) maps the public URLs onto that segment:

export function proxy(request: NextRequest) {
  const url = request.nextUrl.clone()
  const { pathname } = url

  if (pathname === "/en" || pathname.startsWith("/en/")) {
    url.pathname = pathname.slice(3) || "/"
    return NextResponse.redirect(url, 308)
  }
  if (pathname === "/ar" || pathname.startsWith("/ar/")) return

  url.pathname = `/en${pathname === "/" ? "" : pathname}`
  return NextResponse.rewrite(url)
}

/about is rewritten to /en/about internally, /ar/about passes through, and /en/about redirects to /about so each English page has a single address.

Server components read the language with next/root-params, with no prop drilling:

import { lang as rootLang } from "next/root-params"

export const getLang = async () => {
  const value = await rootLang()
  const lang = value && isLang(value) ? value : "en"
  return { lang, t: dictionaries[lang] }
}

The dictionaries are plain TypeScript objects. The Arabic one is typed as typeof en, so a missing translation fails the type check instead of shipping a blank.

Gotcha 1: usePathname returns the rewritten path

Client components get the language from the URL. During server rendering, though, usePathname() returns the rewritten path: /en/skills, not /skills. My language toggle rendered a link to /ar/en/skills, and the header path would have caused a hydration mismatch. The fix is one regex that strips either prefix, used everywhere through a small hook:

export const stripLang = (pathname: string) =>
  /^\/(en|ar)(\/|$)/.test(pathname) ? pathname.slice(3) || "/" : pathname

Gotcha 2: unknown URLs answered 200

A catch-all [lang]/[...rest] route calls notFound(), so unknown URLs get the localized 404 page. But curl showed status 200. The cause: [lang]/loading.tsx wrapped every route in a Suspense boundary, so the page shell started streaming with a 200 before notFound() ran. Moving the home page and its loading state into a (home) route group took the catch-all out of that boundary, and real 404s came back.

Gotcha 3: the Open Graph image disappeared

app/opengraph-image.tsx stopped applying once the root layout moved under [lang]. Moving the image into [lang] would put it behind the proxy's /en redirect, so I kept it at the root and linked it explicitly in the shared metadata helper.

Gotcha 4: Arabic in a monospace font

The site's body font is Geist Mono, which has no Arabic glyphs, so Arabic fell back to IBM Plex Sans Arabic. But the space character still came from the mono font, and its wide spaces made a two-word menu item like "السيرة الذاتية" read as two items. On Arabic pages the Arabic font now comes first. Letter-spacing also breaks Arabic letter joining, so tracking-* classes are reset under dir="rtl".

Right-to-left without a rewrite

Most of the RTL work was mechanical: swap physical Tailwind classes for logical ones (ms-, me-, ps-, start-, end-), and flip directional arrows with rtl:-scale-x-100. Flexbox and grid mirror on their own once dir="rtl" is set.

SEO

Every page emits a canonical URL plus hreflang alternates for en, ar and x-default from a single helper. The sitemap lists both languages with the same alternates. The JSON-LD Person includes my Arabic name as alternateName.

The result: two fully static languages, one proxy file, no i18n dependency, and an Arabic site that reads like it was designed that way, not translated after the fact.