📖 Scenario: You are building a website that supports two languages: English and Spanish. You want users to see the language in the URL as a sub-path, like /en for English and /es for Spanish.
🎯 Goal: Create a Next.js app that uses sub-path routing to show content in English or Spanish based on the URL prefix /en or /es.
📋 What You'll Learn
Create a Next.js page component that shows a greeting message.
Add a configuration variable to define supported locales.
Use Next.js routing to detect the locale from the URL sub-path.
Render the greeting message in the correct language based on the locale.
💡 Why This Matters
🌍 Real World
Many websites support multiple languages and show the language in the URL to help users and search engines understand the content language.
💼 Career
Knowing how to implement locale-based routing is important for building internationalized web apps that reach a global audience.
Progress0 / 4 steps
1
Create the greeting messages object
Create a constant called greetings that is an object with two keys: en and es. Set en to the string 'Hello!' and es to the string '¡Hola!'.
NextJS
Hint
Use const greetings = { en: 'Hello!', es: '¡Hola!' }.
2
Define supported locales array
Create a constant called locales that is an array containing the strings 'en' and 'es'.
NextJS
Hint
Use const locales = ['en', 'es'];.
3
Create the Next.js page component with locale detection
Create a default exported React functional component called HomePage. Use the Next.js useRouter hook to get the router object. Get the locale using router.locale, defaulting to 'en' if not present. Render a <main> element with a <h1> that shows the greeting message from greetings for the detected locale.
NextJS
Hint
Use const locale = router.locale || 'en';
4
Add Next.js config for i18n sub-path routing
Create a next.config.js file exporting an object with an i18n property. Set locales to the locales array and defaultLocale to 'en'. This enables Next.js sub-path routing for locales.
1. What is the main purpose of sub-path routing for locales in Next.js?
easy
A. To create separate apps for each language without sharing code.
B. To serve different language versions of the site using URL prefixes like /en or /fr.
C. To load all languages at once on the homepage.
D. To disable internationalization features in Next.js.
Solution
Step 1: Understand sub-path routing concept
Sub-path routing uses URL parts like /en or /fr to 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 /en or /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 B
Quick Check:
Sub-path routing = URL prefixes for languages [OK]
Hint: Remember: sub-path routing adds language code in URL path [OK]
Common Mistakes:
Thinking sub-path routing loads all languages at once
Confusing sub-path routing with separate apps per language
Believing it disables i18n features
2. Which is the correct way to configure supported locales in next.config.js for sub-path routing?
easy
A. "i18n: ['en', 'fr'], defaultLocale: 'en'"
B. "locales: ['en', 'fr'], default: 'en'"
C. "i18n: { languages: ['en', 'fr'], defaultLang: 'en' }"
D. "i18n: { locales: ['en', 'fr'], defaultLocale: 'en' }"
Solution
Step 1: Recall Next.js i18n config structure
The correct config uses the i18n object with locales array and defaultLocale string.
Step 2: Match options to correct syntax
"i18n: { locales: ['en', 'fr'], defaultLocale: 'en' }" matches the exact syntax. Others use wrong keys or structure.
A. The config is correct and will enable sub-path routing with locale detection.
B. localeDetection is not a valid option inside i18n.
C. locales must include the default locale twice.
D. localeDetection should be localeDetection: false to enable sub-path routing.
Solution
Step 1: Check valid i18n config options
Next.js supports localeDetection inside i18n to enable or disable automatic locale detection.
Step 2: Verify given config
The config has valid keys and values. Setting localeDetection: true enables automatic detection, which is allowed.
Final Answer:
The config is correct and will enable sub-path routing with locale detection. -> Option A
Quick Check:
localeDetection is valid and true means enabled [OK]
Hint: Check Next.js docs for valid i18n keys [OK]
Common Mistakes:
Thinking localeDetection is invalid
Believing localeDetection must be false for sub-path routing
Duplicating default locale in locales array
5. You want to create a Next.js app supporting English and French with sub-path routing. You want the default locale to be English and URLs like /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.
hard
A. Only A, B, and C are needed.
B. All A, B, C, and D are needed.
C. Only A and B are needed.
D. Only B and D are needed.
Solution
Step 1: Configure locales and defaultLocale
Setting i18n.locales and defaultLocale in next.config.js enables sub-path routing for English and French.
Step 2: Use useRouter to get locale and show content
Using useRouter() to get locale helps render language-specific content dynamically.
Step 3: Organize routes for each locale
Creating folders or files under app or pages supports 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.