Performance: Why i18n matters
Internationalization (i18n) affects page load speed and user interaction by managing language resources efficiently and avoiding unnecessary reloads or large bundles.
Use dynamic imports or Next.js middleware to load only the needed language file on demand
import all language JSON files upfront in _app.js and bundle them together
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Load all languages upfront | Low | Multiple on initial load | High due to large bundle | [X] Bad |
| Dynamic language loading | Low | Single on load | Low | [OK] Good |
| Full page reload on language switch | High | Multiple | High | [X] Bad |
| Client-side language switch | Low | Single | Low | [OK] Good |
useTranslations to easily get translated strings in components.useRouter manages routing, useState manages state, and useEffect handles side effects, none are for translations.import { useTranslations } from 'next-intl';
export default function Greeting() {
const t = useTranslations('Home');
return <h1>{t('welcomeMessage')}</h1>;
}{ "welcomeMessage": "Hello, world!" }, what will be rendered?useTranslations('Home') returns a function t that looks up keys in the 'Home' translation file.t('welcomeMessage') returns "Hello, world!".import { useTranslations } from 'next-intl';
export default function Header() {
const t = useTranslations('Header');
return <h2>t('title')</h2>;
}t('title') is written as plain text, so it won't execute.t('title') in curly braces like {t('title')} correctly calls the function and renders the translation.Intl.DateTimeFormat formats dates according to locale, which can be obtained from Next.js router or i18n context.Pass the test and this pattern moves unknown