You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
55 lines
1.3 KiB
55 lines
1.3 KiB
2 years ago
|
import type { LocaleSetting, LocaleType } from '@/types/config'
|
||
|
|
||
|
import { defineStore } from 'pinia'
|
||
|
import { store } from '@/store'
|
||
|
|
||
|
import { LOCALE_KEY } from '@/enums/cacheEnum'
|
||
|
import { createLocalStorage } from '@/utils/cache'
|
||
|
import { localeSetting } from '@/settings/localeSetting'
|
||
|
|
||
|
const ls = createLocalStorage()
|
||
|
|
||
|
const lsLocaleSetting = (ls.get(LOCALE_KEY) || localeSetting) as LocaleSetting
|
||
|
|
||
|
interface LocaleState {
|
||
|
localInfo: LocaleSetting
|
||
|
}
|
||
|
|
||
|
export const useLocaleStore = defineStore('app-locale', {
|
||
|
state: (): LocaleState => ({
|
||
|
localInfo: lsLocaleSetting
|
||
|
}),
|
||
|
getters: {
|
||
|
getShowPicker(): boolean {
|
||
|
return !!this.localInfo?.showPicker
|
||
|
},
|
||
|
getLocale(): LocaleType {
|
||
|
return this.localInfo?.locale ?? 'zh_CN'
|
||
|
}
|
||
|
},
|
||
|
actions: {
|
||
|
/**
|
||
|
* Set up multilingual information and cache
|
||
|
* @param info multilingual info
|
||
|
*/
|
||
|
setLocaleInfo(info: Partial<LocaleSetting>) {
|
||
|
this.localInfo = { ...this.localInfo, ...info }
|
||
|
ls.set(LOCALE_KEY, this.localInfo)
|
||
|
},
|
||
|
/**
|
||
|
* Initialize multilingual information and load the existing configuration from the local cache
|
||
|
*/
|
||
|
initLocale() {
|
||
|
this.setLocaleInfo({
|
||
|
...localeSetting,
|
||
|
...this.localInfo
|
||
|
})
|
||
|
}
|
||
|
}
|
||
|
})
|
||
|
|
||
|
// Need to be used outside the setup
|
||
|
export function useLocaleStoreWithOut() {
|
||
|
return useLocaleStore(store)
|
||
|
}
|