But you get an error: "messages is undefined". What is the likely cause?
medium
A. You forgot to load and pass messages as a prop to the Page component
B. NextIntlProvider does not accept a messages prop
C. Locale must be passed as a number, not a string
D. You must import useTranslations instead of NextIntlProvider
Solution
Step 1: Check messages prop usage
The NextIntlProvider requires a valid messages object passed as a prop.
Step 2: Identify cause of undefined
If messages is undefined, it means the parent or data loader did not supply it properly.
Final Answer:
You forgot to load and pass messages as a prop to the Page component -> Option A
Quick Check:
Missing messages prop causes undefined error [OK]
Hint: Always load and pass messages before using NextIntlProvider [OK]
Common Mistakes:
Thinking NextIntlProvider doesn't need messages
Passing locale as number instead of string
Confusing hooks with provider component
5. You want to support English and Spanish in your Next.js app using next-intl. You have message files:
locales/en.json
locales/es.json
How should you structure your app/layout.tsx to load messages dynamically and provide translations to all pages?
hard
A. Load messages inside each page component instead of layout
B. Import both JSON files statically and pass both to NextIntlProvider at once
C. Use useTranslations in layout to load messages and pass to children
D. Use async function to load messages based on locale, then wrap children with NextIntlProvider passing loaded messages and locale
Solution
Step 1: Understand dynamic message loading
To support multiple locales, load messages dynamically based on the current locale in an async layout or provider.
Step 2: Wrap app with NextIntlProvider
Pass the loaded messages and locale to NextIntlProvider wrapping all children so translations are available app-wide.
Final Answer:
Use async function to load messages based on locale, then wrap children with NextIntlProvider passing loaded messages and locale -> Option D
Quick Check:
Dynamic loading + provider wrapping = Use async function to load messages based on locale, then wrap children with NextIntlProvider passing loaded messages and locale [OK]
Hint: Load messages async by locale, wrap app with NextIntlProvider [OK]
Common Mistakes:
Importing all locales at once statically
Loading messages inside pages instead of layout
Using useTranslations to load messages
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.