import { defineRouting } from 'next-intl/routing';export const routing = defineRouting({ // A list of all locales that are supported locales: ['en', 'es', 'fr', 'de'], // Used when no locale matches defaultLocale: 'en',});
Update your next.config.js to include the internationalization plugin:
Copy
const withNextIntl = require('next-intl/plugin')( // This is the default (also the `src` folder is supported out of the box) './src/i18n/request.ts');module.exports = withNextIntl({ // Other Next.js configuration});
import { ReactNode } from 'react';type Props = { children: ReactNode;};// Since we have a `not-found.tsx` page on the root, a layout file// is required, even if it's just passing children through.export default function RootLayout({ children }: Props) { return children;}
import { NextIntlClientProvider } from 'next-intl';import { getMessages } from 'next-intl/server';import { notFound } from 'next/navigation';import { routing } from '@/i18n/routing';export default async function LocaleLayout({ children, params: { locale }}: { children: React.ReactNode; params: { locale: string };}) { // Ensure that the incoming `locale` is valid if (!routing.locales.includes(locale as any)) { notFound(); } // Providing all messages to the client // side is the easiest way to get started const messages = await getMessages(); return ( <html lang={locale}> <body> <NextIntlClientProvider messages={messages}> {children} </NextIntlClientProvider> </body> </html> );}