All files / src/stores preference.ts

28.57% Statements 2/7
100% Branches 0/0
50% Functions 1/2
28.57% Lines 2/7

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23          1x 4x                                
import { defineStore } from 'pinia'
import { i18n, syncDocumentLang, LOCALE_STORAGE_KEY, type AppLocale } from '@/locales'
 
export type Locale = AppLocale
 
export const usePreferenceStore = defineStore('preference', {
  state: () => ({
    locale: String(i18n.global.locale.value) as Locale
  }),
  actions: {
    setLocale(locale: Locale) {
      this.locale = locale
      i18n.global.locale.value = locale
      try {
        localStorage.setItem(LOCALE_STORAGE_KEY, locale)
      } catch {
        // 存储受限时仅本次会话生效,不阻断切换
      }
      syncDocumentLang(locale)
    }
  }
})