Introduction
i18n helps your app speak many languages so more people can use it easily.
i18n helps your app speak many languages so more people can use it easily.
import { useRouter } from 'next/router'; import { useTranslations } from 'next-intl'; export default function Page() { const t = useTranslations('Home'); return <h1>{t('welcome')}</h1>; }
Use useTranslations hook to get translated text by keys.
Translation keys are organized by namespaces like 'Home' for clarity.
const t = useTranslations('Header'); return <p>{t('greeting')}</p>;
const { locale } = useRouter(); console.log(locale);
This component shows a greeting and introduction text using i18n keys.
import { useTranslations } from 'next-intl'; export default function Welcome() { const t = useTranslations('Welcome'); return <main> <h1>{t('hello')}</h1> <p>{t('intro')}</p> </main>; } // Example translation JSON for English (en): // { // "hello": "Hello!", // "intro": "Welcome to our app." // }
Always keep your translation keys clear and consistent.
Test your app in different languages to catch layout or text issues.
Use fallback languages to avoid missing text errors.
i18n makes your app usable by people worldwide.
It handles language, date, number, and cultural differences.
Next.js provides easy tools like useTranslations to add i18n.
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