mirror of
https://github.com/1Panel-dev/MaxKB.git
synced 2025-12-26 01:33:05 +00:00
feat: i18n
This commit is contained in:
parent
c4de677c86
commit
fd7383c519
|
|
@ -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<string, () => Promise<{ default: Object }>>;
|
||||
|
||||
const langModuleMap = new Map<string, Object>()
|
||||
const langModuleMap = new Map<string, Object>();
|
||||
|
||||
export const langCode: Array<string> = []
|
||||
export const langCode: Array<string> = [];
|
||||
|
||||
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;
|
||||
|
|
|
|||
Loading…
Reference in New Issue