Skip to content

Commit

Permalink
fix: update globalLocaleRoutes logic
Browse files Browse the repository at this point in the history
  • Loading branch information
s00d committed Sep 27, 2024
1 parent 1a846e3 commit d64b101
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 27 deletions.
9 changes: 1 addition & 8 deletions src/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ export default defineNuxtModule<ModuleOptions>({
dateBuild: Date.now(),
baseURL: nuxt.options.app.baseURL,
hashMode: nuxt.options?.router?.options?.hashMode ?? false,
globalLocaleRoutes: options.globalLocaleRoutes ?? {},
globalLocaleRoutes: undefined,
}
nuxt.options.runtimeConfig.i18nConfig = {
rootDir: nuxt.options.rootDir,
Expand Down Expand Up @@ -135,13 +135,6 @@ export default defineNuxtModule<ModuleOptions>({

addImportsDir(resolver.resolve('./runtime/composables'))

// if (options.includeDefaultLocaleRoute) {
// addServerHandler({
// middleware: true,
// handler: resolver.resolve('./runtime/server/middleware/i18n-redirect'),
// })
// }

addServerHandler({
route: '/_locales/:page/:locale/data.json',
handler: resolver.resolve('./runtime/server/middleware/i18n-loader'),
Expand Down
15 changes: 6 additions & 9 deletions src/runtime/plugins/01.plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,21 +144,18 @@ function getLocalizedRoute(
})
}

if (i18nConfig.includeDefaultLocaleRoute) {
const globalLocaleRoutes = i18nConfig.globalLocaleRoutes ?? {}
if (globalLocaleRoutes[routeName] == false) {
const newParams = resolveParams(to)
delete newParams.locale
return router.resolve({ name: routeName, params: newParams })
}
}

// Determine the new route name based on locale and configuration
const newRouteName
= currentLocale !== i18nConfig.defaultLocale || i18nConfig.includeDefaultLocaleRoute
? `localized-${routeName}`
: routeName

if (!router.hasRoute(newRouteName)) {
const newParams = resolveParams(to)
delete newParams.locale
return router.resolve({ name: routeName, params: newParams })
}

const newParams = resolveParams(to)
delete newParams.locale

Expand Down
19 changes: 9 additions & 10 deletions src/runtime/plugins/03.define.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ export default defineNuxtPlugin(async (_nuxtApp) => {
const router = useRouter()

const i18nConfig: ModuleOptionsExtend = config.public.i18nConfig as ModuleOptionsExtend
const globalLocaleRoutes = i18nConfig.globalLocaleRoutes ?? {}

// Функция нормализации, которая объединяет массивы и объекты в единый массив строк
const normalizeLocales = (locales?: string[] | LocalesObject): LocalesObject => {
Expand Down Expand Up @@ -41,18 +40,17 @@ export default defineNuxtPlugin(async (_nuxtApp) => {
.replace(new RegExp(`-${currentLocale}$`), '')

if (!to.params.locale) {
const name = to.name?.toString() ?? ''
if (globalLocaleRoutes[name] === false) {
return
}

if (router.hasRoute(`localized-${to.name?.toString()}-${currentLocale}`)) {
defaultRouteName = `localized-${to.name?.toString()}-${currentLocale}`
}
else {
defaultRouteName = `localized-${to.name?.toString()}`
}

if (!router.hasRoute(defaultRouteName)) {
return
}

const newParams = { ...to.params }
newParams.locale = i18nConfig.defaultLocale!

Expand Down Expand Up @@ -99,16 +97,17 @@ export default defineNuxtPlugin(async (_nuxtApp) => {
delete newParams.locale

if (i18nConfig.includeDefaultLocaleRoute) {
const name = route.name?.toString() ?? ''
if (globalLocaleRoutes[name] === false) {
return
}
if (router.hasRoute(`localized-${defaultRouteName}-${currentLocale}`)) {
defaultRouteName = `localized-${defaultRouteName}-${currentLocale}`
}
else {
defaultRouteName = `localized-${defaultRouteName}`
}

if (!router.hasRoute(defaultRouteName)) {
return
}

newParams.locale = i18nConfig.defaultLocale!
newParams.name = defaultRouteName
}
Expand Down

0 comments on commit d64b101

Please sign in to comment.