Sub-path routing for locales helps show your website in different languages by using parts of the URL. It makes it easy for visitors to see content in their language.
Sub-path routing for locales in NextJS
export const nextConfig = { i18n: { locales: ['en', 'fr', 'es'], defaultLocale: 'en', localeDetection: true } };
The locales array lists all supported languages.
The defaultLocale is the language shown when no locale is specified.
i18n: {
locales: ['en', 'fr'],
defaultLocale: 'en'
}i18n: {
locales: ['en', 'fr', 'de'],
defaultLocale: 'fr'
}This Next.js component shows the current language and available languages using sub-path routing. The config sets English, French, and Spanish as supported languages with English as default.
import { useRouter } from 'next/router'; export default function Home() { const { locale, locales, defaultLocale, asPath } = useRouter(); return ( <main> <h1>Welcome!</h1> <p>Current language: {locale}</p> <p>Default language: {defaultLocale}</p> <p>Available languages: {locales.join(', ')}</p> <p>Current path: {asPath}</p> </main> ); } // next.config.js const nextConfig = { i18n: { locales: ['en', 'fr', 'es'], defaultLocale: 'en' } }; export default nextConfig;
When you visit /fr, the page shows French content automatically.
Make sure to add translations for each locale in your app.
Locale detection can be turned on or off to control automatic language selection.
Sub-path routing uses URL parts like /en or /fr to show different languages.
Configure supported languages in next.config.js under i18n.
Use Next.js router to get current locale and show language-specific content.
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
