Skip to content

Commit

Permalink
fix: improved i18n differentDomains support
Browse files Browse the repository at this point in the history
Fixes #172
  • Loading branch information
harlan-zw committed Jul 1, 2024
1 parent f9eea76 commit 292deb0
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 17 deletions.
8 changes: 4 additions & 4 deletions src/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -175,20 +175,20 @@ export default defineNuxtModule<ModuleOptions>({
const alternatives = Object.keys(pageLocales)
.map(l => ({
hreflang: normalisedLocales.find(nl => nl.code === l)?.iso || l,
href: generatePathForI18nPages({ localeCode: l, pageLocales: pageLocales[l], nuxtI18nConfig }),
href: generatePathForI18nPages({ localeCode: l, pageLocales: pageLocales[l], nuxtI18nConfig, normalisedLocales }),
}))
if (alternatives.length && nuxtI18nConfig.defaultLocale && pageLocales[nuxtI18nConfig.defaultLocale])
alternatives.push({ hreflang: 'x-default', href: generatePathForI18nPages({ localeCode: nuxtI18nConfig.defaultLocale, pageLocales: pageLocales[nuxtI18nConfig.defaultLocale], nuxtI18nConfig }) })
alternatives.push({ hreflang: 'x-default', href: generatePathForI18nPages({ normalisedLocales, localeCode: nuxtI18nConfig.defaultLocale, pageLocales: pageLocales[nuxtI18nConfig.defaultLocale], nuxtI18nConfig }) })
i18nPagesSources.urls!.push({
_sitemap: locale.iso || locale.code,
loc: generatePathForI18nPages({ localeCode, pageLocales: pageLocales[localeCode], nuxtI18nConfig }),
loc: generatePathForI18nPages({ normalisedLocales, localeCode, pageLocales: pageLocales[localeCode], nuxtI18nConfig }),
alternatives,
})
// add extra loc with the default locale code prefix on prefix and default strategy
if (nuxtI18nConfig.strategy === 'prefix_and_default' && localeCode === nuxtI18nConfig.defaultLocale) {
i18nPagesSources.urls!.push({
_sitemap: locale.iso || locale.code,
loc: generatePathForI18nPages({ localeCode, pageLocales: pageLocales[localeCode], nuxtI18nConfig, forcedStrategy: 'prefix' }),
loc: generatePathForI18nPages({ normalisedLocales, localeCode, pageLocales: pageLocales[localeCode], nuxtI18nConfig, forcedStrategy: 'prefix' }),
alternatives,
})
}
Expand Down
20 changes: 12 additions & 8 deletions src/util/i18n.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import type { NuxtI18nOptions } from '@nuxtjs/i18n'
import type { Strategies } from 'vue-i18n-routing'
import { joinURL } from 'ufo'
import type { AutoI18nConfig, FilterInput } from '../runtime/types'
import { joinURL, withBase, withHttps } from 'ufo'
import type { AutoI18nConfig, FilterInput, NormalisedLocales } from '../runtime/types'
import { splitForLocales } from '../runtime/utils-pure'

export interface StrategyProps {
localeCode: string
pageLocales: string
nuxtI18nConfig: NuxtI18nOptions
forcedStrategy?: Strategies
normalisedLocales: NormalisedLocales
}

export function splitPathForI18nLocales(path: FilterInput, autoI18n: AutoI18nConfig) {
Expand All @@ -26,15 +27,18 @@ export function splitPathForI18nLocales(path: FilterInput, autoI18n: AutoI18nCon
]
}

export function generatePathForI18nPages({ localeCode, pageLocales, nuxtI18nConfig, forcedStrategy }: StrategyProps): string {
export function generatePathForI18nPages(ctx: StrategyProps): string {
const { localeCode, pageLocales, nuxtI18nConfig, forcedStrategy, normalisedLocales } = ctx
const locale = normalisedLocales.find(l => l.code === localeCode)
let path = pageLocales
switch (forcedStrategy ?? nuxtI18nConfig.strategy) {
case 'prefix_except_default':
case 'prefix_and_default':
return localeCode === nuxtI18nConfig.defaultLocale ? pageLocales : joinURL(localeCode, pageLocales)
path = localeCode === nuxtI18nConfig.defaultLocale ? pageLocales : joinURL(localeCode, pageLocales)
break
case 'prefix':
return joinURL(localeCode, pageLocales)
case 'no_prefix':
default:
return pageLocales
path = joinURL(localeCode, pageLocales)
break
}
return locale?.domain ? withHttps(withBase(path, locale.domain)) : path
}
15 changes: 10 additions & 5 deletions src/util/nuxtSitemap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import { useNuxt } from '@nuxt/kit'
import { extname } from 'pathe'
import { defu } from 'defu'
import type { ConsolaInstance } from 'consola'
import type { SitemapDefinition, SitemapUrl, SitemapUrlInput } from '../runtime/types'
import { withBase, withHttps } from 'ufo'
import type { NormalisedLocales, SitemapDefinition, SitemapUrl, SitemapUrlInput } from '../runtime/types'
import { createPathFilter } from '../runtime/utils-pure'
import type { CreateFilterOptions } from '../runtime/utils-pure'

Expand All @@ -27,7 +28,7 @@ export async function resolveUrls(urls: Required<SitemapDefinition>['urls'], ctx
}

export interface NuxtPagesToSitemapEntriesOptions {
normalisedLocales: { code: string, iso?: string }[]
normalisedLocales: NormalisedLocales
routesNameSeparator?: string
autoLastmod: boolean
defaultLocale: string
Expand Down Expand Up @@ -148,20 +149,24 @@ export function convertNuxtPagesToSitemapEntries(pages: NuxtPage[], config: Nuxt
}
return entries.map((entry) => {
const alternatives = entries.map((entry) => {
const locale = config.normalisedLocales.find(l => l.code === entry.locale)
// check if the locale has a iso code
const hreflang = config.normalisedLocales.find(l => l.code === entry.locale)?.iso || entry.locale
const hreflang = locale?.iso || entry.locale
if (!pathFilter(entry.loc))
return false
const href = locale?.domain ? withHttps(withBase(entry.loc, locale?.domain)) : entry.loc
return {
hreflang,
href: entry.loc,
href,
}
}).filter(Boolean)
const xDefault = entries.find(a => a.locale === config.defaultLocale)
if (xDefault && alternatives.length && pathFilter(xDefault.loc)) {
const locale = config.normalisedLocales.find(l => l.code === xDefault.locale)
const href = locale?.domain ? withHttps(withBase(xDefault.loc, locale?.domain)) : xDefault.loc
alternatives.push({
hreflang: 'x-default',
href: xDefault.loc,
href,
})
}
const e = { ...entry }
Expand Down

0 comments on commit 292deb0

Please sign in to comment.