diff --git a/playground/pages/subpage.vue b/playground/pages/subpage.vue index de925cae..c6b07464 100644 --- a/playground/pages/subpage.vue +++ b/playground/pages/subpage.vue @@ -152,9 +152,11 @@ import { useNuxtApp } from '#imports' const { $getLocale, $switchLocale, $getLocales, $localeRoute, $t, $tc, $defineI18nRoute } = useNuxtApp() +const locale_en = { greeting: 'Hello', farewell: 'Goodbye' } + $defineI18nRoute({ locales: { - en: { greeting: 'Hello', farewell: 'Goodbye' }, + en: locale_en, ru: { greeting: 'Привет', farewell: 'До свидания' }, de: { greeting: 'Hallo', farewell: 'Auf Wiedersehen' }, }, diff --git a/src/page-manager.ts b/src/page-manager.ts index edf307b8..7381d666 100644 --- a/src/page-manager.ts +++ b/src/page-manager.ts @@ -3,7 +3,6 @@ import { readFileSync } from 'node:fs' import type { NuxtPage } from '@nuxt/schema' import type { GlobalLocaleRoutes, Locale } from './types' import { - extractDefineI18nRouteConfig, normalizePath, isLocaleDefault, isPageRedirectOnly, @@ -12,6 +11,7 @@ import { shouldAddLocalePrefix, buildFullPath, removeLeadingSlash, + extractLocaleRoutes, } from './utils' // Класс PageManager @@ -83,11 +83,11 @@ export class PageManager { if (page.file) { const filePath = path.resolve(rootDir, page.file) const fileContent = readFileSync(filePath, 'utf-8') - const i18nRouteConfig = extractDefineI18nRouteConfig(fileContent, filePath) + const localeRoutes = extractLocaleRoutes(fileContent, filePath) - if (i18nRouteConfig?.localeRoutes) { + if (localeRoutes) { const normalizedFullPath = normalizePath(path.join(parentPath, page.path)) - localizedPaths[normalizedFullPath] = i18nRouteConfig.localeRoutes + localizedPaths[normalizedFullPath] = localeRoutes } } } diff --git a/src/utils.ts b/src/utils.ts index 5adef5a6..2b15941b 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -1,54 +1,40 @@ import path from 'node:path' import type { NuxtPage } from '@nuxt/schema' -import type { DefineI18nRouteConfig, Locale } from './types' +import type { Locale, LocaleCode } from './types' -function normalizeLocales(localesArray: string[]): Record { - const localesObject: Record = {} - for (const locale of localesArray) { - localesObject[locale] = {} // Присваиваем пустой объект как значение для каждого ключа - } - return localesObject -} +export function extractLocaleRoutes(content: string, filePath: string): Record | null { + // Ищем вызов defineI18nRoute (с долларом или без) + const defineMatch = content.match(/\$?\bdefineI18nRoute\s*\(\s*\{[\s\S]*?\}\s*\)/) + if (defineMatch) { + // Ищем блок localeRoutes внутри вызова defineI18nRoute + const localeRoutesMatch = defineMatch[0].match(/localeRoutes:\s*(\{[\s\S]*?\})/) -export function extractDefineI18nRouteConfig(content: string, filePath: string): DefineI18nRouteConfig | null { - const match = content.match(/^[ \t]*\$defineI18nRoute\((\{[\s\S]*?\})\)/m) - if (match && match[1]) { - try { - const parsedObject = Function('"use strict";return (' + match[1] + ')')() - - // Добавляем нормализацию массивов в объекты - if (parsedObject.locales && Array.isArray(parsedObject.locales)) { - parsedObject.locales = normalizeLocales(parsedObject.locales) - } + if (localeRoutesMatch && localeRoutesMatch[1]) { + try { + // Парсим найденный блок localeRoutes + const parsedLocaleRoutes = Function('"use strict";return (' + localeRoutesMatch[1] + ')')() - if (validateDefineI18nRouteConfig(parsedObject)) { - return parsedObject + if (typeof parsedLocaleRoutes === 'object' && parsedLocaleRoutes !== null) { + if (validateDefineI18nRouteConfig(parsedLocaleRoutes)) { + return parsedLocaleRoutes + } + } + else { + console.error('localeRoutes found but it is not a valid object in file:', filePath) + } } - else { - console.error('Invalid defineI18nRoute configuration format:', parsedObject, 'in file: ', filePath) + catch (error) { + console.error('Failed to parse localeRoutes:', error, 'in file:', filePath) } } - catch (error) { - console.error('Failed to parse defineI18nRoute configuration:', error, 'in file: ', filePath) - } } return null } -export function validateDefineI18nRouteConfig(obj: DefineI18nRouteConfig): boolean { - if (typeof obj !== 'object' || obj === null) return false - if (obj.locales) { - if (typeof obj.locales !== 'object') return false - for (const localeKey in obj.locales) { - const translations = obj.locales[localeKey] - if (typeof translations !== 'object' || translations === null) return false - } - } - if (obj.localeRoutes) { - if (typeof obj.localeRoutes !== 'object') return false - for (const routeKey in obj.localeRoutes) { - if (typeof obj.localeRoutes[routeKey] !== 'string') return false - } +export function validateDefineI18nRouteConfig(obj: Record>): boolean { + if (typeof obj !== 'object') return false + for (const routeKey in obj.localeRoutes) { + if (typeof obj.localeRoutes[routeKey] !== 'string') return false } return true }