Skip to content

Commit

Permalink
fix: custom child route localization (#2743)
Browse files Browse the repository at this point in the history
  • Loading branch information
BobbieGoede committed Jan 31, 2024
1 parent 545ad1d commit 1cb9e70
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 8 deletions.
25 changes: 19 additions & 6 deletions src/routing.ts
Expand Up @@ -61,6 +61,10 @@ type LocalizeRouteParams = {
* parent route
*/
parent?: NuxtPage
/**
* localized parent route
*/
parentLocalized?: NuxtPage
/**
* indicates whether this is a default route for 'prefix_and_default' strategy
*/
Expand All @@ -82,7 +86,10 @@ export function localizeRoutes(routes: NuxtPage[], options: LocalizeRoutesParams
return routes
}

function localizeRoute(route: NuxtPage, { locales = [], parent, extra = false }: LocalizeRouteParams): NuxtPage[] {
function localizeRoute(
route: NuxtPage,
{ locales = [], parent, parentLocalized, extra = false }: LocalizeRouteParams
): NuxtPage[] {
// skip route localization
if (route.redirect && !route.file) {
return [route]
Expand Down Expand Up @@ -121,11 +128,6 @@ export function localizeRoutes(routes: NuxtPage[], options: LocalizeRoutesParams
// localize name if set
localized.name &&= join(...nameSegments)

// localize child routes if set
localized.children &&= localized.children.flatMap(child =>
localizeRoute(child, { locales: [locale], parent: route, extra })
)

// use custom path if found
localized.path = componentOptions.paths?.[locale] ?? localized.path

Expand All @@ -139,6 +141,17 @@ export function localizeRoutes(routes: NuxtPage[], options: LocalizeRoutesParams
}

localized.path &&= adjustRoutePathForTrailingSlash(localized, options.trailingSlash)

// remove parent path from child route
if (parentLocalized != null) {
localized.path = localized.path.replace(parentLocalized.path + '/', '')
}

// localize child routes if set
localized.children &&= localized.children.flatMap(child =>
localizeRoute(child, { locales: [locale], parent: route, parentLocalized: localized, extra })
)

localizedRoutes.push(localized)
}

Expand Down
4 changes: 2 additions & 2 deletions test/pages/__snapshots__/custom_route.test.ts.snap
Expand Up @@ -56,7 +56,7 @@ exports[`#1649 1`] = `
"children": [],
"file": "/path/to/1649/pages/account/addresses.vue",
"name": "account-addresses___fr",
"path": "/fr/compte/adresses",
"path": "adresses",
},
{
"children": [],
Expand All @@ -68,7 +68,7 @@ exports[`#1649 1`] = `
"children": [],
"file": "/path/to/1649/pages/account/profile.vue",
"name": "account-profile___fr",
"path": "/fr/compte/profil",
"path": "profil",
},
],
"file": "/path/to/1649/pages/account.vue",
Expand Down

0 comments on commit 1cb9e70

Please sign in to comment.