|
@@ -1,6 +1,5 @@
|
|
|
import { defineStore } from 'pinia'
|
|
import { defineStore } from 'pinia'
|
|
|
import { ref, computed } from 'vue'
|
|
import { ref, computed } from 'vue'
|
|
|
-import { useStorage } from '@vueuse/core'
|
|
|
|
|
// 导入 Element Plus 中英文语言包
|
|
// 导入 Element Plus 中英文语言包
|
|
|
import zhCn from 'element-plus/es/locale/lang/zh-cn'
|
|
import zhCn from 'element-plus/es/locale/lang/zh-cn'
|
|
|
import en from 'element-plus/es/locale/lang/en'
|
|
import en from 'element-plus/es/locale/lang/en'
|
|
@@ -9,13 +8,29 @@ import i18n from '@/locales'
|
|
|
// 默认语言从环境变量获取
|
|
// 默认语言从环境变量获取
|
|
|
const DEFAULT_LANG = import.meta.env.VITE_APP_LANG || 'zh-cn'
|
|
const DEFAULT_LANG = import.meta.env.VITE_APP_LANG || 'zh-cn'
|
|
|
|
|
|
|
|
|
|
+// 从 localStorage 获取初始语言
|
|
|
|
|
+function getInitialLanguage(): string {
|
|
|
|
|
+ try {
|
|
|
|
|
+ const stored = localStorage.getItem('language')
|
|
|
|
|
+ if (stored) {
|
|
|
|
|
+ const parsed = JSON.parse(stored)
|
|
|
|
|
+ if (parsed === 'zh-cn' || parsed === 'en') {
|
|
|
|
|
+ return parsed
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ } catch {
|
|
|
|
|
+ // ignore
|
|
|
|
|
+ }
|
|
|
|
|
+ return DEFAULT_LANG
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
export const useAppStore = defineStore('app', () => {
|
|
export const useAppStore = defineStore('app', () => {
|
|
|
// 侧边栏状态
|
|
// 侧边栏状态
|
|
|
const sidebarOpened = ref(true)
|
|
const sidebarOpened = ref(true)
|
|
|
const loading = ref(false)
|
|
const loading = ref(false)
|
|
|
|
|
|
|
|
- // 语言:开发环境使用 ref(每次启动用环境变量),生产环境使用 useStorage(持久化用户选择)
|
|
|
|
|
- const language = import.meta.env.DEV ? ref(DEFAULT_LANG) : useStorage('language', DEFAULT_LANG)
|
|
|
|
|
|
|
+ // 语言:使用简单的 ref,手动处理持久化
|
|
|
|
|
+ const language = ref(getInitialLanguage())
|
|
|
const size = ref<'default' | 'small' | 'large'>('default')
|
|
const size = ref<'default' | 'small' | 'large'>('default')
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
@@ -50,6 +65,12 @@ export const useAppStore = defineStore('app', () => {
|
|
|
function changeLanguage(val: string) {
|
|
function changeLanguage(val: string) {
|
|
|
language.value = val
|
|
language.value = val
|
|
|
i18n.global.locale.value = val
|
|
i18n.global.locale.value = val
|
|
|
|
|
+ // 手动保存到 localStorage
|
|
|
|
|
+ try {
|
|
|
|
|
+ localStorage.setItem('language', JSON.stringify(val))
|
|
|
|
|
+ } catch {
|
|
|
|
|
+ // ignore
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
/**
|