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) } } }, })