Bird
Raised Fist0
NextJSframework~20 mins

Server component translations in NextJS - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Server Translation Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate
2:00remaining
What is the rendered output of this server component using next-intl?

Given this Next.js server component using next-intl for translations, what will be rendered when the locale is set to en?

NextJS
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!" } }
A<h1>Hello, friend!</h1>
B<h1>Greeting.hello</h1>
C<h1>hello</h1>
D<h1>undefined</h1>
Attempts:
2 left
💡 Hint

Remember that useTranslations returns a function to get the translated string by key.

📝 Syntax
intermediate
2:00remaining
Which option correctly loads translations in a Next.js server component?

In Next.js 14 server components, which code snippet correctly loads locale messages for translations?

A
import {readFileSync} from 'fs';

export default function Page({params}) {
  const messages = JSON.parse(readFileSync(`./locales/${params.locale}.json`, 'utf-8'));
  return &lt;div&gt;{messages.title}&lt;/div&gt;;
}
B
import {readFile} from 'fs/promises';

export default function Page({params}) {
  const messages = JSON.parse(readFile(`./locales/${params.locale}.json`));
  return &lt;div&gt;{messages.title}&lt;/div&gt;;
}
C
import {readFileSync} from 'fs';

export default async function Page({params}) {
  const messages = JSON.parse(readFileSync(`./locales/${params.locale}.json`, 'utf-8'));
  return &lt;div&gt;{messages.title}&lt;/div&gt;;
}
D
import {readFileSync} from 'fs';

export default async function Page({params}) {
  const messages = await JSON.parse(readFileSync(`./locales/${params.locale}.json`, 'utf-8'));
  return &lt;div&gt;{messages.title}&lt;/div&gt;;
}
Attempts:
2 left
💡 Hint

Consider synchronous vs asynchronous file reading in server components.

❓ state_output
advanced
2:00remaining
What is the output when switching locales in a server component with next-intl?

Consider this server component that switches locale based on params. What will be the output if params.locale is fr?

NextJS
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!' } }
A<p>Bienvenue!</p>
B<p>Welcome!</p>
C<p>Home.welcome</p>
D<p>undefined</p>
Attempts:
2 left
💡 Hint

The locale is passed via params and affects which messages are loaded.

🔧 Debug
advanced
2:00remaining
Why does this server component throw an error when using useTranslations?

Given this server component code, why does it throw an error?

NextJS
import {useTranslations} from 'next-intl';

export default async function Page() {
  const t = useTranslations('Common');
  return <p>{t('hello')}</p>;
}
AuseTranslations cannot be used in async server components because it relies on React hooks.
BThe component must import 'useTranslations' from 'next-intl/server' in server components.
CThe component is missing a 'use client' directive, so hooks cannot run.
DThe 'Common' namespace is missing from the messages, causing a runtime error.
Attempts:
2 left
💡 Hint

Check the import source for useTranslations in server components.

🧠 Conceptual
expert
2:00remaining
How does Next.js server component translations improve performance compared to client-side translations?

Which statement best explains the performance benefit of using server component translations in Next.js?

AServer components cache translations in the browser, improving client-side rendering speed.
BTranslations are fetched on the client after page load, allowing faster initial server response.
CUsing server component translations disables React hydration, making the page static.
DTranslations are loaded and rendered on the server, reducing client bundle size and avoiding runtime translation loading on the client.
Attempts:
2 left
💡 Hint

Think about where translation data is loaded and how it affects client code size.

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