Next-intl helps you show your website in different languages easily. It makes your app friendly for people who speak different languages.
Next-intl library integration in NextJS
import {NextIntlProvider} from 'next-intl'; export default function App({Component, pageProps}) { return ( <NextIntlProvider messages={pageProps.messages}> <Component {...pageProps} /> </NextIntlProvider> ); }
Wrap your app with NextIntlProvider to provide translations.
Pass the messages prop containing translations for the current language.
useTranslations hook to get translated text inside components.import {useTranslations} from 'next-intl'; export default function Greeting() { const t = useTranslations('Greeting'); return <p>{t('hello')}</p>; }
getStaticProps.export async function getStaticProps({locale}) { return { props: { messages: (await import(`../messages/${locale}.json`)).default } }; }
This example shows a Next.js app using next-intl. The App component wraps everything with NextIntlProvider. The Greeting component uses translations from the Greeting namespace. The getStaticProps function loads the right language messages based on the locale.
import {NextIntlProvider, useTranslations} from 'next-intl'; export default function App({Component, pageProps}) { return ( <NextIntlProvider messages={pageProps.messages}> <Component {...pageProps} /> </NextIntlProvider> ); } export function Greeting() { const t = useTranslations('Greeting'); return <h1>{t('hello')}</h1>; } export async function getStaticProps({locale}) { return { props: { messages: (await import(`../messages/${locale}.json`)).default } }; }
Keep your translation files organized by language and namespace for easy management.
Use the useTranslations hook inside components to get translated strings.
Remember to handle fallback languages if a translation is missing.
Next-intl helps you add multiple languages to your Next.js app.
Wrap your app with NextIntlProvider and load messages per locale.
Use useTranslations hook to get translated text inside components.
Practice
NextIntlProvider component in a Next.js app using next-intl?Solution
Step 1: Understand the role of NextIntlProvider
TheNextIntlProviderwraps the app and supplies translation messages and locale information to all child components.Step 2: Compare with other options
Fetching data, routing, and styling are unrelated toNextIntlProvider's purpose.Final Answer:
To provide translation messages and locale context to the app -> Option BQuick Check:
NextIntlProvider = translation context provider [OK]
- Confusing NextIntlProvider with data fetching
- Thinking it handles routing or styling
- Not realizing it shares locale and messages
useTranslations hook in a Next.js component?Solution
Step 1: Check correct import syntax
The hook is nameduseTranslationsand imported with curly braces from 'next-intl'.Step 2: Verify usage with namespace
It requires a namespace string like 'common' to get translations from that section.Final Answer:
import { useTranslations } from 'next-intl'; const t = useTranslations('common'); -> Option AQuick Check:
Correct import and call with namespace = import { useTranslations } from 'next-intl'; const t = useTranslations('common'); [OK]
- Importing without braces
- Calling useTranslations without arguments
- Using wrong hook name useTranslation
import { useTranslations } from 'next-intl';
export default function Greeting() {
const t = useTranslations('greeting');
return <h1>{t('hello')}</h1>;
}If the messages for locale 'en' are
{ greeting: { hello: 'Hello, friend!' } }, what will be rendered?Solution
Step 1: Understand useTranslations usage
The hookuseTranslations('greeting')accesses the 'greeting' namespace in messages.Step 2: Check message key 'hello'
The key 'hello' inside 'greeting' is 'Hello, friend!'. Sot('hello')returns that string.Final Answer:
<h1>Hello, friend!</h1> -> Option CQuick Check:
Namespace + key returns correct text = <h1>Hello, friend!</h1> [OK]
- Expecting raw keys instead of translated text
- Assuming missing keys cause errors
- Confusing namespace with key
import { NextIntlProvider } from 'next-intl';
export default function Page({ messages }) {
return (
<NextIntlProvider messages={messages} locale="en">
<h1>Welcome</h1>
</NextIntlProvider>
);
}But you get an error: "messages is undefined". What is the likely cause?
Solution
Step 1: Check messages prop usage
TheNextIntlProviderrequires a validmessagesobject passed as a prop.Step 2: Identify cause of undefined
Ifmessagesis undefined, it means the parent or data loader did not supply it properly.Final Answer:
You forgot to load and pass messages as a prop to the Page component -> Option AQuick Check:
Missing messages prop causes undefined error [OK]
- Thinking NextIntlProvider doesn't need messages
- Passing locale as number instead of string
- Confusing hooks with provider component
locales/en.json locales/es.json
How should you structure your
app/layout.tsx to load messages dynamically and provide translations to all pages?Solution
Step 1: Understand dynamic message loading
To support multiple locales, load messages dynamically based on the current locale in an async layout or provider.Step 2: Wrap app with NextIntlProvider
Pass the loaded messages and locale toNextIntlProviderwrapping all children so translations are available app-wide.Final Answer:
Use async function to load messages based on locale, then wrap children with NextIntlProvider passing loaded messages and locale -> Option DQuick Check:
Dynamic loading + provider wrapping = Use async function to load messages based on locale, then wrap children withNextIntlProviderpassing loaded messages and locale [OK]
- Importing all locales at once statically
- Loading messages inside pages instead of layout
- Using useTranslations to load messages
Ready to prove it?
Pass the test and this pattern moves unknown
