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