Skip to content

Commit fd7383c

Browse files
feat: i18n
1 parent c4de677 commit fd7383c

File tree

1 file changed

+43
-48
lines changed

1 file changed

+43
-48
lines changed

ui/src/locales/index.ts

Lines changed: 43 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,71 +1,66 @@
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';
34

4-
import { createI18n } from 'vue-i18n'
5-
import useStore from '@/stores'
6-
const { user } = useStore()
75
// 导入语言文件
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 }>>;
127

13-
const langModuleMap = new Map<string, Object>()
8+
const langModuleMap = new Map<string, Object>();
149

15-
export const langCode: Array<string> = []
10+
export const langCode: Array<string> = [];
1611

17-
export const localeConfigKey = 'MaxKB-locale'
12+
export const localeConfigKey = 'MaxKB-locale';
1813

1914
// 获取浏览器默认语言环境
20-
const languages = usePreferredLanguages()
15+
const languages = usePreferredLanguages();
2116

2217
// 生成语言模块列表
2318
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+
};
3429

3530
// 导出 Message
3631
const importMessages = computed(() => {
37-
generateLangModuleMap()
32+
generateLangModuleMap();
3833

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+
});
4540

4641
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+
});
5348

5449
export const langList = computed(() => {
55-
if (langModuleMap.size === 0) generateLangModuleMap()
50+
if (langModuleMap.size === 0) generateLangModuleMap();
5651

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+
});
6459

65-
return list
66-
})
60+
return list;
61+
});
6762

6863
// @ts-ignore
69-
export const { t } = i18n.global
64+
export const { t } = i18n.global;
7065

71-
export default i18n
66+
export default i18n;

0 commit comments

Comments
 (0)