Consider this Next.js functional component using next-intl for translations. What will it display when the locale is set to en and the messages contain { greeting: 'Hello, {name}!' }?
import {useTranslations} from 'next-intl'; export default function Greeting({name}) { const t = useTranslations(); return <p>{t('greeting', {name})}</p>; }
Think about how useTranslations replaces placeholders in the message.
The useTranslations hook fetches the message for the key greeting and replaces {name} with the passed prop value. So it outputs "Hello, John!".
You want to load translations for a Next.js page using next-intl. Which code snippet correctly fetches messages for the current locale?
export async function getStaticProps({locale}) { // Fill in the correct code to load messages }
Remember that getStaticProps runs on the server and supports dynamic imports.
Using await import() dynamically loads the JSON file and you must access default to get the messages object. Other options either use unsupported syntax or asynchronous code incorrectly.
Given this page code, why does it throw TypeError: t is not a function when rendering?
import {useTranslations} from 'next-intl'; export default function Page() { const t = useTranslations; return <p>{t('welcome')}</p>; }
Check how useTranslations is assigned and used.
The code assigns t = useTranslations without parentheses, so t is the hook function itself, not the translation function. Calling t('welcome') causes a TypeError.
In a Next.js app using next-intl, you have a button that changes the locale from 'en' to 'fr'. What will the component display after clicking the button?
import {useTranslations, useLocale, useRouter} from 'next-intl'; import {useState} from 'react'; export default function LocaleSwitcher() { const t = useTranslations(); const locale = useLocale(); const router = useRouter(); const [currentLocale, setCurrentLocale] = useState(locale); async function switchLocale() { const newLocale = currentLocale === 'en' ? 'fr' : 'en'; setCurrentLocale(newLocale); await router.push('/', '/', {locale: newLocale}); } return ( <> <p>{t('greeting')}</p> <button onClick={switchLocale}>Switch Locale</button> </> ); }
Consider how router.push with a new locale affects the page and translations.
Calling router.push with a new locale reloads the page with that locale, so useTranslations returns messages in French, updating the greeting text.
In Next.js App Router (app directory), how does next-intl recommend loading locale messages for server components?
Think about Next.js App Router's server components and static imports.
Next-intl recommends loading messages in async server components by dynamically importing JSON files based on the locale parameter. This leverages server rendering and avoids client fetching.