lang util

This commit is contained in:
KUN1007 2023-05-26 02:30:13 +08:00
parent fdd57d4880
commit 8ccfa0c23f
2 changed files with 32 additions and 42 deletions

View file

@ -1,5 +1,7 @@
<!-- 设置面板组件展示整个论坛的设置面板 -->
<script setup lang="ts">
// vue
import { ref, watch, onMounted } from 'vue'
//
import { Icon } from '@iconify/vue'
//
@ -9,9 +11,36 @@ import Background from './components/Background.vue'
// store
import { useSettingsPanelStore } from '@/store/modules/settings'
import { storeToRefs } from 'pinia'
//
import selectedLocale from '@/config/storage/lang-storage'
import changeLanguage from '@/config/storage/lang-storage'
// i18n
import { useI18n } from 'vue-i18n'
/*
* 网站的语言设置
*/
const { locale } = useI18n({ useScope: 'global' })
const selectedLocale = ref(locale.value)
// selectedLocaleVue I18nlocale
watch(selectedLocale, (newVal) => {
locale.value = newVal
})
// localStorage
onMounted(() => {
const savedLocale = localStorage.getItem('KUNGalgame-locale')
if (savedLocale) {
selectedLocale.value = savedLocale
}
})
// localStorage
watch(locale, (newVal) => {
localStorage.setItem('KUNGalgame-locale', newVal)
})
const changeLanguage = () => {
locale.value = selectedLocale.value
}
/*
* 设置面板显示切换

View file

@ -1,39 +0,0 @@
/*
*
*/
// 导入 vue 函数
import { ref, watch, onMounted } from 'vue'
// 导入 i18n
import { useI18n } from 'vue-i18n'
const { locale } = useI18n({ useScope: 'global' })
const selectedLocale = ref(locale.value)
// 监听selectedLocale的变化并更新Vue I18n的locale
watch(selectedLocale, (newVal) => {
locale.value = newVal
})
// 在页面加载时从localStorage中读取保存的语言设置
onMounted(() => {
const savedLocale = localStorage.getItem('KUNGalgame-locale')
if (savedLocale) {
selectedLocale.value = savedLocale
}
})
// 监听语言变化并将语言设置保存到localStorage
watch(locale, (newVal) => {
localStorage.setItem('KUNGalgame-locale', newVal)
})
const changeLanguage = () => {
locale.value = selectedLocale.value
}
export default {
selectedLocale,
changeLanguage,
}