Skip to content

Commit

Permalink
fix: reuse message loading logic
Browse files Browse the repository at this point in the history
  • Loading branch information
BobbieGoede committed Oct 3, 2024
1 parent e87c438 commit a78ae55
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 50 deletions.
55 changes: 15 additions & 40 deletions src/runtime/server/api/merged.get.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<DefineLocaleMessage, Locale>) => {
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
Expand All @@ -64,6 +39,6 @@ export default defineEventHandler(async () => {
return {
messages,
numberFormats,
dateFormats
datetimeFormats
}
})
5 changes: 1 addition & 4 deletions src/runtime/server/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 => {
Expand Down
4 changes: 4 additions & 0 deletions src/runtime/server/utils.ts
Original file line number Diff line number Diff line change
@@ -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() }
9 changes: 3 additions & 6 deletions src/type-generation.ts
Original file line number Diff line number Diff line change
@@ -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'

/**
Expand Down Expand Up @@ -46,11 +47,7 @@ export function enableVueI18nTypeGeneration(
handler: resolver.resolve('./runtime/server/api/merged.get')
})

let res: {
messages: Record<string, string>
numberFormats: Record<string, string>
dateFormats: Record<string, string>
}
let res: Pick<I18nOptions, 'messages' | 'numberFormats' | 'datetimeFormats'>

/**
* We're using a runtime server endpoint to retrieve and merge options,
Expand Down Expand Up @@ -83,7 +80,7 @@ interface GeneratedLocaleMessage {
}
interface GeneratedDateTimeFormat {
${Object.keys(res.dateFormats || {})
${Object.keys(res.datetimeFormats || {})
.map(k => `${k}: DateTimeFormatOptions;`)
.join(`\n `)}
}
Expand Down

0 comments on commit a78ae55

Please sign in to comment.