提交 | 用户 | 时间
|
314507
|
1 |
import { defineStore } from 'pinia' |
H |
2 |
import { store } from '../index' |
|
3 |
import zhCn from 'element-plus/es/locale/lang/zh-cn' |
|
4 |
import en from 'element-plus/es/locale/lang/en' |
|
5 |
import { CACHE_KEY, useCache } from '@/hooks/web/useCache' |
|
6 |
import { LocaleDropdownType } from '@/types/localeDropdown' |
|
7 |
|
|
8 |
const { wsCache } = useCache() |
|
9 |
|
|
10 |
const elLocaleMap = { |
|
11 |
'zh-CN': zhCn, |
|
12 |
en: en |
|
13 |
} |
|
14 |
interface LocaleState { |
|
15 |
currentLocale: LocaleDropdownType |
|
16 |
localeMap: LocaleDropdownType[] |
|
17 |
} |
|
18 |
|
|
19 |
export const useLocaleStore = defineStore('locales', { |
|
20 |
state: (): LocaleState => { |
|
21 |
return { |
|
22 |
currentLocale: { |
|
23 |
lang: wsCache.get(CACHE_KEY.LANG) || 'zh-CN', |
|
24 |
elLocale: elLocaleMap[wsCache.get(CACHE_KEY.LANG) || 'zh-CN'] |
|
25 |
}, |
|
26 |
// 多语言 |
|
27 |
localeMap: [ |
|
28 |
{ |
|
29 |
lang: 'zh-CN', |
|
30 |
name: '简体中文' |
|
31 |
}, |
|
32 |
{ |
|
33 |
lang: 'en', |
|
34 |
name: 'English' |
|
35 |
} |
|
36 |
] |
|
37 |
} |
|
38 |
}, |
|
39 |
getters: { |
|
40 |
getCurrentLocale(): LocaleDropdownType { |
|
41 |
return this.currentLocale |
|
42 |
}, |
|
43 |
getLocaleMap(): LocaleDropdownType[] { |
|
44 |
return this.localeMap |
|
45 |
} |
|
46 |
}, |
|
47 |
actions: { |
|
48 |
setCurrentLocale(localeMap: LocaleDropdownType) { |
|
49 |
// this.locale = Object.assign(this.locale, localeMap) |
|
50 |
this.currentLocale.lang = localeMap?.lang |
|
51 |
this.currentLocale.elLocale = elLocaleMap[localeMap?.lang] |
|
52 |
wsCache.set(CACHE_KEY.LANG, localeMap?.lang) |
|
53 |
} |
|
54 |
} |
|
55 |
}) |
|
56 |
|
|
57 |
export const useLocaleStoreWithOut = () => { |
|
58 |
return useLocaleStore(store) |
|
59 |
} |