feat: i18n
This commit is contained in:
parent
c4de677c86
commit
fd7383c519
@ -1,71 +1,66 @@
|
|||||||
import { useLocalStorage, usePreferredLanguages } from '@vueuse/core'
|
import { useLocalStorage, usePreferredLanguages } from '@vueuse/core';
|
||||||
import { computed } from 'vue'
|
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<
|
const langModules = import.meta.glob('./lang/*/index.ts', { eager: true }) as Record<string, () => Promise<{ default: Object }>>;
|
||||||
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 generateLangModuleMap = () => {
|
||||||
const fullPaths = Object.keys(langModules)
|
const fullPaths = Object.keys(langModules);
|
||||||
fullPaths.forEach((fullPath) => {
|
fullPaths.forEach((fullPath) => {
|
||||||
const k = fullPath.replace('./lang', '')
|
const k = fullPath.replace('./lang', '');
|
||||||
const startIndex = 1
|
const startIndex = 1;
|
||||||
const lastIndex = k.lastIndexOf('/')
|
const lastIndex = k.lastIndexOf('/');
|
||||||
const code = k.substring(startIndex, lastIndex)
|
const code = k.substring(startIndex, lastIndex);
|
||||||
langCode.push(code)
|
langCode.push(code);
|
||||||
langModuleMap.set(code, langModules[fullPath])
|
langModuleMap.set(code, langModules[fullPath]);
|
||||||
})
|
});
|
||||||
}
|
};
|
||||||
|
|
||||||
// 导出 Message
|
// 导出 Message
|
||||||
const importMessages = computed(() => {
|
const importMessages = computed(() => {
|
||||||
generateLangModuleMap()
|
generateLangModuleMap();
|
||||||
|
|
||||||
const message: Recordable = {}
|
const message: Recordable = {};
|
||||||
langModuleMap.forEach((value: any, key) => {
|
langModuleMap.forEach((value: any, key) => {
|
||||||
message[key] = value.default
|
message[key] = value.default;
|
||||||
})
|
});
|
||||||
return message
|
return message;
|
||||||
})
|
});
|
||||||
|
|
||||||
export const i18n = createI18n({
|
export const i18n = createI18n({
|
||||||
legacy: false,
|
legacy: false,
|
||||||
locale: user.getLanguage() || languages.value[0] || 'zh_CN',
|
locale: useLocalStorage(localeConfigKey, 'zh_CN').value || languages.value[0] || 'zh_CN',
|
||||||
fallbackLocale: 'zh_CN',
|
fallbackLocale: 'zh_CN',
|
||||||
messages: importMessages.value,
|
messages: importMessages.value,
|
||||||
globalInjection: true
|
globalInjection: true,
|
||||||
})
|
});
|
||||||
|
|
||||||
export const langList = computed(() => {
|
export const langList = computed(() => {
|
||||||
if (langModuleMap.size === 0) generateLangModuleMap()
|
if (langModuleMap.size === 0) generateLangModuleMap();
|
||||||
|
|
||||||
const list:any=[]
|
const list:any=[]
|
||||||
langModuleMap.forEach((value: any, key) => {
|
langModuleMap.forEach((value: any, key) => {
|
||||||
list.push({
|
list.push({
|
||||||
label: value.default.lang,
|
label: value.default.lang,
|
||||||
value: key
|
value: key,
|
||||||
})
|
});
|
||||||
})
|
});
|
||||||
|
|
||||||
return list
|
return list;
|
||||||
})
|
});
|
||||||
|
|
||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
export const { t } = i18n.global
|
export const { t } = i18n.global;
|
||||||
|
|
||||||
export default i18n
|
export default i18n;
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user