1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
| import { defineStore } from 'pinia'
| import { SET_LANGUAGE } from '@/store/types.store'
| import { getStorage, setStorage, getBrowserLang } from '@/utils/index'
|
| const SUPPORTED_LOCALES = ['en', 'CN', 'zh-CN', 'Korean', 'Japanese', 'de', 'fr', 'vi', 'th', 'Italy', 'es', 'pt', 'gr']
|
| function getInitialLang() {
| const cached = getStorage('lang')
| if (cached && SUPPORTED_LOCALES.includes(cached)) return cached
| const browserLang = getBrowserLang()
| return SUPPORTED_LOCALES.includes(browserLang) ? browserLang : 'en'
| }
|
| export const useLanguageStore = defineStore('language', {
| persist: true,
| state: () => ({
| language: getInitialLang()
| }),
| actions: {
| [SET_LANGUAGE](locale) {
| if (SUPPORTED_LOCALES.includes(locale)) {
| this.language = locale
| setStorage('lang', locale)
| }
| }
| },
| })
|
|