Server component translations let you show text in different languages directly from the server. This helps your app speak your user's language without extra work on the browser.
Server component translations in NextJS
import { useTranslations } from 'next-intl'; export default function Page({ params }) { const t = useTranslations('Namespace'); return ( <main> <h1>{t('title')}</h1> <p>{t('description')}</p> </main> ); }
useTranslations is a hook that fetches translation strings for a given namespace.
This code runs on the server, so translations are loaded before sending HTML to the browser.
const t = useTranslations('Home'); return <h1>{t('welcome')}</h1>;
const t = useTranslations('Common'); return <button>{t('submit')}</button>;
export async function generateStaticParams() { return [ { locale: 'en' }, { locale: 'fr' } ]; }
This server component loads translations from the 'Greeting' namespace. It shows a hello message and a welcome message in the user's language. The app supports English and Spanish locales.
import { useTranslations } from 'next-intl'; export default function Greeting({ params }) { const t = useTranslations('Greeting'); return ( <section> <h2>{t('hello')}</h2> <p>{t('welcomeMessage')}</p> </section> ); } export async function generateStaticParams() { return [ { locale: 'en' }, { locale: 'es' } ]; }
Make sure your translation files are organized by locale and namespace.
Server components do not run in the browser, so translation hooks here fetch data on the server.
Use generateStaticParams to tell Next.js which languages to build pages for.
Server component translations let you show text in different languages from the server.
This improves performance and SEO by sending translated HTML directly to the browser.
Use useTranslations hook inside server components to get translated strings.
Practice
Solution
Step 1: Understand server component translations purpose
Server component translations generate translated HTML on the server before sending it to the browser.Step 2: Identify benefits of server-side translation
This approach improves performance by reducing client work and boosts SEO by sending ready-to-read translated content.Final Answer:
They send translated HTML directly from the server, improving performance and SEO. -> Option AQuick Check:
Server translations = better performance and SEO [OK]
- Thinking translations happen on the client side
- Confusing server translations with real-time chat features
- Assuming translations update without page reload
Solution
Step 1: Identify the correct package for server translations
Next.js server component translations use the 'next-intl' package.Step 2: Check the correct import syntax
The function is named 'getTranslations' and imported with curly braces from 'next-intl/server'.Final Answer:
import { getTranslations } from 'next-intl/server'; -> Option BQuick Check:
Correct import = getTranslations from next-intl/server [OK]
- Using 'useTranslations' instead of 'getTranslations'
- Importing from 'react-intl' or 'next-i18next' by mistake
- Importing from 'next/router' which is unrelated
import { getTranslations } from 'next-intl/server';
export default async function Greeting() {
const t = await getTranslations();
return <h1>{t('greeting')}</h1>;
}Solution
Step 1: Understand getTranslations usage
The function, when awaited, returns a function 't' that looks up translation keys for the current locale.Step 2: Check the translation key and locale
For locale 'en', 'greeting' key maps to 'Hello', so t('greeting') returns 'Hello'.Final Answer:
<h1>Hello</h1> -> Option AQuick Check:
t('greeting') = 'Hello' [OK]
- Expecting the key name to render instead of the value
- Assuming an error if key exists
- Thinking undefined is returned for existing keys
import { getTranslations } from 'next-intl/server';
export default async function Welcome() {
const t = await getTranslations('common');
return <div>{t('welcome')}</div>;
}
Assuming translations for 'common' namespace are not loaded.Solution
Step 1: Understand namespace usage in getTranslations
Passing 'common' scopes the lookup to keys prefixed with 'common.'.Step 2: Check behavior when namespace translations are missing
If 'common.welcome' is missing, it falls back to rendering 'common.welcome'.Final Answer:
It will render the namespaced key 'common.welcome' as fallback text. -> Option CQuick Check:
Missing translation = fallback to namespaced id [OK]
- Expecting an error instead of fallback text
- Assuming silent failure with empty output
- Thinking automatic fallback to English happens
Solution
Step 1: Understand fallback behavior in server translations
Server translations fallback to the message id if key is missing.Step 2: Implement fallback with defaultMessage
Use thedefaultMessageoption int()to provide a custom English fallback string.Final Answer:
Use getTranslations() and call t('greeting', {defaultMessage: 'Hello'}) -> Option DQuick Check:
t(key, {defaultMessage}) = custom fallback [OK]
- Assuming automatic fallback without code
- Loading all locales unnecessarily on server
- Using client hooks in server components incorrectly
Ready to prove it?
Pass the test and this pattern moves unknown
