From 417ffa83de84ca2de058a498479ee83416255684 Mon Sep 17 00:00:00 2001 From: Hccake Date: Tue, 30 Jan 2024 14:32:49 +0800 Subject: [PATCH] =?UTF-8?q?:bug:=20(i18n)=20=E4=BF=AE=E5=A4=8D=E5=BC=80?= =?UTF-8?q?=E5=90=AF=E5=9B=BD=E9=99=85=E5=8C=96=E6=97=B6=E7=9A=84=E5=8A=A0?= =?UTF-8?q?=E8=BD=BD=E5=BC=82=E5=B8=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/hooks/i18n.ts | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/src/hooks/i18n.ts b/src/hooks/i18n.ts index 06e44f3..88aea6c 100644 --- a/src/hooks/i18n.ts +++ b/src/hooks/i18n.ts @@ -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 } @@ -18,6 +20,16 @@ export const useAdminI18n = () => { i18nText, rawI18nText }, - enableI18n ? useI18n() : ({} as ReturnType) + 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 + } +}