From a78ae55cc20f03a77f68586f20b6cfe3d58e8b1e Mon Sep 17 00:00:00 2001 From: Bobbie Goede Date: Thu, 3 Oct 2024 23:00:35 +0200 Subject: [PATCH] fix: reuse message loading logic --- src/runtime/server/api/merged.get.ts | 55 ++++++++-------------------- src/runtime/server/plugin.ts | 5 +-- src/runtime/server/utils.ts | 4 ++ src/type-generation.ts | 9 ++--- 4 files changed, 23 insertions(+), 50 deletions(-) create mode 100644 src/runtime/server/utils.ts diff --git a/src/runtime/server/api/merged.get.ts b/src/runtime/server/api/merged.get.ts index dff0db959..a0bec440e 100644 --- a/src/runtime/server/api/merged.get.ts +++ b/src/runtime/server/api/merged.get.ts @@ -3,59 +3,34 @@ import { deepCopy } from '@intlify/shared' import { defineEventHandler } from '#imports' import { vueI18nConfigs, localeLoaders } from '#internal/i18n/options.mjs' -import type { I18nOptions } from 'vue-i18n' +import type { I18nOptions, Locale, LocaleMessages } from 'vue-i18n' +import { loadLocale, loadVueI18nOptions } from '../../messages' +import { nuxtMock } from '../utils' +import type { DefineLocaleMessage } from '@intlify/h3' export default defineEventHandler(async () => { const messages = {} - const dateFormats = {} + const datetimeFormats = {} const numberFormats = {} - for (const config of vueI18nConfigs) { - const intermediate = await (await config)() - const res = ('default' in intermediate ? intermediate.default : intermediate)() as I18nOptions | undefined - - if (res == null) continue - - for (const v of Object.values(res.messages ?? [])) { - deepCopy(v, messages) - } - for (const v of Object.values(res.numberFormats ?? [])) { - deepCopy(v, numberFormats) - } - for (const v of Object.values(res.datetimeFormats ?? [])) { - deepCopy(v, dateFormats) - } + const vueI18nConfig = await loadVueI18nOptions(vueI18nConfigs, nuxtMock) + for (const locale in vueI18nConfig.messages) { + deepCopy(vueI18nConfig.messages[locale] || {}, messages) } + deepCopy(vueI18nConfig.numberFormats || {}, numberFormats) + deepCopy(vueI18nConfig.datetimeFormats || {}, datetimeFormats) // @ts-ignore const _defineI18nLocale = globalThis.defineI18nLocale - // @ts-ignore - // eslint-disable-next-line @typescript-eslint/no-unsafe-return globalThis.defineI18nLocale = val => val - // @ts-ignore - // eslint-disable-next-line @typescript-eslint/no-unsafe-return - // globalThis.defineI18nConfig = val => val - for (const code in localeLoaders) { - for (const f of localeLoaders[code]) { - let message - const getter = await f.load().then(r => ('default' in r ? r.default : r)) - if (typeof getter === 'function') { - message = await getter(code) - } else { - message = getter - } - - try { - deepCopy(message, messages) - } catch (err) { - console.log(err) - } + const setter = (_: Locale, message: LocaleMessages) => { + deepCopy(message, messages) } - // we could only check one locale's files (serving as master/template) for speed - // break + + await loadLocale(code, localeLoaders, setter) } // @ts-ignore @@ -64,6 +39,6 @@ export default defineEventHandler(async () => { return { messages, numberFormats, - dateFormats + datetimeFormats } }) diff --git a/src/runtime/server/plugin.ts b/src/runtime/server/plugin.ts index 0e0ad625f..22001c804 100644 --- a/src/runtime/server/plugin.ts +++ b/src/runtime/server/plugin.ts @@ -3,15 +3,12 @@ import { defineI18nMiddleware } from '@intlify/h3' import { localeCodes, vueI18nConfigs, localeLoaders } from '#internal/i18n/options.mjs' import { defineNitroPlugin } from 'nitropack/dist/runtime/plugin' import { localeDetector as _localeDetector } from '#internal/i18n/locale.detector.mjs' +import { nuxtMock } from './utils' import { loadVueI18nOptions, loadInitialMessages, makeFallbackLocaleCodes, loadAndSetLocaleMessages } from '../messages' import type { H3Event } from 'h3' import type { Locale, DefineLocaleMessage } from 'vue-i18n' import type { CoreContext } from '@intlify/h3' -import type { NuxtApp } from 'nuxt/app' - -// eslint-disable-next-line @typescript-eslint/no-unsafe-return -const nuxtMock: { runWithContext: NuxtApp['runWithContext'] } = { runWithContext: async fn => await fn() } // eslint-disable-next-line @typescript-eslint/no-misused-promises export default defineNitroPlugin(async nitro => { diff --git a/src/runtime/server/utils.ts b/src/runtime/server/utils.ts new file mode 100644 index 000000000..e8e905eab --- /dev/null +++ b/src/runtime/server/utils.ts @@ -0,0 +1,4 @@ +import type { NuxtApp } from 'nuxt/app' + +// eslint-disable-next-line @typescript-eslint/no-unsafe-return +export const nuxtMock: { runWithContext: NuxtApp['runWithContext'] } = { runWithContext: async fn => await fn() } diff --git a/src/type-generation.ts b/src/type-generation.ts index d1ec36cab..20dfc47e2 100644 --- a/src/type-generation.ts +++ b/src/type-generation.ts @@ -1,6 +1,7 @@ import { addServerHandler, addTypeTemplate, createResolver, updateTemplates, useNitro } from '@nuxt/kit' import type { Nuxt } from '@nuxt/schema' +import type { I18nOptions } from 'vue-i18n' import type { I18nNuxtContext } from './context' /** @@ -46,11 +47,7 @@ export function enableVueI18nTypeGeneration( handler: resolver.resolve('./runtime/server/api/merged.get') }) - let res: { - messages: Record - numberFormats: Record - dateFormats: Record - } + let res: Pick /** * We're using a runtime server endpoint to retrieve and merge options, @@ -83,7 +80,7 @@ interface GeneratedLocaleMessage { } interface GeneratedDateTimeFormat { - ${Object.keys(res.dateFormats || {}) + ${Object.keys(res.datetimeFormats || {}) .map(k => `${k}: DateTimeFormatOptions;`) .join(`\n `)} }