0
0
NextJSframework~10 mins

Sub-path routing for locales in NextJS - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Sub-path routing for locales
User visits URL
Check URL path prefix
Match locale sub-path?
Load locale-specific content
Render page with locale
User sees page in chosen language
The app checks the URL prefix to detect the locale, loads content for that locale, and renders the page accordingly.
Execution Sample
NextJS
export const i18n = {
  locales: ['en', 'fr', 'es'],
  defaultLocale: 'en',
  localeDetection: false
};

// Example URL: /fr/about loads French content
This config sets up sub-path routing for English, French, and Spanish locales.
Execution Table
StepURL VisitedLocale DetectedActionPage Rendered
1/en/homeenLoad English contentHome page in English
2/fr/aboutfrLoad French contentAbout page in French
3/es/contactesLoad Spanish contentContact page in Spanish
4/homeen (default)Load default English contentHome page in English
5/de/homenoneFallback to default localeHome page in English
💡 Routing stops after locale detection and page render for each URL.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4After Step 5
localeundefinedenfresenen
pageContentundefinedEnglish contentFrench contentSpanish contentEnglish contentEnglish content
Key Moments - 2 Insights
How does the app know which language to show when the URL has no locale prefix?
The app uses the defaultLocale ('en') when no locale prefix is found, as shown in step 4 of the execution_table.
What happens if the URL has a locale prefix not in the locales list?
The app falls back to the defaultLocale, as in step 5 where '/de/home' is not recognized and English content is loaded.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what locale is detected when visiting '/fr/about'?
Aen
Bfr
Ces
DdefaultLocale
💡 Hint
Check Step 2 in the execution_table under 'Locale Detected'.
At which step does the app use the default locale because no prefix is present?
AStep 1
BStep 3
CStep 4
DStep 5
💡 Hint
Look for the step where URL has no locale prefix and defaultLocale is used.
If we add 'de' to the locales array, how would step 5 change?
ALocale detected would be 'de' and German content loads
BLocale detected remains 'en' and English content loads
CLocale detected is undefined and fallback triggers
DPage would not render
💡 Hint
Refer to variable_tracker and execution_table step 5 about locale detection and fallback.
Concept Snapshot
Sub-path routing uses URL prefixes like /en, /fr to detect locale.
Next.js loads content based on this prefix.
If no prefix, defaultLocale is used.
Unknown prefixes fallback to defaultLocale.
This enables multi-language sites with clear URLs.
Full Transcript
Sub-path routing for locales in Next.js means the app looks at the start of the URL path to find a language code like 'en' or 'fr'. If it finds one, it loads the content for that language. If not, it uses the default language, usually English. If the URL has a language code not supported, it falls back to the default. This way, users see the page in their language based on the URL they visit.