|
1 | | -import { useLocalStorage, usePreferredLanguages } from '@vueuse/core' |
2 | | -import { computed } from 'vue' |
| 1 | +import { useLocalStorage, usePreferredLanguages } from '@vueuse/core'; |
| 2 | +import { computed } from 'vue'; |
| 3 | +import { createI18n } from 'vue-i18n'; |
3 | 4 |
|
4 | | -import { createI18n } from 'vue-i18n' |
5 | | -import useStore from '@/stores' |
6 | | -const { user } = useStore() |
7 | 5 | // 导入语言文件 |
8 | | -const langModules = import.meta.glob('./lang/*/index.ts', { eager: true }) as Record< |
9 | | - string, |
10 | | - () => Promise<{ default: Object }> |
11 | | -> |
| 6 | +const langModules = import.meta.glob('./lang/*/index.ts', { eager: true }) as Record<string, () => Promise<{ default: Object }>>; |
12 | 7 |
|
13 | | -const langModuleMap = new Map<string, Object>() |
| 8 | +const langModuleMap = new Map<string, Object>(); |
14 | 9 |
|
15 | | -export const langCode: Array<string> = [] |
| 10 | +export const langCode: Array<string> = []; |
16 | 11 |
|
17 | | -export const localeConfigKey = 'MaxKB-locale' |
| 12 | +export const localeConfigKey = 'MaxKB-locale'; |
18 | 13 |
|
19 | 14 | // 获取浏览器默认语言环境 |
20 | | -const languages = usePreferredLanguages() |
| 15 | +const languages = usePreferredLanguages(); |
21 | 16 |
|
22 | 17 | // 生成语言模块列表 |
23 | 18 | const generateLangModuleMap = () => { |
24 | | - const fullPaths = Object.keys(langModules) |
25 | | - fullPaths.forEach((fullPath) => { |
26 | | - const k = fullPath.replace('./lang', '') |
27 | | - const startIndex = 1 |
28 | | - const lastIndex = k.lastIndexOf('/') |
29 | | - const code = k.substring(startIndex, lastIndex) |
30 | | - langCode.push(code) |
31 | | - langModuleMap.set(code, langModules[fullPath]) |
32 | | - }) |
33 | | -} |
| 19 | + const fullPaths = Object.keys(langModules); |
| 20 | + fullPaths.forEach((fullPath) => { |
| 21 | + const k = fullPath.replace('./lang', ''); |
| 22 | + const startIndex = 1; |
| 23 | + const lastIndex = k.lastIndexOf('/'); |
| 24 | + const code = k.substring(startIndex, lastIndex); |
| 25 | + langCode.push(code); |
| 26 | + langModuleMap.set(code, langModules[fullPath]); |
| 27 | + }); |
| 28 | +}; |
34 | 29 |
|
35 | 30 | // 导出 Message |
36 | 31 | const importMessages = computed(() => { |
37 | | - generateLangModuleMap() |
| 32 | + generateLangModuleMap(); |
38 | 33 |
|
39 | | - const message: Recordable = {} |
40 | | - langModuleMap.forEach((value: any, key) => { |
41 | | - message[key] = value.default |
42 | | - }) |
43 | | - return message |
44 | | -}) |
| 34 | + const message: Recordable = {}; |
| 35 | + langModuleMap.forEach((value: any, key) => { |
| 36 | + message[key] = value.default; |
| 37 | + }); |
| 38 | + return message; |
| 39 | +}); |
45 | 40 |
|
46 | 41 | export const i18n = createI18n({ |
47 | | - legacy: false, |
48 | | - locale: user.getLanguage() || languages.value[0] || 'zh_CN', |
49 | | - fallbackLocale: 'zh_CN', |
50 | | - messages: importMessages.value, |
51 | | - globalInjection: true |
52 | | -}) |
| 42 | + legacy: false, |
| 43 | + locale: useLocalStorage(localeConfigKey, 'zh_CN').value || languages.value[0] || 'zh_CN', |
| 44 | + fallbackLocale: 'zh_CN', |
| 45 | + messages: importMessages.value, |
| 46 | + globalInjection: true, |
| 47 | +}); |
53 | 48 |
|
54 | 49 | export const langList = computed(() => { |
55 | | - if (langModuleMap.size === 0) generateLangModuleMap() |
| 50 | + if (langModuleMap.size === 0) generateLangModuleMap(); |
56 | 51 |
|
57 | | - const list: any = [] |
58 | | - langModuleMap.forEach((value: any, key) => { |
59 | | - list.push({ |
60 | | - label: value.default.lang, |
61 | | - value: key |
62 | | - }) |
63 | | - }) |
| 52 | + const list:any=[] |
| 53 | + langModuleMap.forEach((value: any, key) => { |
| 54 | + list.push({ |
| 55 | + label: value.default.lang, |
| 56 | + value: key, |
| 57 | + }); |
| 58 | + }); |
64 | 59 |
|
65 | | - return list |
66 | | -}) |
| 60 | + return list; |
| 61 | +}); |
67 | 62 |
|
68 | 63 | // @ts-ignore |
69 | | -export const { t } = i18n.global |
| 64 | +export const { t } = i18n.global; |
70 | 65 |
|
71 | | -export default i18n |
| 66 | +export default i18n; |
0 commit comments