From f9ba0562a7b624c4b6235c5c31b1d19501fa4017 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ivan=20Pe=C5=A1i=C4=87?= Date: Mon, 12 Aug 2024 15:44:09 +0200 Subject: [PATCH] fix(i18n): support excluded locales (#331) Co-authored-by: ipesic --- src/module.ts | 6 ++++-- src/runtime/types.ts | 4 +++- src/util/i18n.ts | 7 +++++++ 3 files changed, 14 insertions(+), 3 deletions(-) diff --git a/src/module.ts b/src/module.ts index af52125f..557dea8b 100644 --- a/src/module.ts +++ b/src/module.ts @@ -28,6 +28,7 @@ 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' @@ -35,7 +36,7 @@ import { includesSitemapRoot, isNuxtGenerate, setupPrerenderHandler } from './pr 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 {} @@ -162,7 +163,8 @@ export default defineNuxtModule({ 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 = { diff --git a/src/runtime/types.ts b/src/runtime/types.ts index 9c3d8b41..cd060e35 100644 --- a/src/runtime/types.ts +++ b/src/runtime/types.ts @@ -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 diff --git a/src/util/i18n.ts b/src/util/i18n.ts index 464bbbe2..b87cad0f 100644 --- a/src/util/i18n.ts +++ b/src/util/i18n.ts @@ -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)