Performance: Next-intl library integration
This affects page load speed and interaction responsiveness by how translations are loaded and rendered in Next.js apps.
import {getTranslations} from 'next-intl/server'; export async function generateStaticParams() { return [{locale: 'en'}, {locale: 'fr'}]; } export default async function Page({params}) { const t = await getTranslations(params.locale); return <p>{t('welcome')}</p>; } // Translations loaded server-side or statically
'use client'; import {useTranslations} from 'next-intl'; export default function Page() { const t = useTranslations(); return <p>{t('welcome')}</p>; } // Translations fetched client-side after page load
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Client-side translation fetching | Minimal DOM nodes added | Triggers 1 reflow after translations load | Delays paint by 200-400ms | [X] Bad |
| Server-side or static translation loading | Minimal DOM nodes added | Single reflow during initial paint | Paints immediately with translations | [OK] Good |
NextIntlProvider component in a Next.js app using next-intl?NextIntlProvider wraps the app and supplies translation messages and locale information to all child components.NextIntlProvider's purpose.useTranslations hook in a Next.js component?useTranslations and imported with curly braces from 'next-intl'.import { useTranslations } from 'next-intl';
export default function Greeting() {
const t = useTranslations('greeting');
return <h1>{t('hello')}</h1>;
}{ greeting: { hello: 'Hello, friend!' } }, what will be rendered?useTranslations('greeting') accesses the 'greeting' namespace in messages.t('hello') returns that string.import { NextIntlProvider } from 'next-intl';
export default function Page({ messages }) {
return (
<NextIntlProvider messages={messages} locale="en">
<h1>Welcome</h1>
</NextIntlProvider>
);
}NextIntlProvider requires a valid messages object passed as a prop.messages is undefined, it means the parent or data loader did not supply it properly.locales/en.json locales/es.json
app/layout.tsx to load messages dynamically and provide translations to all pages?NextIntlProvider wrapping all children so translations are available app-wide.NextIntlProvider passing loaded messages and locale [OK]Pass the test and this pattern moves unknown