Skip to content

Commit

Permalink
🐛 (i18n) 修复开启国际化时的加载异常
Browse files Browse the repository at this point in the history
  • Loading branch information
Hccake committed Jan 30, 2024
1 parent 5b990e2 commit 417ffa8
Showing 1 changed file with 15 additions and 3 deletions.
18 changes: 15 additions & 3 deletions src/hooks/i18n.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import { useI18n } from 'vue-i18n'
import { enableI18n } from '@/config'
import type { VueI18n } from 'vue-i18n'

export const useAdminI18n = () => {
const i18nGlobal = getI18nGlobal()

function rawI18nText(code: string, defaultText?: string) {
if (enableI18n) {
return useI18n().t(code)
return i18nGlobal.t(code)
}
return defaultText ?? code
}
Expand All @@ -18,6 +20,16 @@ export const useAdminI18n = () => {
i18nText,
rawI18nText
},
enableI18n ? useI18n() : ({} as ReturnType<typeof useI18n>)
i18nGlobal
)
}

function getI18nGlobal(): VueI18n {
if (enableI18n) {
const modules = import.meta.glob('@/locales/index.ts', { eager: true })
const module = Object.values(modules)[0] as any
return module.i18n.global as VueI18n
} else {
return {} as VueI18n
}
}

0 comments on commit 417ffa8

Please sign in to comment.