Complete lang switch

This commit is contained in:
KUN1007 2023-05-21 00:16:15 +08:00
parent 6cf85280bd
commit 8448e5ffeb
5 changed files with 56 additions and 89 deletions

View file

@ -1,5 +1,7 @@
<!-- 设置面板组件展示整个论坛的设置面板 -->
<script setup lang="ts">
// vue
import { ref, watch, onMounted } from 'vue'
//
import { Icon } from '@iconify/vue'
//
@ -9,27 +11,32 @@ import Background from './components/Background.vue'
// store
import { useSettingsPanelStore } from '@/store/modules/settings'
import { storeToRefs } from 'pinia'
// hook
import { useLang } from '@/hooks/useLang'
// i18n
// i18n
import { useI18n } from 'vue-i18n'
//
const { kungalgameLang, setLang, initLang } = useLang()
const { t, locale } = useI18n({ useScope: 'global' })
const selectedLocale = ref(locale.value)
//
initLang()
// selectedLocaleVue I18nlocale
watch(selectedLocale, (newVal) => {
locale.value = newVal
})
//
const changeLang = () => {
if (kungalgameLang.value === 'en') {
setLang('en')
locale.value = 'en'
} else {
setLang('zh')
locale.value = 'zh'
// localStorage
onMounted(() => {
const savedLocale = localStorage.getItem('locale')
if (savedLocale) {
selectedLocale.value = savedLocale
}
})
// localStorage
watch(locale, (newVal) => {
localStorage.setItem('locale', newVal)
})
const changeLanguage = () => {
locale.value = selectedLocale.value
}
// 使 store
@ -68,12 +75,23 @@ const handleClose = () => {
</li>
</div>
</div>
<div class="fix-loli">
<span>语言设置</span>
<div class="set-lang">
<span>网站语言设置</span>
<select
class="select"
v-model="selectedLocale"
@change="changeLanguage"
>
<option value="en">English</option>
<option value="zh">中文</option>
</select>
</div>
<div>
<!-- 设置主页的宽度 -->
<span>主页页面宽度设置</span>
<div class="width-container">
<span>主页页面宽度设置</span>
<span>61.8%</span>
</div>
<div class="page-width">
<span>61.8%</span><input class="main" type="range" value="0" /><span
>90%</span
@ -127,6 +145,7 @@ const handleClose = () => {
}
}
}
// 使
.settings-icon {
animation: settings 3s linear infinite;
}
@ -159,6 +178,21 @@ const handleClose = () => {
color: @kungalgame-blue-4;
}
}
//
.set-lang {
display: flex;
justify-content: space-between;
}
//
.select {
width: 100px;
font-size: 16px;
border: 1px solid @kungalgame-blue-4;
background-color: @kungalgame-trans-white-9;
option {
background-color: @kungalgame-trans-white-9;
}
}
.page-width {
font-size: 15px;
display: flex;
@ -173,8 +207,10 @@ const handleClose = () => {
margin: 20px 0;
}
/* 固定看板娘 */
.fix-loli {
.set-lang {
margin-bottom: 20px;
}
.width-container {
display: flex;
justify-content: space-between;
}

View file

@ -1,26 +0,0 @@
// 钩子函数,网站显示语言
import { ref, watchEffect } from 'vue'
import { getLangStatus, setLangStatus } from '@/utils/cache/local-storage'
// 初始化 KUNGalgame 语言设置true 为中文
const kungalgameLang = ref<string>(getLangStatus() || 'zh')
const setLang = (status: string) => {
kungalgameLang.value = status
}
const initLang = () => {
watchEffect(() => {
setLangStatus(kungalgameLang.value)
})
}
// 全局 hook
export function useLang() {
return {
kungalgameLang,
setLang,
initLang,
}
}

View file

@ -1,13 +1,10 @@
import { createI18n } from 'vue-i18n'
// 获取网站 localStorage 中的默认语言
import { getLangStatus } from '@/utils/cache/local-storage'
// 引入语言文件
import zh from './zh'
import en from './en'
const i18n = createI18n({
locale: getLangStatus() || 'en',
// locale: 'zh',
locale: localStorage.getItem('locale') || 'en',
// 支持 Vue3 composition API
legacy: false,
// 全局注册 ts 方法

View file

@ -1,23 +0,0 @@
/* KUNGalgame cache key 的名字 */
const KUN = 'KUNGalgame'
// KUNGalgame 的 cache 键
class KUNCacheKey {
// 中文 / 英文
static MAIN_LANG = `${KUN}-lang`
// 白天 / 黑夜模式
static THEME_STATUS = `${KUN}-theme`
// 主页的宽度
static MAIN_PAGE_WIDTH = `${KUN}-main-page-width`
// 背景图片
static BACKGROUND_PICTURE = `${KUN}-background-picture`
// 是否固定看板娘
static LOLI_STATUS = `${KUN}-loli-status`
// 看板娘定位 X
static LOLI_POSITION_X = `${KUN}-loli-position-x`
// 看板娘定位 Y
static LOLI_POSITION_Y = `${KUN}-loli-position-y`
}
export default KUNCacheKey

View file

@ -1,17 +0,0 @@
// KUNGalgame localStorage
// 引入定义的键
import KUNCacheKey from './cache-key'
/*
* KUNGalgame
*/
export const getLangStatus = () => {
return localStorage.getItem(KUNCacheKey.MAIN_LANG) as string
}
export const setLangStatus = (lang: string) => {
// true -> chinese, false -> english
localStorage.setItem(KUNCacheKey.MAIN_LANG, lang)
}