Skip to content

Commit

Permalink
fix(i18n): support excluded locales (#331)
Browse files Browse the repository at this point in the history
Co-authored-by: ipesic <[email protected]>
  • Loading branch information
Xenossolitarius and ipesic authored Aug 12, 2024
1 parent f919726 commit f9ba056
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 3 deletions.
6 changes: 4 additions & 2 deletions src/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,15 @@ import type {
SitemapSourceInput,
SitemapSourceResolved,
ModuleOptions as _ModuleOptions, FilterInput,
NormalisedLocale,
} from './runtime/types'
import { convertNuxtPagesToSitemapEntries, generateExtraRoutesFromNuxtConfig, resolveUrls } from './util/nuxtSitemap'
import { createNitroPromise, createPagesPromise, extendTypes, getNuxtModuleOptions, resolveNitroPreset } from './util/kit'
import { includesSitemapRoot, isNuxtGenerate, setupPrerenderHandler } from './prerender'
import { mergeOnKey } from './runtime/utils-pure'
import { setupDevToolsUI } from './devtools'
import { normaliseDate } from './runtime/nitro/sitemap/urlset/normalise'
import { generatePathForI18nPages, splitPathForI18nLocales } from './util/i18n'
import { generatePathForI18nPages, getExcludedLocalesFromI18nConfig, splitPathForI18nLocales } from './util/i18n'
import { normalizeFilters } from './util/filter'

export interface ModuleOptions extends _ModuleOptions {}
Expand Down Expand Up @@ -162,7 +163,8 @@ export default defineNuxtModule<ModuleOptions>({
if (!await hasNuxtModuleCompatibility('@nuxtjs/i18n', '>=8'))
logger.warn(`You are using @nuxtjs/i18n v${i18nVersion}. For the best compatibility, please upgrade to @nuxtjs/i18n v8.0.0 or higher.`)
nuxtI18nConfig = (await getNuxtModuleOptions('@nuxtjs/i18n') || {}) as NuxtI18nOptions
normalisedLocales = mergeOnKey((nuxtI18nConfig.locales || []).map((locale: any) => typeof locale === 'string' ? { code: locale } : locale), 'code')
const excludedLocales = getExcludedLocalesFromI18nConfig(nuxtI18nConfig)
normalisedLocales = mergeOnKey((nuxtI18nConfig.locales || []).map((locale: any) => typeof locale === 'string' ? { code: locale } : locale), 'code').filter((locale: NormalisedLocale) => !excludedLocales.includes(locale.code))
usingI18nPages = !!Object.keys(nuxtI18nConfig.pages || {}).length
if (usingI18nPages && !hasDisabledAutoI18n) {
const i18nPagesSources: SitemapSourceBase = {
Expand Down
4 changes: 3 additions & 1 deletion src/runtime/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,9 @@ export type AppSourceContext = 'nuxt:pages' | 'nuxt:prerender' | 'nuxt:route-rul

export type SitemapSourceInput = string | [string, FetchOptions] | SitemapSourceBase | SitemapSourceResolved

export type NormalisedLocales = { code: string, iso?: string, domain?: string }[]
export interface NormalisedLocale { code: string, iso?: string, domain?: string }

export type NormalisedLocales = NormalisedLocale[]
export interface AutoI18nConfig {
differentDomains?: boolean
locales: NormalisedLocales
Expand Down
7 changes: 7 additions & 0 deletions src/util/i18n.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,13 @@ export function splitPathForI18nLocales(path: FilterInput, autoI18n: AutoI18nCon
]
}

export function getExcludedLocalesFromI18nConfig(nuxtI18nConfig: NuxtI18nOptions) {
const onlyLocales = nuxtI18nConfig?.bundle?.onlyLocales
if (!onlyLocales) return []
const excludedLocales = typeof onlyLocales === 'string' ? [onlyLocales] : onlyLocales
return excludedLocales
}

export function generatePathForI18nPages(ctx: StrategyProps): string {
const { localeCode, pageLocales, nuxtI18nConfig, forcedStrategy, normalisedLocales } = ctx
const locale = normalisedLocales.find(l => l.code === localeCode)
Expand Down

0 comments on commit f9ba056

Please sign in to comment.