From fd7383c519d5941068c2b0d25acf97600ff40210 Mon Sep 17 00:00:00 2001 From: wangdan-fit2cloud Date: Mon, 20 Jan 2025 18:02:04 +0800 Subject: [PATCH] feat: i18n --- ui/src/locales/index.ts | 91 +++++++++++++++++++---------------------- 1 file changed, 43 insertions(+), 48 deletions(-) diff --git a/ui/src/locales/index.ts b/ui/src/locales/index.ts index b263bb0fc..51f94c11c 100644 --- a/ui/src/locales/index.ts +++ b/ui/src/locales/index.ts @@ -1,71 +1,66 @@ -import { useLocalStorage, usePreferredLanguages } from '@vueuse/core' -import { computed } from 'vue' +import { useLocalStorage, usePreferredLanguages } from '@vueuse/core'; +import { computed } from 'vue'; +import { createI18n } from 'vue-i18n'; -import { createI18n } from 'vue-i18n' -import useStore from '@/stores' -const { user } = useStore() // 导入语言文件 -const langModules = import.meta.glob('./lang/*/index.ts', { eager: true }) as Record< - string, - () => Promise<{ default: Object }> -> +const langModules = import.meta.glob('./lang/*/index.ts', { eager: true }) as Record Promise<{ default: Object }>>; -const langModuleMap = new Map() +const langModuleMap = new Map(); -export const langCode: Array = [] +export const langCode: Array = []; -export const localeConfigKey = 'MaxKB-locale' +export const localeConfigKey = 'MaxKB-locale'; // 获取浏览器默认语言环境 -const languages = usePreferredLanguages() +const languages = usePreferredLanguages(); // 生成语言模块列表 const generateLangModuleMap = () => { - const fullPaths = Object.keys(langModules) - fullPaths.forEach((fullPath) => { - const k = fullPath.replace('./lang', '') - const startIndex = 1 - const lastIndex = k.lastIndexOf('/') - const code = k.substring(startIndex, lastIndex) - langCode.push(code) - langModuleMap.set(code, langModules[fullPath]) - }) -} + const fullPaths = Object.keys(langModules); + fullPaths.forEach((fullPath) => { + const k = fullPath.replace('./lang', ''); + const startIndex = 1; + const lastIndex = k.lastIndexOf('/'); + const code = k.substring(startIndex, lastIndex); + langCode.push(code); + langModuleMap.set(code, langModules[fullPath]); + }); +}; // 导出 Message const importMessages = computed(() => { - generateLangModuleMap() + generateLangModuleMap(); - const message: Recordable = {} - langModuleMap.forEach((value: any, key) => { - message[key] = value.default - }) - return message -}) + const message: Recordable = {}; + langModuleMap.forEach((value: any, key) => { + message[key] = value.default; + }); + return message; +}); export const i18n = createI18n({ - legacy: false, - locale: user.getLanguage() || languages.value[0] || 'zh_CN', - fallbackLocale: 'zh_CN', - messages: importMessages.value, - globalInjection: true -}) + legacy: false, + locale: useLocalStorage(localeConfigKey, 'zh_CN').value || languages.value[0] || 'zh_CN', + fallbackLocale: 'zh_CN', + messages: importMessages.value, + globalInjection: true, +}); export const langList = computed(() => { - if (langModuleMap.size === 0) generateLangModuleMap() + if (langModuleMap.size === 0) generateLangModuleMap(); - const list: any = [] - langModuleMap.forEach((value: any, key) => { - list.push({ - label: value.default.lang, - value: key - }) - }) + const list:any=[] + langModuleMap.forEach((value: any, key) => { + list.push({ + label: value.default.lang, + value: key, + }); + }); - return list -}) + return list; +}); // @ts-ignore -export const { t } = i18n.global +export const { t } = i18n.global; -export default i18n +export default i18n;