diff --git a/package-lock.json b/package-lock.json index 317489c..67f107a 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "nuxt-i18n-micro", - "version": "1.29.4", + "version": "1.29.5", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "nuxt-i18n-micro", - "version": "1.29.4", + "version": "1.29.5", "license": "MIT", "workspaces": [ "client", @@ -14324,6 +14324,10 @@ } } }, + "node_modules/nuxt-app": { + "resolved": "test/fixtures/kkvilzxovy", + "link": true + }, "node_modules/nuxt-site-config": { "version": "2.2.18", "resolved": "https://registry.npmjs.org/nuxt-site-config/-/nuxt-site-config-2.2.18.tgz", @@ -21384,6 +21388,13 @@ "resolved": "src/module", "link": true }, + "test/fixtures/kkvilzxovy": { + "name": "nuxt-app", + "hasInstallScript": true, + "devDependencies": { + "@nuxt/content": "^2.13.2" + } + }, "test/fixtures/layer": {}, "test/fixtures/named": {}, "test/fixtures/redirect": {}, diff --git a/src/page-manager.ts b/src/page-manager.ts index 9fd60ce..4a282c4 100644 --- a/src/page-manager.ts +++ b/src/page-manager.ts @@ -14,6 +14,10 @@ import { extractLocaleRoutes, } from './utils' +const buildRouteNameFromRoute = (name: string | null | undefined, path: string | null | undefined) => { + return name ?? (path ?? '').replace(/[^a-z0-9]/gi, '-').replace(/^-+|-+$/g, '') +} + // Класс PageManager export class PageManager { locales: Locale[] @@ -48,7 +52,6 @@ export class PageManager { pages.forEach((page) => { if (!page.name) { console.warn(`[nuxt-i18n-next] Page name is missing for the file: ${page.file}`) - return } const customRoute = this.globalLocaleRoutes[page.name ?? ''] ?? null @@ -79,7 +82,7 @@ export class PageManager { const localizedPaths: { [key: string]: { [locale: string]: string } } = {} pages.forEach((page) => { - const pageName = page.name ?? '' + const pageName = buildRouteNameFromRoute(page.name, page.path) const globalLocalePath = this.globalLocaleRoutes[pageName] if (!globalLocalePath) { @@ -165,7 +168,7 @@ export class PageManager { const currentChildren = page.children ? [...page.children] : [] if (originalChildren.length) { - const newName = normalizePath(path.join('/', page.name ?? '')) + const newName = normalizePath(path.join('/', buildRouteNameFromRoute(page.name, page.path))) const localizedChildren = this.mergeChildren(originalChildren, newName, [this.defaultLocale.code]) // Мапа для поиска детей по имени @@ -254,7 +257,7 @@ export class PageManager { customRegex?: string | RegExp, ): NuxtPage { const routePath = this.buildRoutePath(localeCodes, page.path, customPath, isCustom, customRegex) - const routeName = buildRouteName(page.name ?? '', localeCodes[0], isCustom) + const routeName = buildRouteName(buildRouteNameFromRoute(page.name, page.path), localeCodes[0], isCustom) return { ...page, @@ -274,7 +277,7 @@ export class PageManager { addLocalePrefix: boolean, ): NuxtPage { const finalPath = this.buildLocalizedRoutePath(routePath, locale, customLocalePaths, addLocalePrefix) - const routeName = this.buildLocalizedRouteName(route.name ?? '', locale, modifyName) + const routeName = this.buildLocalizedRouteName(buildRouteNameFromRoute(route.name, route.path), locale, modifyName) return { ...route, diff --git a/src/runtime/plugins/01.plugin.ts b/src/runtime/plugins/01.plugin.ts index 9eedeb3..02a377c 100644 --- a/src/runtime/plugins/01.plugin.ts +++ b/src/runtime/plugins/01.plugin.ts @@ -168,6 +168,20 @@ function getLocalizedRoute( return params } + + if (!routeName || routeName === '') { + const resolved = router.resolve(to) + let url = resolved.path.replace(new RegExp(`^/${currentLocale}/`), '/') + if (currentLocale !== i18nConfig.defaultLocale || i18nConfig.includeDefaultLocaleRoute) { + url = '/' + currentLocale + '' + url + } + return router.resolve({ + path: url, + query: selectRoute.query, + hash: selectRoute.hash, + }) + } + // Check if the localized route exists if (router.hasRoute(`localized-${routeName}-${currentLocale}`)) { const newParams = resolveParams(selectRoute) diff --git a/test/fixtures/redirect/nuxt.config.ts b/test/fixtures/redirect/nuxt.config.ts index c6c212a..4e7deab 100644 --- a/test/fixtures/redirect/nuxt.config.ts +++ b/test/fixtures/redirect/nuxt.config.ts @@ -6,7 +6,7 @@ export default defineNuxtConfig({ ], // eslint-disable-next-line @typescript-eslint/ban-ts-comment - // @ts-expect-error + // @ts-ignore i18n: { locales: [ { code: 'en', iso: 'en_EN' },