Files
claude-relay-service/web/admin-spa/src/stores/locale.js

48 lines
1.3 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { defineStore } from 'pinia'
import { ref } from 'vue'
import { i18n, SUPPORTED_LOCALES, getSupportedLocalesWithI18n } from '@/i18n'
export const useLocaleStore = defineStore('locale', () => {
const currentLocale = ref(i18n.global.locale.value)
// 切换语言
const setLocale = (locale) => {
if (!SUPPORTED_LOCALES[locale]) {
console.warn(`Unsupported locale: ${locale}`)
return
}
currentLocale.value = locale
i18n.global.locale.value = locale
localStorage.setItem('app-locale', locale)
// 更新HTML lang属性
document.documentElement.setAttribute('lang', locale)
}
// 获取当前语言信息兼容i18n
const getCurrentLocaleInfo = (t = null) => {
if (t) {
const supportedLocales = getSupportedLocalesWithI18n(t)
return supportedLocales[currentLocale.value] || supportedLocales['zh-cn']
}
return SUPPORTED_LOCALES[currentLocale.value] || SUPPORTED_LOCALES['zh-cn']
}
// 获取所有支持的语言兼容i18n
const getSupportedLocales = (t = null) => {
const supportedLocales = t ? getSupportedLocalesWithI18n(t) : SUPPORTED_LOCALES
return Object.entries(supportedLocales).map(([key, value]) => ({
code: key,
...value
}))
}
return {
currentLocale,
setLocale,
getCurrentLocaleInfo,
getSupportedLocales
}
})