fix(i18n): 修复微信小程序国际化问题

修复微信小程序 tabbar 的 text 和导航栏标题的国际化问题
This commit is contained in:
wyl86 2024-06-19 12:11:14 +08:00
parent db5dc87df1
commit c970e7bee1
No known key found for this signature in database
GPG Key ID: 778CA5A622551CA5
2 changed files with 27 additions and 0 deletions

View File

@ -1,8 +1,25 @@
<script setup lang="ts">
import { onLaunch, onShow, onHide } from '@dcloudio/uni-app'
import { useI18n } from 'vue-i18n'
import { tabBar } from '@/pages.json'
onLaunch(() => {
console.log('App Launch')
// #ifdef MP-WEIXIN
const setTabbarText = () => {
const { t } = useI18n()
const tabbarTexts = tabBar.list.map((item) => item.text.replace(/(^%|%$)/g, ''))
tabbarTexts.forEach((transKey: string, index: number) => {
uni.setTabBarItem({
index,
text: t(transKey),
})
})
}
// fix api tabbar text
setTabbarText()
uni.onLocaleChange(setTabbarText)
// #endif
})
onShow(() => {
console.log('App Show')

View File

@ -47,6 +47,16 @@
<script lang="ts" setup>
import i18n, { formatI18n, formatString, translate } from '@/locale/index'
import { testI18n } from '@/utils/i18n'
import { useI18n } from 'vue-i18n'
const { t } = useI18n()
onLoad(() => {
// #ifdef MP-WEIXIN
// fix api
uni.setNavigationBarTitle({ title: t('app.name') })
// #endif
})
const current = ref(uni.getLocale())
const user = { name: '张三', detail: { height: 178, weight: '75kg' } }