Bird
Raised Fist0
NextJSframework~10 mins

Server component translations in NextJS - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Server component translations
Start Server Component
↓
Load translation files
↓
Select locale based on request
↓
Fetch translations for locale
↓
Render component with translations
↓
Send rendered HTML to client
↓
Client receives translated content
This flow shows how a Next.js server component loads translations based on the user's locale, renders the content with those translations, and sends the HTML to the client.
Execution Sample
NextJS
import { cookies } from 'next/headers';
import { getTranslations } from './i18n';

export default async function Page() {
  const locale = cookies().get('locale')?.value || 'en';
  const t = await getTranslations(locale);
  return <h1>{t('welcome')}</h1>;
}
This server component reads the locale from cookies, loads translations, and renders a heading with the translated 'welcome' text.
Execution Table
StepActionEvaluationResult
1Read cookies for 'locale'cookies().get('locale')?.value'fr'
2Locale found?Is 'fr' defined?Yes
3Load translations for 'fr'await getTranslations('fr'){ welcome: 'Bienvenue' }
4Render <h1> with t('welcome')t('welcome')Bienvenue
5Send rendered HTML to client<h1>Bienvenue</h1>Client receives translated content
💡 Translations loaded and component rendered with locale 'fr', then sent to client.
Variable Tracker
VariableStartAfter Step 1After Step 3Final
localeundefined'fr''fr''fr'
tundefinedundefined{ welcome: 'Bienvenue' }{ welcome: 'Bienvenue' }
Key Moments - 2 Insights
Why do we use 'await' when loading translations?
Because loading translations is asynchronous (it may read files or fetch data), 'await' pauses rendering until translations are ready, as shown in step 3 of the execution_table.
What happens if the 'locale' cookie is missing?
The code defaults to 'en' locale (English), so translations for 'en' are loaded instead, ensuring the component still renders, as seen in step 2 where it checks if locale exists.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of 'locale' after step 1?
A'en'
B'fr'
Cundefined
Dnull
💡 Hint
Check the 'Evaluation' column in step 1 where cookies().get('locale')?.value is read.
At which step does the component get rendered with the translated text?
AStep 2
BStep 3
CStep 4
DStep 5
💡 Hint
Look for the step where

is rendered with t('welcome') in the 'Action' column.

If the locale cookie was missing, what would be the default locale used?
A'en'
B'fr'
Cundefined
D'es'
💡 Hint
Refer to the code line where locale is assigned with fallback 'en' if cookie is missing.
Concept Snapshot
Server component translations in Next.js:
- Read user locale from cookies or default to 'en'
- Load translations asynchronously with await
- Render component with translated strings
- Send fully rendered HTML to client
- Ensures fast, localized content without client JS
Full Transcript
This example shows how a Next.js server component handles translations. First, it reads the user's locale from cookies. If no locale is found, it defaults to English. Then it loads the translation data asynchronously for that locale. After translations are ready, the component renders the content using the translated strings. Finally, the server sends the fully rendered HTML to the client. This approach delivers localized content quickly without needing client-side JavaScript to translate.

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

  1. Step 1: Understand server component translations purpose

    Server component translations generate translated HTML on the server before sending it to the browser.
  2. 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.
  3. Final Answer:

    They send translated HTML directly from the server, improving performance and SEO. -> Option A
  4. 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

  1. Step 1: Identify the correct package for server translations

    Next.js server component translations use the 'next-intl' package.
  2. Step 2: Check the correct import syntax

    The function is named 'getTranslations' and imported with curly braces from 'next-intl/server'.
  3. Final Answer:

    import { getTranslations } from 'next-intl/server'; -> Option B
  4. 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

  1. Step 1: Understand getTranslations usage

    The function, when awaited, returns a function 't' that looks up translation keys for the current locale.
  2. Step 2: Check the translation key and locale

    For locale 'en', 'greeting' key maps to 'Hello', so t('greeting') returns 'Hello'.
  3. Final Answer:

    <h1>Hello</h1> -> Option A
  4. 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

  1. Step 1: Understand namespace usage in getTranslations

    Passing 'common' scopes the lookup to keys prefixed with 'common.'.
  2. Step 2: Check behavior when namespace translations are missing

    If 'common.welcome' is missing, it falls back to rendering 'common.welcome'.
  3. Final Answer:

    It will render the namespaced key 'common.welcome' as fallback text. -> Option C
  4. 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

  1. Step 1: Understand fallback behavior in server translations

    Server translations fallback to the message id if key is missing.
  2. Step 2: Implement fallback with defaultMessage

    Use the defaultMessage option in t() to provide a custom English fallback string.
  3. Final Answer:

    Use getTranslations() and call t('greeting', {defaultMessage: 'Hello'}) -> Option D
  4. Quick Check:

    t(key, {defaultMessage}) = custom fallback [OK]
Hint: t('key', {defaultMessage: 'Hello'}) handles fallback [OK]
Common Mistakes:
  • Assuming automatic fallback without code
  • Loading all locales unnecessarily on server
  • Using client hooks in server components incorrectly
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard