updata:移动端添加语言切换功能

This commit is contained in:
rainv123
2025-09-26 14:33:06 +08:00
parent d022dd385a
commit dfcac51312
23 changed files with 2248 additions and 589 deletions
+55
View File
@@ -1,12 +1,16 @@
<script setup lang="ts">
import { onHide, onLaunch, onShow } from '@dcloudio/uni-app'
import { watch, onMounted } from 'vue'
import { usePageAuth } from '@/hooks/usePageAuth'
import { useConfigStore } from '@/store'
import { t } from '@/i18n'
import { useLangStore } from '@/store/lang'
import 'abortcontroller-polyfill/dist/abortcontroller-polyfill-only'
usePageAuth()
const configStore = useConfigStore()
const langStore = useLangStore()
onLaunch(() => {
console.log('App Launch')
@@ -17,7 +21,58 @@ onLaunch(() => {
})
onShow(() => {
console.log('App Show')
// 使用setTimeout延迟执行,确保tabBar已经初始化
setTimeout(() => {
updateTabBarText()
}, 100)
})
// 动态更新tabBar文本
function updateTabBarText() {
try {
// 设置首页tabBar文本
uni.setTabBarItem({
index: 0,
text: t('tabBar.home'),
success: () => {},
fail: (err) => {
console.log('设置首页tabBar文本失败:', err)
}
})
// 设置配网tabBar文本
uni.setTabBarItem({
index: 1,
text: t('tabBar.deviceConfig'),
success: () => {},
fail: (err) => {
console.log('设置配网tabBar文本失败:', err)
}
})
// 设置系统tabBar文本
uni.setTabBarItem({
index: 2,
text: t('tabBar.settings'),
success: () => {},
fail: (err) => {
console.log('设置系统tabBar文本失败:', err)
}
})
} catch (error) {
console.log('更新tabBar文本时出错:', error)
}
}
// 监听语言切换事件
onMounted(() => {
// 监听语言变化,当语言改变时自动更新tabBar文本
watch(() => langStore.currentLang, () => {
console.log('语言已切换,更新tabBar文本')
// 语言切换后立即更新tabBar文本
updateTabBarText()
})
})
onHide(() => {
console.log('App Hide')
})