0
0
NextJSframework~20 mins

Server component translations in NextJS - Practice Problems & Coding Challenges

Choose your learning style9 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.