Supporting multiple languages in Next.js can become complicated when you manage routing and translations yourself. next-intl simplifies internationalization (i18n) by providing translation APIs, locale-aware navigation, and routing support.
npm install next-intl
Create one JSON file for each language:
messages/├── en.json└── vi.json
messages/en.json
{"Home": {"title": "Welcome to Green Garden"}}
messages/vi.json
{"Home": {"title": "Chào mừng đến Green Garden"}}
This defines the supported languages and sets Vietnamese as the default locale. Create src/i18n/routing.ts:
import { defineRouting } from "next-intl/routing";export const routing = defineRouting({locales: ["en", "vi"],defaultLocale: "vi",});
This loads the correct translation file based on the requested locale and falls back to Vietnamese if the locale is unsupported. Create src/i18n/request.ts:
import { getRequestConfig } from "next-intl/server";import { hasLocale } from "next-intl";import { routing } from "./routing";export default getRequestConfig(async ({ requestLocale }) => {const requestedLocale = await requestLocale;const locale = hasLocale(routing.locales,requestedLocale)? requestedLocale: routing.defaultLocale;return {locale,messages: (await import(`../../messages/${locale}.json`)).default,};});
Update next.config.ts:
import createNextIntlPlugin from "next-intl/plugin";import type { NextConfig } from "next";const nextConfig: NextConfig = {};export default createNextIntlPlugin()(nextConfig);
Create src/proxy.ts for Next.js 16:
import createMiddleware from "next-intl/middleware";import { routing } from "./i18n/routing";export default createMiddleware(routing);export const config = {matcher: "/((?!api|trpc|_next|_vercel|.*\\..*).*)",};
Organize your pages under the [locale] segment:
src/app/└── [locale]/├── layout.tsx├── page.tsx└── plants/└── page.tsx
Create src/app/[locale]/layout.tsx:
import { hasLocale } from "next-intl";import { getMessages, setRequestLocale } from "next-intl/server";import { NextIntlClientProvider } from "next-intl";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,}: {children: React.ReactNode;params: Promise<{ locale: string }>;}) {const { locale } = await params;if (!hasLocale(routing.locales, locale)) {notFound();}setRequestLocale(locale);const messages = await getMessages();return (<html lang={locale}><body><NextIntlClientProvider messages={messages}>{children}</NextIntlClientProvider></body></html>);}
In a Server Component, use getTranslations:
import { getTranslations } from "next-intl/server";export default async function HomePage() {const t = await getTranslations("Home");return <h1>{t("title")}</h1>;}
The output depends on the current locale:
/vi → Chào mừng đến Green Garden/en → Welcome to Green GardenFor Client Components, use useTranslations with "use client".