Given this Next.js server component using next-intl for translations, what will be rendered when the locale is set to en?
import {useTranslations} from 'next-intl/server'; export default function Greeting() { const t = useTranslations('Greeting'); return <h1>{t('hello')}</h1>; } // Assume messages for 'en': { "Greeting": { "hello": "Hello, friend!" } }
Remember that useTranslations returns a function to get the translated string by key.
The useTranslations('Greeting') hook returns a function t that looks up keys inside the Greeting namespace. Calling t('hello') returns the translated string Hello, friend! for the en locale.
In Next.js 14 server components, which code snippet correctly loads locale messages for translations?
Consider synchronous vs asynchronous file reading in server components.
Server components can use synchronous APIs like readFileSync to load files. Option C correctly uses readFileSync inside an async function and parses JSON synchronously. Option C uses readFile without awaiting, causing an error. Option C is not async but uses synchronous read, which is allowed but less recommended. Option C incorrectly awaits a synchronous function.
Consider this server component that switches locale based on params. What will be the output if params.locale is fr?
import {useTranslations} from 'next-intl/server'; export default function Page({params}) { const t = useTranslations('Home'); return <p>{t('welcome')}</p>; } // Messages: // en: { Home: { welcome: 'Welcome!' } } // fr: { Home: { welcome: 'Bienvenue!' } }
The locale is passed via params and affects which messages are loaded.
The useTranslations hook uses the current locale from the server context, which is set by params.locale. For fr, it loads French messages, so t('welcome') returns Bienvenue!.
Given this server component code, why does it throw an error?
import {useTranslations} from 'next-intl'; export default async function Page() { const t = useTranslations('Common'); return <p>{t('hello')}</p>; }
Check the import source for useTranslations in server components.
In Next.js server components, you must import useTranslations from next-intl/server instead of next-intl. Importing from next-intl is for client components and causes errors in server components.
Which statement best explains the performance benefit of using server component translations in Next.js?
Think about where translation data is loaded and how it affects client code size.
Server component translations load and render translated text on the server. This reduces the JavaScript sent to the client because translation logic and data do not need to be included in client bundles. It also avoids client runtime translation loading, improving performance and reducing flicker.