0
0
NextJSframework~20 mins

Next-intl library integration in NextJS - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Next-intl Mastery
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 Next.js component using next-intl?

Consider this Next.js functional component using next-intl for translations. What will it display when the locale is set to en and the messages contain { greeting: 'Hello, {name}!' }?

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

export default function Greeting({name}) {
  const t = useTranslations();
  return <p>{t('greeting', {name})}</p>;
}
AHello, {name}!
BHello, John!
Cgreeting
DError: Missing translation
Attempts:
2 left
💡 Hint

Think about how useTranslations replaces placeholders in the message.

📝 Syntax
intermediate
2:00remaining
Which option correctly initializes next-intl messages in a Next.js page?

You want to load translations for a Next.js page using next-intl. Which code snippet correctly fetches messages for the current locale?

NextJS
export async function getStaticProps({locale}) {
  // Fill in the correct code to load messages
}
A
const messages = JSON.parse(`../messages/${locale}.json`);
return {props: {messages}};
B
const messages = require(`../messages/${locale}.json`);
return {props: {messages}};
C
const messages = fetch(`../messages/${locale}.json`).then(res =&gt; res.json());
return {props: {messages}};
D
const messages = await import(`../messages/${locale}.json`);
return {props: {messages: messages.default}};
Attempts:
2 left
💡 Hint

Remember that getStaticProps runs on the server and supports dynamic imports.

🔧 Debug
advanced
2:00remaining
Why does this Next.js page using next-intl throw a runtime error?

Given this page code, why does it throw TypeError: t is not a function when rendering?

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

export default function Page() {
  const t = useTranslations;
  return <p>{t('welcome')}</p>;
}
ABecause the component is missing a <IntlProvider> wrapper around it.
BBecause the translation key 'welcome' is missing in messages, causing t to be undefined.
CBecause useTranslations is assigned without calling it, so t is not a function but a reference to the hook.
DBecause useTranslations must be called with a namespace string argument.
Attempts:
2 left
💡 Hint

Check how useTranslations is assigned and used.

state_output
advanced
2:00remaining
What is the output after changing locale dynamically with next-intl?

In a Next.js app using next-intl, you have a button that changes the locale from 'en' to 'fr'. What will the component display after clicking the button?

NextJS
import {useTranslations, useLocale, useRouter} from 'next-intl';
import {useState} from 'react';

export default function LocaleSwitcher() {
  const t = useTranslations();
  const locale = useLocale();
  const router = useRouter();
  const [currentLocale, setCurrentLocale] = useState(locale);

  async function switchLocale() {
    const newLocale = currentLocale === 'en' ? 'fr' : 'en';
    setCurrentLocale(newLocale);
    await router.push('/', '/', {locale: newLocale});
  }

  return (
    <>
      <p>{t('greeting')}</p>
      <button onClick={switchLocale}>Switch Locale</button>
    </>
  );
}
AThe greeting text updates to French after clicking the button.
BThe greeting text stays in English even after clicking the button.
CThe page reloads but the greeting text does not change.
DAn error occurs because useLocale cannot be updated this way.
Attempts:
2 left
💡 Hint

Consider how router.push with a new locale affects the page and translations.

🧠 Conceptual
expert
3:00remaining
Which statement best describes how next-intl handles message loading in Next.js App Router?

In Next.js App Router (app directory), how does next-intl recommend loading locale messages for server components?

AUse async server components to import JSON message files dynamically based on the locale parameter.
BUse a global context provider that fetches messages once on the client side and shares them.
CLoad all locale messages statically at build time and pass them as props to client components.
DUse client components with useEffect to fetch messages from an API endpoint at runtime.
Attempts:
2 left
💡 Hint

Think about Next.js App Router's server components and static imports.