35 lines
656 B
JavaScript
35 lines
656 B
JavaScript
import { defineStore } from 'pinia'
|
|
import i18n from '@/i18n'
|
|
|
|
export const useI18nStore = defineStore(
|
|
'i18n',
|
|
{
|
|
state: () => ({
|
|
currentLocale: 'zh-CN',
|
|
availableLocales: [
|
|
{ label: '简体中文', value: 'zh-CN' },
|
|
{ label: 'English', value: 'en-US' }
|
|
]
|
|
}),
|
|
|
|
getters: {
|
|
localeLabel: (state) => {
|
|
const locale = state.availableLocales.find((item) => item.value === state.currentLocale)
|
|
return locale ? locale.label : ''
|
|
}
|
|
},
|
|
|
|
actions: {
|
|
setLocale(locale) {
|
|
this.currentLocale = locale
|
|
i18n.global.locale.value = locale
|
|
}
|
|
},
|
|
|
|
persist: {
|
|
key: 'i18n-store',
|
|
pick: ['currentLocale']
|
|
}
|
|
}
|
|
)
|