Discover how a simple URL structure can save you hours of confusing link fixes!
Why Sub-path routing for locales in NextJS? - Purpose & Use Cases
Imagine building a website that supports English and Spanish. You try to handle language switching by manually creating separate pages like /home-en and /home-es and linking them everywhere.
This manual approach quickly becomes confusing and error-prone. You must remember to update links everywhere, and users can get lost if URLs are inconsistent. It's hard to keep track of which page belongs to which language.
Sub-path routing for locales automatically organizes your site URLs by language, like /en/home and /es/home. Next.js handles the routing and language detection, so you don't have to manage links manually.
if (lang === 'en') { router.push('/home-en') } else { router.push('/home-es') }
router.push(`/${locale}/home`)This makes your site easier to maintain and provides a smooth, consistent experience for users switching languages.
A global company website where visitors automatically see content in their language with URLs like /en/products or /es/productos, improving clarity and SEO.
Manual language URL handling is confusing and error-prone.
Sub-path routing organizes URLs by locale automatically.
It simplifies maintenance and improves user experience.
Practice
Solution
Step 1: Understand sub-path routing concept
Sub-path routing uses URL parts like/enor/frto show different language versions of the same site.Step 2: Compare options
Only To serve different language versions of the site using URL prefixes like/enor/fr. correctly describes this purpose. Other options describe unrelated or incorrect behaviors.Final Answer:
To serve different language versions of the site using URL prefixes like /en or /fr. -> Option BQuick Check:
Sub-path routing = URL prefixes for languages [OK]
- Thinking sub-path routing loads all languages at once
- Confusing sub-path routing with separate apps per language
- Believing it disables i18n features
next.config.js for sub-path routing?Solution
Step 1: Recall Next.js i18n config structure
The correct config uses thei18nobject withlocalesarray anddefaultLocalestring.Step 2: Match options to correct syntax
"i18n: { locales: ['en', 'fr'], defaultLocale: 'en' }" matches the exact syntax. Others use wrong keys or structure.Final Answer:
"i18n: { locales: ['en', 'fr'], defaultLocale: 'en' }" -> Option DQuick Check:
i18n config = locales + defaultLocale [OK]
- Using wrong keys like 'languages' or 'defaultLang'
- Not wrapping locales inside 'i18n' object
- Using arrays without keys
/fr?
import { useRouter } from 'next/router';
export default function Home() {
const { locale } = useRouter();
return <div>Current language: {locale}</div>;
}Solution
Step 1: Understand useRouter locale property
TheuseRouterhook provides the current locale from the URL sub-path like/fr.Step 2: Match URL to locale value
Since URL is/fr,localewill be 'fr', so the rendered text is "Current language: fr".Final Answer:
Current language: fr -> Option CQuick Check:
URL /fr sets locale = 'fr' [OK]
- Assuming default locale always shows
- Thinking locale is undefined without explicit code
- Confusing useRouter hook usage
next.config.js snippet for locale sub-path routing:
module.exports = {
i18n: {
locales: ['en', 'fr'],
defaultLocale: 'en',
localeDetection: true,
},
};Solution
Step 1: Check valid i18n config options
Next.js supportslocaleDetectioninsidei18nto enable or disable automatic locale detection.Step 2: Verify given config
The config has valid keys and values. SettinglocaleDetection: trueenables automatic detection, which is allowed.Final Answer:
The config is correct and will enable sub-path routing with locale detection. -> Option AQuick Check:
localeDetection is valid and true means enabled [OK]
- Thinking localeDetection is invalid
- Believing localeDetection must be false for sub-path routing
- Duplicating default locale in locales array
/fr/about to show French content. Which steps are needed to correctly configure this?
A) Set i18n.locales to ['en', 'fr'] and defaultLocale to 'en' in next.config.js.
B) Use useRouter() in pages to get locale and show language-specific content.
C) Create folders or files under app or pages to handle routes for each locale.
D) Disable locale detection to force manual locale switching.Solution
Step 1: Configure locales and defaultLocale
Settingi18n.localesanddefaultLocaleinnext.config.jsenables sub-path routing for English and French.Step 2: Use useRouter to get locale and show content
UsinguseRouter()to getlocalehelps render language-specific content dynamically.Step 3: Organize routes for each locale
Creating folders or files underapporpagessupports localized routes like/fr/about.Step 4: Evaluate locale detection setting
Disabling locale detection is optional and not required to support sub-path routing correctly.Final Answer:
Only A, B, and C are needed. -> Option AQuick Check:
Locales config + useRouter + route files = correct setup [OK]
- Thinking disabling locale detection is mandatory
- Not creating route files for localized paths
- Ignoring useRouter for locale detection
Ready to prove it?
Pass the test and this pattern moves unknown
