0
0
NextJSframework~10 mins

Locale detection strategies in NextJS - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Locale detection strategies
User visits site
Check URL for locale
Use URL
Set locale
Render page with locale
The site first looks at the URL for a locale. If none found, it checks the browser's language settings. Then it sets the locale and renders the page accordingly.
Execution Sample
NextJS
export function detectLocale(req) {
  const urlLocale = req.url.match(/^\/([a-z]{2})\//)?.[1];
  if (urlLocale) return urlLocale;
  return req.headers['accept-language']?.split(',')[0].slice(0, 2) || 'en';
}
This function tries to find the locale from the URL first, then from the browser's Accept-Language header, defaulting to 'en'.
Execution Table
StepInput URLAccept-Language HeaderURL Locale Found?Locale SelectedAction
1/fr/homeen-US,en;q=0.9frfrUse locale from URL
2/homees-ES,es;q=0.8noneesUse locale from Accept-Language header
3/homenonenoneenDefault to 'en' locale
4/de/aboutfr-FR,fr;q=0.7dedeUse locale from URL
5/aboutzh-CN,zh;q=0.6nonezhUse locale from Accept-Language header
💡 Locale is selected based on URL if present; otherwise, browser language header; defaults to 'en' if none found.
Variable Tracker
VariableStartAfter 1After 2After 3After 4After 5
urlLocaleundefinedfrundefinedundefineddeundefined
acceptLanguageen-US,en;q=0.9en-US,en;q=0.9es-ES,es;q=0.8nonefr-FR,fr;q=0.7zh-CN,zh;q=0.6
localeundefinedfresendezh
Key Moments - 3 Insights
Why does the function check the URL before the Accept-Language header?
Because the URL locale is explicit and user-controlled, it takes priority over browser settings, as shown in execution_table rows 1 and 4.
What happens if neither URL nor Accept-Language header provides a locale?
The function defaults to 'en' as a safe fallback, demonstrated in execution_table row 3.
How does the function extract the locale from the URL?
It uses a regular expression to match the first two letters after the initial slash, as seen in the code and reflected in urlLocale values in variable_tracker.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what locale is selected at step 2?
Afr
Ben
Ces
Dde
💡 Hint
Check the 'Locale Selected' column at step 2 in the execution_table.
At which step does the locale default to 'en'?
AStep 3
BStep 1
CStep 4
DStep 5
💡 Hint
Look for the row where both URL Locale Found? and Accept-Language Header are 'none' or missing.
If the URL was '/es/home' and Accept-Language was 'fr-FR', what locale would be selected?
Afr
Bes
Cen
Dnone
💡 Hint
URL locale has priority over Accept-Language header as shown in the concept_flow and execution_table.
Concept Snapshot
Locale detection in Next.js:
1. Check URL path for locale code (e.g., '/fr/') first.
2. If none, check browser Accept-Language header.
3. Default to 'en' if no locale found.
This ensures user preference or browser setting guides language.
Use regex to extract locale from URL.
Always have a fallback locale.
Full Transcript
When a user visits a Next.js site, the app tries to detect their language preference. First, it looks at the URL path to see if a locale code like 'fr' or 'de' is present. If it finds one, it uses that locale. If not, it checks the browser's Accept-Language header to guess the preferred language. If that is missing or empty, it defaults to English ('en'). This approach respects explicit user choices in the URL and falls back gracefully. The detection function uses a simple regular expression to extract the locale from the URL and splits the Accept-Language header to get the first language code. This logic helps the app render pages in the right language automatically.