Skip to content

Commit b1d5270

Browse files
committed
add handling to resolveRoutes instead
1 parent 1b9e048 commit b1d5270

File tree

2 files changed

+14
-17
lines changed

2 files changed

+14
-17
lines changed

packages/next/src/server/lib/router-utils/resolve-routes.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -395,6 +395,17 @@ export function getResolveRoutes(
395395
normalized = normalizers.postponed.normalize(normalized, true)
396396
}
397397

398+
if (config.i18n) {
399+
const curLocaleResult = normalizeLocalePath(
400+
normalized,
401+
config.i18n.locales
402+
)
403+
404+
if (curLocaleResult.detectedLocale) {
405+
parsedUrl.query.__nextLocale = curLocaleResult.detectedLocale
406+
}
407+
}
408+
398409
// If we updated the pathname, and it had a base path, re-add the
399410
// base path.
400411
if (updated) {

packages/next/src/shared/lib/i18n/normalize-locale-path.ts

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,8 @@ export function normalizeLocalePath(
1717
locales?: string[]
1818
): PathLocale {
1919
let detectedLocale: string | undefined
20-
const isDataRoute = pathname.startsWith('/_next/data')
21-
22-
// Create a pathname for locale detection, removing /_next/data/[buildId] if present
23-
// This is because locale detection relies on path splitting and so the first part
24-
// should be the locale.
25-
const pathNameNoDataPrefix = isDataRoute
26-
? pathname.replace(/^\/_next\/data\/[^/]+/, '')
27-
: pathname
28-
29-
// Split the path for locale detection
30-
const pathnameParts = pathNameNoDataPrefix.split('/')
20+
// first item will be empty string from splitting at first char
21+
const pathnameParts = pathname.split('/')
3122

3223
;(locales || []).some((locale) => {
3324
if (
@@ -36,17 +27,12 @@ export function normalizeLocalePath(
3627
) {
3728
detectedLocale = locale
3829
pathnameParts.splice(1, 1)
30+
pathname = pathnameParts.join('/') || '/'
3931
return true
4032
}
4133
return false
4234
})
4335

44-
// For non-data routes, we return the path with the locale stripped out.
45-
// For data routes, we keep the path as is, since we only want to extract the locale.
46-
if (detectedLocale && !isDataRoute) {
47-
pathname = pathnameParts.join('/') || '/'
48-
}
49-
5036
return {
5137
pathname,
5238
detectedLocale,

0 commit comments

Comments
 (0)