import { createI18n } from 'vue-i18n'; import type { languageIndexItemValueType, languageIndexType, languageType } from './type'; import { ref } from 'vue'; export * from './type'; const languagedatas = import.meta.glob('@/language/*.json'); const loadIndexFile = async (): Promise => { try { const module = (await languagedatas['/language/index.json']()) as { default: languageIndexType; }; return module.default; } catch (error) { console.error('加载语言索引文件失败:', error); throw error; } }; const loadLanguageFile = async (fileName: string): Promise => { try { const module = (await languagedatas[`/language/${fileName}`]()) as { default: languageType }; return module.default; } catch (error) { console.error(`加载语言文件失败:${fileName}`, error); return {}; } }; export const messages: languageType | any = {}; export const messageData = ref([]); export let i18n: ReturnType< typeof createI18n< { legacy: boolean; locale: string; fallbackLocale: string; messages: any; }, any, any > >; export function t(data: string): string { return i18n.global.t(data); } export async function installI18n() { const data: languageIndexType = await loadIndexFile(); for (const i of data.languages) { messageData.value.push({ title: i.title, value: i.id, icon: i.icon, }); messages[i.id] = await loadLanguageFile(i.file); } console.log(messages); console.log(languagedatas); console.log(messageData); i18n = createI18n({ legacy: false, locale: 'zh-cn', fallbackLocale: 'zh-cn', messages, }); return i18n; }