Skip to content

Commit

Permalink
fix: inline vue i18n option resolve
Browse files Browse the repository at this point in the history
  • Loading branch information
BobbieGoede authored and kazupon committed Nov 13, 2023
1 parent d1fbb32 commit 3c75cf9
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 24 deletions.
14 changes: 10 additions & 4 deletions src/runtime/plugins/i18n.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,7 @@ import {
extendBaseUrl,
extendPrefixable,
extendSwitchLocalePathIntercepter,
_setLocale,
resolveVueI18nOptions
_setLocale
} from '#build/i18n.utils.mjs'
import {
getBrowserLocale as _getBrowserLocale,
Expand All @@ -42,8 +41,9 @@ import {
detectBrowserLanguage,
DefaultDetectBrowserLanguageFromResult
} from '#build/i18n.internal.mjs'
import defu from 'defu'

import type { Composer, Locale } from 'vue-i18n'
import type { Composer, Locale, I18nOptions } from 'vue-i18n'
import type { LocaleObject, ExtendProperyDescripters, VueI18nRoutingPluginOptions } from 'vue-i18n-routing'
import type { NuxtApp } from '#app'

Expand All @@ -62,7 +62,13 @@ export default defineNuxtPlugin({
const { vueApp: app } = nuxt
const nuxtContext = nuxt as unknown as NuxtApp

const vueI18nOptions = await resolveVueI18nOptions(vueI18nConfigs)
let vueI18nOptions: I18nOptions = { messages: {} }
for (const configFile of vueI18nConfigs) {
const { default: resolver } = await configFile()
const resolved = typeof resolver === 'function' ? await resolver() : resolver

vueI18nOptions = defu(vueI18nOptions, resolved)
}

const useCookie = nuxtI18nOptions.detectBrowserLanguage && nuxtI18nOptions.detectBrowserLanguage.useCookie
const { __normalizedLocales: normalizedLocales } = nuxtI18nInternalOptions
Expand Down
21 changes: 1 addition & 20 deletions src/runtime/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ import {
DefaultDetectBrowserLanguageFromResult
} from '#build/i18n.internal.mjs'
import { joinURL, isEqual } from 'ufo'
import defu from 'defu'

import type {
Route,
Expand All @@ -44,12 +43,7 @@ import type {
} from 'vue-i18n-routing'
import type { NuxtApp } from '#app'
import type { I18n, I18nOptions, Locale, FallbackLocale, LocaleMessages, DefineLocaleMessage } from 'vue-i18n'
import type {
NuxtI18nOptions,
DetectBrowserLanguageOptions,
RootRedirectOptions,
VueI18nConfig
} from '#build/i18n.options.mjs'
import type { NuxtI18nOptions, DetectBrowserLanguageOptions, RootRedirectOptions } from '#build/i18n.options.mjs'
import type { DetectLocaleContext } from '#build/i18n.internal.mjs'
import type { DeepRequired } from 'ts-essentials'

Expand Down Expand Up @@ -537,17 +531,4 @@ export function extendBaseUrl<Context extends NuxtApp = NuxtApp>(
}
}

export const resolveVueI18nOptions = async (configFiles: VueI18nConfig[]) => {
let resolvedOptions: I18nOptions = { messages: {} }

for (const configFile of configFiles) {
const { default: resolver } = await configFile()
const resolved = typeof resolver === 'function' ? await resolver() : resolver

resolvedOptions = defu(resolvedOptions, resolved)
}

return resolvedOptions
}

/* eslint-enable @typescript-eslint/no-explicit-any */

0 comments on commit 3c75cf9

Please sign in to comment.