34 lines
1.1 KiB
TypeScript
34 lines
1.1 KiB
TypeScript
import type { RouterConfig } from '@nuxt/schema'
|
|
import type { RouteRecordRaw } from 'vue-router'
|
|
import { PREFIXED_LOCALES, type SiteLocale } from '~/types/locale'
|
|
|
|
function shouldDuplicateRoute(path: string): boolean {
|
|
if (path.startsWith('/admin')) return false
|
|
if (PREFIXED_LOCALES.some((locale) => path === `/${locale}` || path.startsWith(`/${locale}/`))) {
|
|
return false
|
|
}
|
|
if (path.includes('pathMatch')) return false
|
|
return true
|
|
}
|
|
|
|
function duplicateRouteForLocale(route: RouteRecordRaw, locale: Exclude<SiteLocale, 'en'>): RouteRecordRaw {
|
|
const localePath = route.path === '/' ? `/${locale}` : `/${locale}${route.path}`
|
|
|
|
return {
|
|
...route,
|
|
path: localePath,
|
|
name: route.name ? `${String(route.name)}-${locale}` : undefined,
|
|
children: route.children?.map((child) => duplicateRouteForLocale(child, locale))
|
|
}
|
|
}
|
|
|
|
export default {
|
|
routes: (routes) => {
|
|
const prefixedRoutes = PREFIXED_LOCALES.flatMap((locale) =>
|
|
routes.filter((route) => shouldDuplicateRoute(route.path)).map((route) => duplicateRouteForLocale(route, locale))
|
|
)
|
|
|
|
return [...routes, ...prefixedRoutes]
|
|
}
|
|
} satisfies RouterConfig
|