Set up in 4 steps
- 01
Install
Add @better-i18n/next and next-intl to your project.
terminalbashnpm install lefine - 02
Add middleware (proxy.ts in Next.js 16) for locale detection
The middleware reads the URL prefix, the locale cookie and the Accept-Language header to detect the user's locale and redirect accordingly.
middleware.ts// lefine.tsimport { createLefine } from 'lefine/next';export const lefine = createLefine({ project: 'app_3f9c0e4b2a7d4e1f9b6c5a8d7e2f1a0b', locales: ['en', 'de', 'fr', 'ja'], defaultLocale: 'en',});// proxy.ts (middleware.ts before Next 16)import { lefine } from './lefine';export default lefine.proxy;export const config = { matcher: ['/((?!api|_next|_vercel|.*\\..*).*)'] }; - 03
Load messages in a Server Component
Use getMessages() in your root layout to fetch translations server-side and pass them to BetterI18nProvider.
app/[locale]/layout.tsx// app/[locale]/layout.tsximport { lefine } from '@/lefine';export default async function RootLayout({ children, params }) { const { locale } = await params; return ( <html lang={locale}> <body> <lefine.Provider locale={locale} namespaces={['home']}> {children} </lefine.Provider> </body> </html> );} - 04
Use translations in Client Components
Call useTranslations() in any Client Component. Messages are already hydrated from the server — no extra fetch.
components/HeroSection.tsx'use client';import { useStrings } from 'lefine/react';export function HeroSection() { const t = useStrings('home'); return <h1>{t('title')}</h1>;}