Skip to content

Commit c79ff41

Browse files
authored
fix: load fallback messages on first access (#3352)
1 parent 16d8e18 commit c79ff41

File tree

2 files changed

+19
-12
lines changed

2 files changed

+19
-12
lines changed

src/runtime/messages.ts

+15-11
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { deepCopy, isFunction, isArray, isObject, isString } from '@intlify/shared'
1+
import { deepCopy, isFunction } from '@intlify/shared'
22
import { createLogger } from 'virtual:nuxt-i18n-logger'
33

44
import type { I18nOptions, Locale, FallbackLocale, LocaleMessages, DefineLocaleMessage } from 'vue-i18n'
@@ -35,19 +35,23 @@ export async function loadVueI18nOptions(
3535
}
3636

3737
export function makeFallbackLocaleCodes(fallback: FallbackLocale, locales: Locale[]): Locale[] {
38+
if (fallback === false) return []
39+
if (Array.isArray(fallback)) return fallback
40+
3841
let fallbackLocales: Locale[] = []
39-
if (isArray(fallback)) {
40-
fallbackLocales = fallback
41-
} else if (isObject(fallback)) {
42-
const targets = [...locales, 'default']
43-
for (const locale of targets) {
44-
if (fallback[locale]) {
45-
fallbackLocales = [...fallbackLocales, ...fallback[locale].filter(Boolean)]
46-
}
42+
if (typeof fallback === 'string') {
43+
if (locales.every(locale => locale !== fallback)) {
44+
fallbackLocales.push(fallback)
4745
}
48-
} else if (isString(fallback) && locales.every(locale => locale !== fallback)) {
49-
fallbackLocales.push(fallback)
46+
return fallbackLocales
5047
}
48+
49+
const targets = [...locales, 'default']
50+
for (const locale of targets) {
51+
if (locale in fallback == false) continue
52+
fallbackLocales = [...fallbackLocales, ...fallback[locale].filter(Boolean)]
53+
}
54+
5155
return fallbackLocales
5256
}
5357

src/runtime/plugins/route-locale-detect.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@ import { unref } from 'vue'
22
import { hasPages } from '#build/i18n.options.mjs'
33
import { addRouteMiddleware, defineNuxtPlugin, defineNuxtRouteMiddleware } from '#imports'
44
import { createLogger } from 'virtual:nuxt-i18n-logger'
5-
import { detectLocale, detectRedirect, loadAndSetLocale, navigate } from '../utils'
5+
import { makeFallbackLocaleCodes } from '../messages'
66
import { createLocaleFromRouteGetter } from '../routing/utils'
7+
import { detectLocale, detectRedirect, loadAndSetLocale, navigate } from '../utils'
78

89
import type { NuxtApp } from '#app'
910
import type { CompatRoute } from '../types'
@@ -22,6 +23,8 @@ export default defineNuxtPlugin({
2223

2324
if (nuxtApp._vueI18n.__firstAccess) {
2425
nuxtApp._vueI18n.__setLocale(detected)
26+
const fallbackLocales = makeFallbackLocaleCodes(unref(nuxtApp._vueI18n.global.fallbackLocale), [detected])
27+
await Promise.all(fallbackLocales.map(x => nuxtApp.$i18n.loadLocaleMessages(x)))
2528
await nuxtApp.$i18n.loadLocaleMessages(detected)
2629
}
2730

0 commit comments

Comments
 (0)