diff --git a/src/module.ts b/src/module.ts index c4a5f7efd..3332ef961 100644 --- a/src/module.ts +++ b/src/module.ts @@ -213,10 +213,8 @@ export default defineNuxtModule({ nuxtI18nOptions, isServer }), - NUXT_I18N_MODULE_ID, localeCodes, - nuxtI18nOptionsDefault: DEFAULT_OPTIONS, - nuxtI18nInternalOptions: { __normalizedLocales: normalizedLocales }, + normalizedLocales, dev: nuxt.options.dev, isSSG: nuxt.options._generate, parallelPlugin: options.parallelPlugin diff --git a/src/options.d.ts b/src/options.d.ts index ab01b7d87..bf2e0b0ff 100644 --- a/src/options.d.ts +++ b/src/options.d.ts @@ -21,7 +21,7 @@ export const vueI18nConfigs: VueI18nConfig[] export const localeCodes: string[] = [] export const nuxtI18nOptions: DeepRequired> = {} export const nuxtI18nOptionsDefault: NuxtI18nOptionsDefault = {} -export const nuxtI18nInternalOptions: DeepRequired = {} +export const normalizedLocales: LocaleObject[] = [] export const NUXT_I18N_MODULE_ID = '' export const isSSG = false export const parallelPlugin: boolean @@ -30,6 +30,7 @@ export { NuxtI18nOptions, NuxtI18nOptionsDefault, NuxtI18nInternalOptions, + normalizedLocales, DetectBrowserLanguageOptions, RootRedirectOptions } from './types' diff --git a/src/runtime/composables/index.ts b/src/runtime/composables/index.ts index 23da77283..eea0571eb 100644 --- a/src/runtime/composables/index.ts +++ b/src/runtime/composables/index.ts @@ -1,7 +1,7 @@ import { useRoute, useRouter, useRequestHeaders, useCookie, useNuxtApp } from '#imports' import { ref, computed, watch, onUnmounted } from 'vue' import { parseAcceptLanguage } from '../internal' -import { nuxtI18nInternalOptions, nuxtI18nOptionsDefault, localeCodes as _localeCodes } from '#build/i18n.options.mjs' +import { localeCodes, normalizedLocales, nuxtI18nOptions, nuxtI18nOptionsDefault } from '#build/i18n.options.mjs' import { getActiveHead } from 'unhead' import { useI18n } from 'vue-i18n' import { @@ -17,7 +17,6 @@ import { } from 'vue-i18n-routing' import type { Ref } from 'vue' -import type { DetectBrowserLanguageOptions } from '#build/i18n.options.mjs' export * from 'vue-i18n' export * from './shared' @@ -237,7 +236,7 @@ export function useLocaleHead( * * @public */ -export function useBrowserLocale(normalizedLocales = nuxtI18nInternalOptions.__normalizedLocales): string | null { +export function useBrowserLocale(): string | null { const headers = useRequestHeaders(['accept-language']) return ( findBrowserLocale( @@ -259,17 +258,17 @@ export function useBrowserLocale(normalizedLocales = nuxtI18nInternalOptions.__n * * @public */ -export function useCookieLocale( - options: Required> & { - localeCodes: readonly string[] - } = { - useCookie: nuxtI18nOptionsDefault.detectBrowserLanguage.useCookie, - cookieKey: nuxtI18nOptionsDefault.detectBrowserLanguage.cookieKey, - localeCodes: _localeCodes - } -): Ref { +export function useCookieLocale(): Ref { // Support for importing from `#imports` is generated by auto `imports` nuxt module, so `ref` is imported from `vue` const locale: Ref = ref('') + const options = { + useCookie: + (nuxtI18nOptions.detectBrowserLanguage && nuxtI18nOptions.detectBrowserLanguage.useCookie) ?? + nuxtI18nOptionsDefault.detectBrowserLanguage.useCookie, + cookieKey: + (nuxtI18nOptions.detectBrowserLanguage && nuxtI18nOptions.detectBrowserLanguage.cookieKey) || + nuxtI18nOptionsDefault.detectBrowserLanguage.cookieKey + } if (options.useCookie) { let code: string | null = null @@ -282,7 +281,7 @@ export function useCookieLocale( code = (cookie as any)[options.cookieKey] } - if (code && options.localeCodes.includes(code)) { + if (code && localeCodes.includes(code)) { locale.value = code } } diff --git a/src/runtime/internal.ts b/src/runtime/internal.ts index 9471e535a..e41e12dd9 100644 --- a/src/runtime/internal.ts +++ b/src/runtime/internal.ts @@ -12,13 +12,19 @@ import { import { hasProtocol } from 'ufo' import isHTTPS from 'is-https' import { useRequestHeaders, useRequestEvent, useCookie as useNuxtCookie, useRuntimeConfig, useNuxtApp } from '#imports' -import { nuxtI18nOptionsDefault, NUXT_I18N_MODULE_ID, isSSG } from '#build/i18n.options.mjs' +import { + nuxtI18nOptionsDefault, + NUXT_I18N_MODULE_ID, + isSSG, + localeCodes, + nuxtI18nOptions, + normalizedLocales +} from '#build/i18n.options.mjs' import type { NuxtApp } from '#app' -import type { I18nOptions, Locale, VueI18n } from 'vue-i18n' +import type { Locale, VueI18n } from 'vue-i18n' import type { Route, RouteLocationNormalized, RouteLocationNormalizedLoaded, LocaleObject } from 'vue-i18n-routing' -import type { DeepRequired } from 'ts-essentials' -import type { NuxtI18nOptions, NuxtI18nInternalOptions, DetectBrowserLanguageOptions } from '#build/i18n.options.mjs' +import type { DetectBrowserLanguageOptions } from '#build/i18n.options.mjs' export function formatMessage(message: string) { return NUXT_I18N_MODULE_ID + ' ' + message @@ -85,14 +91,13 @@ export function parseAcceptLanguage(input: string): string[] { return input.split(',').map(tag => tag.split(';')[0]) } -// eslint-disable-next-line @typescript-eslint/no-unused-vars -export function getBrowserLocale(options: Required): string | undefined { +export function getBrowserLocale(): string | undefined { let ret: string | undefined if (process.client) { if (navigator.languages) { // get browser language either from navigator if running on client side, or from the headers - ret = findBrowserLocale(options.__normalizedLocales, navigator.languages as string[]) + ret = findBrowserLocale(normalizedLocales, navigator.languages as string[]) __DEBUG__ && console.log('getBrowserLocale (navigator.languages, ret) -', navigator.languages, ret) } } else if (process.server) { @@ -100,7 +105,7 @@ export function getBrowserLocale(options: Required): st __DEBUG__ && console.log('getBrowserLocale accept-language', header) const accept = header['accept-language'] if (accept) { - ret = findBrowserLocale(options.__normalizedLocales, parseAcceptLanguage(accept)) + ret = findBrowserLocale(normalizedLocales, parseAcceptLanguage(accept)) __DEBUG__ && console.log('getBrowserLocale ret', ret) } } @@ -108,13 +113,14 @@ export function getBrowserLocale(options: Required): st return ret } -export function getLocaleCookie({ - useCookie = nuxtI18nOptionsDefault.detectBrowserLanguage.useCookie, - cookieKey = nuxtI18nOptionsDefault.detectBrowserLanguage.cookieKey, - localeCodes = [] -}: Pick & { - localeCodes?: readonly string[] -} = {}): string | undefined { +export function getLocaleCookie(): string | undefined { + const useCookie = + (nuxtI18nOptions.detectBrowserLanguage && nuxtI18nOptions.detectBrowserLanguage.useCookie) ?? + nuxtI18nOptionsDefault.detectBrowserLanguage.useCookie + const cookieKey = + (nuxtI18nOptions.detectBrowserLanguage && nuxtI18nOptions.detectBrowserLanguage.cookieKey) || + nuxtI18nOptionsDefault.detectBrowserLanguage.cookieKey + __DEBUG__ && console.log('getLocaleCookie', { useCookie, cookieKey, localeCodes }) if (!useCookie) { return @@ -129,9 +135,8 @@ export function getLocaleCookie({ } } -export function setLocaleCookie( - locale: string, - { +export function setLocaleCookie(locale: string) { + const { useCookie = nuxtI18nOptionsDefault.detectBrowserLanguage.useCookie, cookieKey = nuxtI18nOptionsDefault.detectBrowserLanguage.cookieKey, cookieDomain = nuxtI18nOptionsDefault.detectBrowserLanguage.cookieDomain, @@ -140,8 +145,7 @@ export function setLocaleCookie( }: Pick< DetectBrowserLanguageOptions, 'useCookie' | 'cookieDomain' | 'cookieKey' | 'cookieSecure' | 'cookieCrossOrigin' - > = {} -) { + > = nuxtI18nOptions.detectBrowserLanguage || {} if (!useCookie) { return } @@ -191,13 +195,10 @@ export const DefaultDetectBrowserLanguageFromResult: DetectBrowserLanguageFromRe from: 'unknown' } -export function detectBrowserLanguage( +export function detectBrowserLanguage( route: string | Route | RouteLocationNormalized | RouteLocationNormalizedLoaded, - nuxtI18nOptions: DeepRequired>, - nuxtI18nInternalOptions: DeepRequired, - vueI18nOptions: I18nOptions, + vueI18nOptionsLocale: Locale | undefined, detectLocaleContext: DetectLocaleContext, - localeCodes: string[] = [], locale: Locale = '' ): DetectBrowserLanguageFromResult { const { strategy } = nuxtI18nOptions @@ -248,13 +249,13 @@ export function detectBrowserLanguage( // get preferred language from cookie if present and enabled if (useCookie) { - matchedLocale = cookieLocale = getLocaleCookie({ ...nuxtI18nOptions.detectBrowserLanguage, localeCodes }) + matchedLocale = cookieLocale = getLocaleCookie() localeFrom = 'cookie' __DEBUG__ && console.log('detectBrowserLanguage: cookieLocale', cookieLocale) } // try to get locale from either navigator or header detection if (!matchedLocale) { - matchedLocale = getBrowserLocale(nuxtI18nInternalOptions) + matchedLocale = getBrowserLocale() localeFrom = 'navigator_or_header' __DEBUG__ && console.log('detectBrowserLanguage: browserLocale', matchedLocale) } @@ -279,7 +280,7 @@ export function detectBrowserLanguage( localeFrom ) - const vueI18nLocale = locale || vueI18nOptions.locale + const vueI18nLocale = locale || vueI18nOptionsLocale __DEBUG__ && console.log('detectBrowserLanguage: vueI18nLocale', vueI18nLocale) // handle cookie option to prevent multiple redirects @@ -364,12 +365,12 @@ export function getLocaleDomain(locales: LocaleObject[]): string { return host } -export function getDomainFromLocale(localeCode: Locale, locales: LocaleObject[]): string | undefined { +export function getDomainFromLocale(localeCode: Locale): string | undefined { const runtimeConfig = useRuntimeConfig() const nuxtApp = useNuxtApp() // lookup the `differentDomain` origin associated with given locale. const config = runtimeConfig.public.i18n as { locales?: Record } - const lang = locales.find(locale => locale.code === localeCode) + const lang = normalizedLocales.find(locale => locale.code === localeCode) const domain = config?.locales?.[localeCode]?.domain ?? lang?.domain if (domain) { diff --git a/src/runtime/plugins/i18n.ts b/src/runtime/plugins/i18n.ts index eeb72ac69..81baae91f 100644 --- a/src/runtime/plugins/i18n.ts +++ b/src/runtime/plugins/i18n.ts @@ -25,10 +25,10 @@ import { localeCodes, vueI18nConfigs, nuxtI18nOptions as _nuxtI18nOptions, - nuxtI18nInternalOptions, isSSG, localeLoaders, - parallelPlugin + parallelPlugin, + normalizedLocales } from '#build/i18n.options.mjs' import { loadVueI18nOptions, loadInitialMessages } from '../messages' import { @@ -51,7 +51,7 @@ import { } from '../internal' import type { Composer, Locale, I18nOptions } from 'vue-i18n' -import type { LocaleObject, ExtendProperyDescripters, VueI18nRoutingPluginOptions } from 'vue-i18n-routing' +import type { ExtendProperyDescripters, VueI18nRoutingPluginOptions } from 'vue-i18n-routing' import type { NuxtApp } from '#app' type GetRouteBaseName = typeof getRouteBaseName @@ -74,31 +74,18 @@ export default defineNuxtPlugin({ const vueI18nOptions: I18nOptions = await loadVueI18nOptions(vueI18nConfigs, useNuxtApp()) - const useCookie = nuxtI18nOptions.detectBrowserLanguage && nuxtI18nOptions.detectBrowserLanguage.useCookie - const { __normalizedLocales: normalizedLocales } = nuxtI18nInternalOptions - const { - defaultLocale, - differentDomains, - skipSettingLocaleOnNavigate, - lazy, - routesNameSeparator, - defaultLocaleRouteNameSuffix, - strategy, - rootRedirect - } = nuxtI18nOptions + const _detectBrowserLanguage = nuxtI18nOptions.detectBrowserLanguage + + const useCookie = _detectBrowserLanguage && _detectBrowserLanguage.useCookie __DEBUG__ && console.log('isSSG', isSSG) __DEBUG__ && console.log('useCookie on setup', useCookie) - __DEBUG__ && console.log('defaultLocale on setup', defaultLocale) + __DEBUG__ && console.log('defaultLocale on setup', nuxtI18nOptions.defaultLocale) - nuxtI18nOptions.baseUrl = extendBaseUrl(nuxtI18nOptions.baseUrl, { - differentDomains, - localeCodeLoader: defaultLocale, - normalizedLocales - }) + nuxtI18nOptions.baseUrl = extendBaseUrl() const getLocaleFromRoute = createLocaleFromRouteGetter( localeCodes, - routesNameSeparator, - defaultLocaleRouteNameSuffix + nuxtI18nOptions.routesNameSeparator, + nuxtI18nOptions.defaultLocaleRouteNameSuffix ) vueI18nOptions.messages = vueI18nOptions.messages || {} @@ -109,8 +96,8 @@ export default defineNuxtPlugin({ registerGlobalOptions(router, { ...nuxtI18nOptions, dynamicRouteParamsKey: 'nuxtI18n', - switchLocalePathIntercepter: extendSwitchLocalePathIntercepter(differentDomains, normalizedLocales), - prefixable: extendPrefixable(differentDomains) + switchLocalePathIntercepter: extendSwitchLocalePathIntercepter(), + prefixable: extendPrefixable() }) const getDefaultLocale = (defaultLocale: string) => defaultLocale || vueI18nOptions.locale || 'en-US' @@ -119,12 +106,13 @@ export default defineNuxtPlugin({ let initialLocale = detectLocale( route, getLocaleFromRoute, - nuxtI18nOptions, - vueI18nOptions, - getDefaultLocale(defaultLocale), - { ssg: isSSG && strategy === 'no_prefix' ? 'ssg_ignore' : 'normal', callType: 'setup', firstAccess: true }, - normalizedLocales, - localeCodes + vueI18nOptions.locale, + getDefaultLocale(nuxtI18nOptions.defaultLocale), + { + ssg: isSSG && nuxtI18nOptions.strategy === 'no_prefix' ? 'ssg_ignore' : 'normal', + callType: 'setup', + firstAccess: true + } ) __DEBUG__ && console.log('first detect initial locale', initialLocale) @@ -146,10 +134,7 @@ export default defineNuxtPlugin({ __DEBUG__ && console.log('final initial locale:', initialLocale) // create i18n instance - const i18n = createI18n({ - ...vueI18nOptions, - locale: initialLocale - }) + const i18n = createI18n({ ...vueI18nOptions, locale: initialLocale }) let notInitialSetup = true const isInitialLocaleSetup = (locale: Locale) => initialLocale !== locale && notInitialSetup @@ -161,7 +146,7 @@ export default defineNuxtPlugin({ * NOTE: * avoid hydration mismatch for SSG mode */ - if (isSSGModeInitialSetup() && strategy === 'no_prefix' && process.client) { + if (isSSGModeInitialSetup() && nuxtI18nOptions.strategy === 'no_prefix' && process.client) { nuxt.hook('app:mounted', async () => { __DEBUG__ && console.log('hook app:mounted') const { @@ -172,11 +157,8 @@ export default defineNuxtPlugin({ } = nuxtI18nOptions.detectBrowserLanguage ? detectBrowserLanguage( route, - nuxtI18nOptions, - nuxtI18nInternalOptions, - vueI18nOptions, + vueI18nOptions.locale, { ssg: 'ssg_setup', callType: 'setup', firstAccess: true }, - localeCodes, initialLocale ) : DefaultDetectBrowserLanguageFromResult @@ -201,23 +183,13 @@ export default defineNuxtPlugin({ context: nuxtContext, hooks: { onExtendComposer(composer: Composer) { - composer.strategy = strategy - composer.localeProperties = computed(() => { - return ( - normalizedLocales.find((l: LocaleObject) => l.code === composer.locale.value) || { - code: composer.locale.value - } - ) - }) + composer.strategy = nuxtI18nOptions.strategy + composer.localeProperties = computed( + () => normalizedLocales.find(l => l.code === composer.locale.value) || { code: composer.locale.value } + ) composer.setLocale = async (locale: string) => { const localeSetup = isInitialLocaleSetup(locale) - const [modified] = await loadAndSetLocale(locale, i18n, { - useCookie, - differentDomains, - initial: localeSetup, - skipSettingLocaleOnNavigate, - lazy - }) + const [modified] = await loadAndSetLocale(locale, i18n, localeSetup) if (modified && localeSetup) { notInitialSetup = false @@ -226,8 +198,7 @@ export default defineNuxtPlugin({ const redirectPath = detectRedirect({ route: { to: route }, targetLocale: locale, - routeLocaleGetter: getLocaleFromRoute, - nuxtI18nOptions + routeLocaleGetter: getLocaleFromRoute }) __DEBUG__ && console.log('redirectPath on setLocale', redirectPath) @@ -238,20 +209,14 @@ export default defineNuxtPlugin({ locale, route }, - { - differentDomains, - skipSettingLocaleOnNavigate, - rootRedirect, - enableNavigate: true - } + { enableNavigate: true } ) } - composer.differentDomains = differentDomains - composer.defaultLocale = defaultLocale - composer.getBrowserLocale = () => _getBrowserLocale(nuxtI18nInternalOptions) - composer.getLocaleCookie = () => _getLocaleCookie({ ...nuxtI18nOptions.detectBrowserLanguage, localeCodes }) - composer.setLocaleCookie = (locale: string) => - _setLocaleCookie(locale, nuxtI18nOptions.detectBrowserLanguage || undefined) + composer.differentDomains = nuxtI18nOptions.differentDomains + composer.defaultLocale = nuxtI18nOptions.defaultLocale + composer.getBrowserLocale = () => _getBrowserLocale() + composer.getLocaleCookie = () => _getLocaleCookie() + composer.setLocaleCookie = (locale: string) => _setLocaleCookie(locale) composer.onBeforeLanguageSwitch = (oldLocale, newLocale, initialSetup, context) => nuxt.callHook('i18n:beforeLocaleSwitch', { oldLocale, newLocale, initialSetup, context }) @@ -450,31 +415,22 @@ export default defineNuxtPlugin({ const locale = detectLocale( to, getLocaleFromRoute, - nuxtI18nOptions, - vueI18nOptions, + vueI18nOptions.locale, () => { - return getLocale(i18n) || getDefaultLocale(defaultLocale) + return getLocale(i18n) || getDefaultLocale(nuxtI18nOptions.defaultLocale) }, { - ssg: isSSGModeInitialSetup() && strategy === 'no_prefix' ? 'ssg_ignore' : 'normal', + ssg: isSSGModeInitialSetup() && nuxtI18nOptions.strategy === 'no_prefix' ? 'ssg_ignore' : 'normal', callType: 'routing', firstAccess: routeChangeCount === 0 - }, - normalizedLocales, - localeCodes + } ) __DEBUG__ && console.log('detect locale', locale) const localeSetup = isInitialLocaleSetup(locale) __DEBUG__ && console.log('localeSetup', localeSetup) - const [modified] = await loadAndSetLocale(locale, i18n, { - useCookie, - differentDomains, - initial: localeSetup, - skipSettingLocaleOnNavigate, - lazy - }) + const [modified] = await loadAndSetLocale(locale, i18n, localeSetup) if (modified && localeSetup) { notInitialSetup = false @@ -484,26 +440,13 @@ export default defineNuxtPlugin({ route: { to, from }, targetLocale: locale, routeLocaleGetter: nuxtI18nOptions.strategy === 'no_prefix' ? () => locale : getLocaleFromRoute, - nuxtI18nOptions, calledWithRouting: true }) __DEBUG__ && console.log('redirectPath on locale-changing middleware', redirectPath) routeChangeCount++ - return navigate( - { - i18n, - redirectPath, - locale, - route: to - }, - { - differentDomains, - skipSettingLocaleOnNavigate, - rootRedirect - } - ) + return navigate({ i18n, redirectPath, locale, route: to }) }), { global: true } ) diff --git a/src/runtime/utils.ts b/src/runtime/utils.ts index 117feef89..5ea56abfe 100644 --- a/src/runtime/utils.ts +++ b/src/runtime/utils.ts @@ -18,11 +18,12 @@ import { joinURL, isEqual } from 'ufo' import { isString, isFunction, isArray, isObject } from '@intlify/shared' import { navigateTo, useNuxtApp, useRoute, useRuntimeConfig, useState } from '#imports' import { - nuxtI18nInternalOptions, nuxtI18nOptionsDefault, NUXT_I18N_MODULE_ID, isSSG, - localeLoaders + localeLoaders, + normalizedLocales, + nuxtI18nOptions } from '#build/i18n.options.mjs' import { detectBrowserLanguage, @@ -48,10 +49,9 @@ import type { I18nHeadOptions, SeoAttributesOptions } from 'vue-i18n-routing' -import type { I18n, I18nOptions, Locale, FallbackLocale } from 'vue-i18n' +import type { I18n, Locale, FallbackLocale } from 'vue-i18n' import type { NuxtApp } from '#app' -import type { NuxtI18nOptions, DetectBrowserLanguageOptions, RootRedirectOptions } from '#build/i18n.options.mjs' -import type { DeepRequired } from 'ts-essentials' +import type { RootRedirectOptions } from '#build/i18n.options.mjs' import type { DetectLocaleContext } from './internal' import type { HeadSafe } from '@unhead/vue' import { useLocaleRoute, useRouteBaseName, useSwitchLocalePath } from '#i18n' @@ -90,20 +90,18 @@ export async function finalizePendingLocaleChange(i18n: I18n) { return callVueI18nInterfaces(i18n, 'finalizePendingLocaleChange') } -export async function loadAndSetLocale( +export async function loadAndSetLocale( newLocale: string, i18n: I18n, - { - useCookie = nuxtI18nOptionsDefault.detectBrowserLanguage.useCookie, - skipSettingLocaleOnNavigate = nuxtI18nOptionsDefault.skipSettingLocaleOnNavigate, - differentDomains = nuxtI18nOptionsDefault.differentDomains, - initial = false, - lazy = false - }: Pick & - Pick, 'lazy' | 'skipSettingLocaleOnNavigate' | 'differentDomains'> & { - initial?: boolean - } = {} + initial: boolean = false ): Promise<[boolean, string]> { + const _differentDomains = nuxtI18nOptions.differentDomains ?? nuxtI18nOptionsDefault.differentDomains + const skipSettingLocaleOnNavigate = + nuxtI18nOptions.skipSettingLocaleOnNavigate ?? nuxtI18nOptionsDefault.skipSettingLocaleOnNavigate + const lazy = nuxtI18nOptions.lazy ?? nuxtI18nOptionsDefault.lazy + const useCookie = + (nuxtI18nOptions.detectBrowserLanguage && nuxtI18nOptions.detectBrowserLanguage.useCookie) ?? + nuxtI18nOptionsDefault.detectBrowserLanguage.useCookie const nuxtApp = useNuxtApp() let ret = false const oldLocale = getLocale(i18n) @@ -113,7 +111,7 @@ export async function loadAndSetLocale( } // abort if different domains option enabled - if (!initial && differentDomains) { + if (!initial && _differentDomains) { return [ret, oldLocale] } @@ -159,15 +157,12 @@ export async function loadAndSetLocale( type LocaleLoader = () => Locale -export function detectLocale( +export function detectLocale( route: string | Route | RouteLocationNormalized | RouteLocationNormalizedLoaded, routeLocaleGetter: ReturnType, - nuxtI18nOptions: DeepRequired>, - vueI18nOptions: I18nOptions, + vueI18nOptionsLocale: Locale | undefined, initialLocaleLoader: Locale | LocaleLoader, - detectLocaleContext: DetectLocaleContext, - normalizedLocales: LocaleObject[], - localeCodes: string[] = [] + detectLocaleContext: DetectLocaleContext ) { const { strategy, defaultLocale, differentDomains } = nuxtI18nOptions @@ -186,11 +181,8 @@ export function detectLocale( } = nuxtI18nOptions.detectBrowserLanguage ? detectBrowserLanguage( route, - nuxtI18nOptions, - nuxtI18nInternalOptions, - vueI18nOptions, + vueI18nOptionsLocale, detectLocaleContext, - localeCodes, initialLocale ) : DefaultDetectBrowserLanguageFromResult @@ -236,7 +228,7 @@ export function detectLocale( nuxtI18nOptions.detectBrowserLanguage ) if (!finalLocale && nuxtI18nOptions.detectBrowserLanguage && nuxtI18nOptions.detectBrowserLanguage.useCookie) { - finalLocale = getLocaleCookie({ ...nuxtI18nOptions.detectBrowserLanguage, localeCodes }) || '' + finalLocale = getLocaleCookie() || '' } __DEBUG__ && console.log('detectLocale: finalLocale last (finalLocale, defaultLocale) -', finalLocale, defaultLocale) @@ -248,11 +240,10 @@ export function detectLocale( return finalLocale } -export function detectRedirect({ +export function detectRedirect({ route, targetLocale, routeLocaleGetter, - nuxtI18nOptions, calledWithRouting = false }: { route: { @@ -261,7 +252,6 @@ export function detectRedirect({ } targetLocale: Locale routeLocaleGetter: ReturnType - nuxtI18nOptions: DeepRequired> calledWithRouting?: boolean }): string { const nuxtApp = useNuxtApp() @@ -342,19 +332,14 @@ function _navigate(redirectPath: string, status: number) { return navigateTo(redirectPath, { redirectCode: status }) } -export async function navigate( +export async function navigate( args: NavigateArgs, - { - status = 302, - rootRedirect = nuxtI18nOptionsDefault.rootRedirect, - differentDomains = nuxtI18nOptionsDefault.differentDomains, - skipSettingLocaleOnNavigate = nuxtI18nOptionsDefault.skipSettingLocaleOnNavigate, - enableNavigate = false - }: { - status?: number - enableNavigate?: boolean - } & Pick, 'skipSettingLocaleOnNavigate' | 'differentDomains' | 'rootRedirect'> = {} + { status = 302, enableNavigate = false }: { status?: number; enableNavigate?: boolean } = {} ) { + const rootRedirect = nuxtI18nOptions.rootRedirect ?? nuxtI18nOptionsDefault.rootRedirect + const differentDomains = nuxtI18nOptions.differentDomains ?? nuxtI18nOptionsDefault.differentDomains + const skipSettingLocaleOnNavigate = + nuxtI18nOptions.skipSettingLocaleOnNavigate ?? nuxtI18nOptionsDefault.skipSettingLocaleOnNavigate const { i18n, locale, route } = args let { redirectPath } = args @@ -430,20 +415,17 @@ export function injectNuxtHelpers(nuxt: NuxtApp, i18n: I18n) { } // override prefix for route path, support domain -export function extendPrefixable(differentDomains: boolean) { +export function extendPrefixable() { return (opts: PrefixableOptions): boolean => { - return DefaultPrefixable(opts) && !differentDomains + return DefaultPrefixable(opts) && !nuxtI18nOptions.differentDomains } } // override switch locale path intercepter, support domain -export function extendSwitchLocalePathIntercepter( - differentDomains: boolean, - normalizedLocales: LocaleObject[] -): SwitchLocalePathIntercepter { +export function extendSwitchLocalePathIntercepter(): SwitchLocalePathIntercepter { return (path: string, locale: Locale): string => { - if (differentDomains) { - const domain = getDomainFromLocale(locale, normalizedLocales) + if (nuxtI18nOptions.differentDomains) { + const domain = getDomainFromLocale(locale) __DEBUG__ && console.log('extendSwitchLocalePathIntercepter: domain -> ', domain, ' path -> ', path) if (domain) { return joinURL(domain, path) @@ -456,26 +438,22 @@ export function extendSwitchLocalePathIntercepter( } } -export function extendBaseUrl( - baseUrl: string | BaseUrlResolveHandler, - options: Pick>, 'differentDomains'> & { - localeCodeLoader: Locale | LocaleLoader - normalizedLocales: LocaleObject[] - } -): BaseUrlResolveHandler { +export function extendBaseUrl(): BaseUrlResolveHandler { return (): string => { const ctx = useNuxtApp() const runtimeConfig = useRuntimeConfig() + const baseUrl = nuxtI18nOptions.baseUrl + if (isFunction(baseUrl)) { const baseUrlResult = baseUrl(ctx) __DEBUG__ && console.log('baseUrl: using localeLoader function -', baseUrlResult) return baseUrlResult } - const { differentDomains, localeCodeLoader, normalizedLocales } = options + const localeCodeLoader = nuxtI18nOptions.defaultLocale const localeCode = isFunction(localeCodeLoader) ? localeCodeLoader() : localeCodeLoader - if (differentDomains && localeCode) { - const domain = getDomainFromLocale(localeCode, normalizedLocales) + if (nuxtI18nOptions.differentDomains && localeCode) { + const domain = getDomainFromLocale(localeCode) if (domain) { __DEBUG__ && console.log('baseUrl: using differentDomains -', domain) return domain diff --git a/src/template.ts b/src/template.ts index 0fd107548..74e366ae0 100644 --- a/src/template.ts +++ b/src/template.ts @@ -1,13 +1,11 @@ import { generateLoaderOptions } from './gen' -import { DEFAULT_OPTIONS } from './constants' +import { DEFAULT_OPTIONS, NUXT_I18N_MODULE_ID } from './constants' import type { LocaleObject } from 'vue-i18n-routing' export type TemplateNuxtI18nOptions = { - NUXT_I18N_MODULE_ID: string localeCodes: string[] - nuxtI18nOptionsDefault: typeof DEFAULT_OPTIONS - nuxtI18nInternalOptions: { __normalizedLocales: LocaleObject[] } + normalizedLocales: LocaleObject[] dev: boolean isSSG: boolean parallelPlugin: boolean @@ -39,11 +37,11 @@ export const vueI18nConfigs = [ export const nuxtI18nOptions = ${JSON.stringify(options.nuxtI18nOptions, null, 2)} -export const nuxtI18nOptionsDefault = ${JSON.stringify(options.nuxtI18nOptionsDefault, null, 2)} +export const nuxtI18nOptionsDefault = ${JSON.stringify(DEFAULT_OPTIONS, null, 2)} -export const nuxtI18nInternalOptions = ${JSON.stringify(options.nuxtI18nInternalOptions, null, 2)} +export const normalizedLocales = ${JSON.stringify(options.normalizedLocales, null, 2)} -export const NUXT_I18N_MODULE_ID = "${options.NUXT_I18N_MODULE_ID}" +export const NUXT_I18N_MODULE_ID = "${NUXT_I18N_MODULE_ID}" export const parallelPlugin = ${options.parallelPlugin} export const isSSG = ${options.isSSG} diff --git a/src/types.ts b/src/types.ts index 60c110fa9..ac97323fa 100644 --- a/src/types.ts +++ b/src/types.ts @@ -124,8 +124,4 @@ export type NuxtI18nOptions = { | 'trailingSlash' > -export type NuxtI18nInternalOptions = { - __normalizedLocales?: LocaleObject[] -} - export type VueI18nConfig = () => Promise<{ default: I18nOptions | (() => I18nOptions | Promise) }>