A server component in Next.js is a React component that runs only on the server. It can fetch data and render HTML before sending it to the browser, improving performance and SEO.
Click to reveal answer
beginner
How do you load translations in a Next.js server component?
You import the translation function or JSON files directly in the server component and use them to render translated text before sending HTML to the client.
Click to reveal answer
intermediate
Why are server components good for translations in Next.js?
Because server components run on the server, they can load translations once and render fully translated HTML. This avoids loading translation data on the client and speeds up page load.
Click to reveal answer
intermediate
What is the role of the 'use' hook with translations in Next.js server components?
The 'use' hook can be used to await promises like loading translation files inside server components, allowing translations to be ready before rendering.
Click to reveal answer
advanced
How do you handle dynamic locale switching in Next.js server components?
You pass the locale as a prop or get it from the URL, then load the corresponding translation files on the server component before rendering the page.
Click to reveal answer
Where do Next.js server components run?
AIn the browser's cache
BOn the client only
CBoth client and server
DOn the server only
✗ Incorrect
Server components run only on the server, not on the client.
What is a benefit of loading translations in server components?
ATranslations load faster because HTML is pre-rendered with text
BTranslations update automatically on the client
CTranslations are stored in browser local storage
DTranslations require no server resources
✗ Incorrect
Server components pre-render HTML with translations, speeding up page load.
How can you get the current locale in a Next.js server component?
ABy calling a client-side hook
BUsing window.navigator.language
CFrom the URL or props passed to the component
DFrom localStorage on the client
✗ Incorrect
Server components get locale from URL params or props, not client APIs.
Which hook helps await translation loading in server components?
AuseState()
Buse()
CuseEffect()
DuseTranslation()
✗ Incorrect
The 'use' hook can await promises in server components.
What happens if you try to use client-only translation hooks in server components?
AIt causes errors because client hooks don't run on server
BIt works fine without issues
CIt improves performance
DIt caches translations on the server
✗ Incorrect
Client-only hooks cannot run in server components and cause errors.
Explain how translations are loaded and rendered in Next.js server components.
Think about where the code runs and how translations get into the HTML.
You got /5 concepts.
Describe the advantages of using server components for translations in Next.js compared to client components.
Consider performance and user experience benefits.
You got /5 concepts.
Practice
(1/5)
1. What is the main benefit of using server component translations in Next.js?
easy
A. They send translated HTML directly from the server, improving performance and SEO.
B. They allow translations to be updated on the client without reloading the page.
C. They automatically translate user input in forms.
D. They enable real-time chat translations between users.
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 A
Quick Check:
Server translations = better performance and SEO [OK]
Hint: Server translations improve SEO by sending ready HTML [OK]
Common Mistakes:
Thinking translations happen on the client side
Confusing server translations with real-time chat features
Assuming translations update without page reload
2. Which is the correct way to import the translation function in a Next.js server component?
easy
A. import useTranslations from 'react-intl';
B. import { getTranslations } from 'next-intl/server';
C. import { useTranslation } from 'next-i18next';
D. import { useTranslations } from 'next/router';
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 B
Quick Check:
Correct import = getTranslations from next-intl/server [OK]
Hint: Use curly braces to import getTranslations from next-intl/server [OK]
Common Mistakes:
Using 'useTranslations' instead of 'getTranslations'
Importing from 'react-intl' or 'next-i18next' by mistake
Importing from 'next/router' which is unrelated
3. Given this server component code, what will be rendered if the locale is 'en' and translations contain { greeting: 'Hello' }?
import { getTranslations } from 'next-intl/server';
export default async function Greeting() {
const t = await getTranslations();
return <h1>{t('greeting')}</h1>;
}
medium
A.
Hello
B.
greeting
C. Error: Missing translation key
D.
undefined
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 A
Quick Check:
t('greeting') = 'Hello' [OK]
Hint: t('key') returns translated string for current locale [OK]
Common Mistakes:
Expecting the key name to render instead of the value
Assuming an error if key exists
Thinking undefined is returned for existing keys
4. What is wrong with this server component code for translations?
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.
medium
A. It will throw an error because the 'common' namespace translations are missing.
B. It will silently render nothing for missing keys.
C. It will render the namespaced key 'common.welcome' as fallback text.
D. It will fallback to English translations automatically.
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 C
Quick Check:
Missing translation = fallback to namespaced id [OK]
Hint: Missing namespace translation falls back to 'common.welcome' [OK]
Common Mistakes:
Expecting an error instead of fallback text
Assuming silent failure with empty output
Thinking automatic fallback to English happens
5. You want to create a server component that shows a greeting in the user's locale and falls back to English if the translation is missing. Which approach correctly handles this?
hard
A. Wrap getTranslations() with a try-catch and return English string on error.
B. Load all locales on the server and use getTranslations() without fallback logic.
C. Use client-side translation hooks to handle fallback automatically.
D. Use getTranslations() and call t('greeting', {defaultMessage: 'Hello'}).
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 the defaultMessage option in t() to provide a custom English fallback string.
Final Answer:
Use getTranslations() and call t('greeting', {defaultMessage: 'Hello'}) -> Option D