Next.js localization with next-intl and Localeo
Wire a Next.js App Router project to the JSON files Localeo publishes for each release. You’ll fetch them on the server in next-intl’s request config, turn Localeo’s flat keys into the nested shape next-intl expects, and fall back to the base language for anything not translated yet.
- Framework
- Next.js 16.4 + next-intl 4.16 (also run on 15.5)
- Format
- JSON
- Time
- 15 minutes
Find your bundle URLs
In Localeo, open your project, go to Releases and pick a release. Each language has a download link; there is also a
latestlink that always points at the newest published release. Both have the same shape:- Pinned:
https://cdn.localeo.app/p/<projectSlug>/r/<releaseSlug>/<lang>.json - Latest:
https://cdn.localeo.app/p/<projectSlug>/r/latest/<lang>.json
Each file is a flat JSON object keyed by your Localeo keys, exactly as you typed them:
{"cart.greeting": "Hi {name}, welcome back!", "cart.items": "{count, plural, one {# item} other {# items}}"}. Keys with no translation in a language are left out of that language’s file.Put the pieces in environment variables:
.env.local LOCALEO_CDN_URL=https://cdn.localeo.appLOCALEO_PROJECT=your-project-slugLOCALEO_RELEASE=latest- Pinned:
Set up next-intl routing
If your app already uses next-intl with locale-prefixed routes, skip to the next step. Otherwise install it and add the standard routing setup. The locale codes are the ones your Localeo files use, so
pt-BRkeeps its hyphen.bash npm install next-intlsrc/i18n/routing.ts import { defineRouting } from "next-intl/routing"; export const routing = defineRouting({ locales: ["en", "nl", "pt-BR"], defaultLocale: "en",});src/proxy.ts import createMiddleware from "next-intl/middleware";import { routing } from "./i18n/routing"; export default createMiddleware(routing); export const config = { matcher: "/((?!api|_next|_vercel|.*\\..*).*)",};next.config.ts import type { NextConfig } from "next";import createNextIntlPlugin from "next-intl/plugin"; const withNextIntl = createNextIntlPlugin(); const nextConfig: NextConfig = { // your existing options stay here}; export default withNextIntl(nextConfig);Unflatten Localeo’s keys
You can’t hand the file to next-intl as it is. next-intl treats a dot in a key as nesting, so
t("cart.greeting")looks formessages.cart.greeting, not a key literally namedcart.greeting. Pass the flat object through andnext devlogsINVALID_KEY: Namespace keys cannot contain the character "."followed byMISSING_MESSAGE;next buildskips the key check, logs onlyMISSING_MESSAGEand renders the raw key (cart.greeting) into your pages.So turn the flat keys into nested objects first:
src/i18n/unflatten.ts import type { AbstractIntlMessages } from "next-intl"; // Localeo ships flat keys ("cart.greeting"); next-intl reads "." as nesting.export function unflatten(flat: Record<string, string>): AbstractIntlMessages { const root: AbstractIntlMessages = {}; // Sorted, so "account" is always placed before "account.title". for (const key of Object.keys(flat).sort()) { const parents = key.split("."); const leaf = parents.pop()!; let node: AbstractIntlMessages | string = root; for (const part of parents) { if (typeof node === "string") break; if (!Object.hasOwn(node, part)) node[part] = {}; node = node[part]; } if (typeof node === "string") { console.warn(`[localeo] skipped "${key}": a shorter key is already a message`); continue; } node[leaf] = flat[key]; } return root;}{"cart.greeting": "…", "cart.items": "…"}becomes{"cart": {"greeting": "…", "items": "…"}}, and as a bonus you can scope a component to a namespace withuseTranslations("cart").Load messages in i18n/request.ts
next-intl calls
src/i18n/request.tson the server whenever it renders a page, which makes it the place to fetch. First a small fetch helper:src/i18n/localeo.ts const { LOCALEO_CDN_URL, LOCALEO_PROJECT, LOCALEO_RELEASE } = process.env;const release = `${LOCALEO_CDN_URL}/p/${LOCALEO_PROJECT}/r/${LOCALEO_RELEASE}`; export async function fetchBundle(locale: string): Promise<Record<string, string>> { const res = await fetch( `${release}/${locale}.json`, LOCALEO_RELEASE === "latest" ? { next: { revalidate: 60 } } // the CDN caches "latest" for 60 s too : { cache: "force-cache" }, // a pinned release never changes ); // Only the file name: the URL contains your project slug. if (!res.ok) throw new Error(`Localeo ${locale}.json: HTTP ${res.status}`); return res.json();}Then the request config. It fetches the requested language and your base language together, lays the translation over the base, and only then unflattens, so the merge is a plain object spread:
src/i18n/request.ts import { hasLocale } from "next-intl";import { getRequestConfig } from "next-intl/server";import { fetchBundle } from "./localeo";import { routing } from "./routing";import { unflatten } from "./unflatten"; const BASE_LANGUAGE = "en"; // your Localeo project's base language export default getRequestConfig(async ({ requestLocale }) => { const requested = await requestLocale; const locale = hasLocale(routing.locales, requested) ? requested : routing.defaultLocale; const [base, translated] = await Promise.all([ fetchBundle(BASE_LANGUAGE), locale === BASE_LANGUAGE ? {} : fetchBundle(locale), ]); // Keys missing from a language's file fall back to the base language. return { locale, messages: unflatten({ ...base, ...translated }) };});Use the messages
Plurals and placeholders need no extra setup: next-intl reads ICU syntax, so the
{count, plural, …}and{name}in your Localeo values work as written. Render the layout statically per locale:src/app/[locale]/layout.tsx import { hasLocale, NextIntlClientProvider } from "next-intl";import { setRequestLocale } from "next-intl/server";import { notFound } from "next/navigation";import { routing } from "@/i18n/routing"; export function generateStaticParams() { return routing.locales.map((locale) => ({ locale }));} export default async function LocaleLayout({ children, params,}: LayoutProps<"/[locale]">) { const { locale } = await params; if (!hasLocale(routing.locales, locale)) notFound(); setRequestLocale(locale); return ( <html lang={locale}> <body> <NextIntlClientProvider>{children}</NextIntlClientProvider> </body> </html> );}Server components and client components read the same messages:
src/app/[locale]/page.tsx import { useTranslations } from "next-intl";import { setRequestLocale } from "next-intl/server";import { use } from "react";import { CartCount } from "./cart-count"; export default function Home({ params }: PageProps<"/[locale]">) { const { locale } = use(params); setRequestLocale(locale); const t = useTranslations(); return ( <main> <h1>{t("app.title")}</h1> <p>{t("cart.greeting", { name: "Sam" })}</p> <p>{t("cart.items", { count: 1 })}</p> <CartCount count={5} /> <button type="button"> {t("checkout.pay_button", { amount: "€12.50" })} </button> </main> );}src/app/[locale]/cart-count.tsx "use client"; import { useTranslations } from "next-intl"; export function CartCount({ count }: { count: number }) { const t = useTranslations("cart"); return <p>{t("items", { count })}</p>;}On
/nlthis renders “Hoi Sam, welkom terug!”, “1 artikel” and “5 artikelen”. On/pt-BRthe pay button reads “Pay €12.50 now”, because that key is missing frompt-BR.jsonand falls back to English.Choose a pinned release or latest
LOCALEO_RELEASEdecides when new copy reaches your users:- A pinned release slug gives reproducible builds. The files never change, so
fetchBundlecaches them for good andnext buildprerenders every locale as fully static. Shipping new copy means changing the slug and redeploying, like any other dependency bump. latestlets you update copy without a redeploy. The CDN caches it for 60 seconds and the pages revalidate every 60 seconds, so a publish shows up within about two minutes. Next.js serves the old page to the first visitor after that window while it regenerates in the background.
If you want pinned builds without editing the slug by hand, trigger a redeploy from Localeo’s
release_publishedwebhook.- A pinned release slug gives reproducible builds. The files never change, so
Next steps
- Releases and bundles: how releases are compiled, cached and what each file contains.
- Webhooks: react to
release_publishedand verify the signature.
Ship it with Localeo
Create a project, add your languages and publish your first release. Then point this setup at its links.
Get started