0
0
NextJSframework~10 mins

Next-intl library integration in NextJS - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Next-intl library integration
Install next-intl
Create messages files
Wrap app with NextIntlProvider
Use useTranslations hook in components
Render translated text
Switch locale to update UI
This flow shows how to add next-intl: install, add messages, wrap app, use translations, and update UI on locale change.
Execution Sample
NextJS
import {NextIntlProvider, useTranslations} from 'next-intl';

export default function App({Component, pageProps}) {
  return <NextIntlProvider messages={pageProps.messages} locale={pageProps.locale}>
    <Component {...pageProps} />
  </NextIntlProvider>;
}

function Greeting() {
  const t = useTranslations('Greeting');
  return <p>{t('hello')}</p>;
}
This code wraps the app with NextIntlProvider and uses useTranslations hook to show translated 'hello' text.
Execution Table
StepActionInput/StateOutput/Result
1Install next-intl packagenpm install next-intlPackage ready to use
2Create messages filesen.json: {"Greeting": {"hello": "Hello!"}}Messages loaded for locales
3Wrap app with NextIntlProviderpageProps.locale='en', pageProps.messages=en.jsonContext provides translations
4Call useTranslations('Greeting') in Greeting componentContext with messagesReturns function t(key)
5Call t('hello')Key='hello'Returns 'Hello!' string
6Render <p>{t('hello')}</p>Text 'Hello!'Paragraph with 'Hello!' shown
7Change locale to 'fr' with messagesfr.json: {"Greeting": {"hello": "Bonjour!"}}UI updates to show 'Bonjour!'
8Call t('hello') againKey='hello', locale='fr'Returns 'Bonjour!' string
9Render updated <p>{t('hello')}</p>Text 'Bonjour!'Paragraph updates to 'Bonjour!'
10End of flowN/AApp shows translated greeting based on locale
💡 Flow ends after rendering translated text and updating UI on locale change
Variable Tracker
VariableStartAfter Step 3After Step 5After Step 7Final
pageProps.localeundefined'en''en''fr''fr'
pageProps.messagesundefined{"Greeting":{"hello":"Hello!"}}{"Greeting":{"hello":"Hello!"}}{"Greeting":{"hello":"Bonjour!"}}{"Greeting":{"hello":"Bonjour!"}}
tundefinedfunctionfunction returning 'Hello!'function returning 'Bonjour!'function returning 'Bonjour!'
Rendered Textnonenone'Hello!''Bonjour!''Bonjour!'
Key Moments - 3 Insights
Why do we need to wrap the app with NextIntlProvider?
NextIntlProvider provides the translation context to all components. Without it, useTranslations hook cannot find messages (see step 3 in execution_table).
How does changing the locale update the displayed text?
Changing locale updates pageProps.locale and pageProps.messages, so useTranslations returns translations for the new locale (see steps 7-9). React re-renders with new text.
What does useTranslations('Greeting') return?
It returns a function t(key) that looks up the translation for the given key in the 'Greeting' namespace (see step 4).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of 'Rendered Text' after step 5?
Aundefined
B'Bonjour!'
C'Hello!'
DAn error
💡 Hint
Check the 'Rendered Text' variable in variable_tracker after step 5
At which step does the UI update to show the French greeting?
AStep 5
BStep 9
CStep 3
DStep 7
💡 Hint
Look at execution_table rows where rendering happens with 'Bonjour!' text
If we remove NextIntlProvider wrapping, what happens when useTranslations is called?
AIt throws an error or returns undefined
BIt returns translations from default locale
CIt works normally
DIt shows untranslated keys
💡 Hint
Refer to key_moments about why NextIntlProvider is needed (step 3)
Concept Snapshot
Next-intl integration steps:
1. Install next-intl package
2. Create locale message JSON files
3. Wrap app with <NextIntlProvider> passing locale and messages
4. Use useTranslations(namespace) hook in components
5. Call t(key) to get translated text
6. Change locale to update UI translations automatically
Full Transcript
This visual execution shows how to integrate the next-intl library in a Next.js app. First, install the package and create JSON files with translations for each locale. Then wrap your app component with NextIntlProvider, passing the current locale and messages. Inside components, use the useTranslations hook with a namespace to get a function t that returns translated strings by key. Rendering t('hello') shows the translated greeting. When the locale changes, the provider updates messages and the UI re-renders with new translations. Key points include the need for NextIntlProvider to provide context and how useTranslations depends on it. This step-by-step trace helps beginners see how state changes flow through the app to update displayed text.