feat(i18n): 添加微信小程序国际化支持

在微信小程序中,手动设置导航栏标题和tabbar文本以确保国际化生效。同时,在页面中添加了多语言测试的相关代码。
This commit is contained in:
feige996 2025-05-21 10:13:01 +08:00
parent ebdaa41a6f
commit 125e305cd7
4 changed files with 36 additions and 0 deletions

View File

@ -1,9 +1,30 @@
<script setup lang="ts"> <script setup lang="ts">
import { onLaunch, onShow, onHide } from '@dcloudio/uni-app' import { onLaunch, onShow, onHide } from '@dcloudio/uni-app'
import 'abortcontroller-polyfill/dist/abortcontroller-polyfill-only' import 'abortcontroller-polyfill/dist/abortcontroller-polyfill-only'
import { useI18n } from 'vue-i18n'
import { tabBarList, getIsTabbar } from '@/utils/index'
onLaunch(() => { onLaunch(() => {
console.log('App Launch') console.log('App Launch')
// #ifdef MP-WEIXIN
const setTabbarText = () => {
if (!getIsTabbar()) {
return
}
const { t } = useI18n()
const tabbarTexts = tabBarList.map((item) => item.text.replace(/(^%|%$)/g, ''))
tabbarTexts.forEach((tabbar: string, index: number) => {
console.log('tabbar', tabbar)
uni.setTabBarItem({
index,
text: t(tabbar),
})
})
}
// fix api tabbar text
setTabbarText()
uni.onLocaleChange(setTabbarText)
// #endif
}) })
onShow(() => { onShow(() => {
console.log('App Show') console.log('App Show')

View File

@ -27,6 +27,17 @@
<script lang="ts" setup> <script lang="ts" setup>
import RequestComp from './components/request.vue' import RequestComp from './components/request.vue'
import UploadComp from './components/upload.vue' import UploadComp from './components/upload.vue'
import i18n, { t } from '@/locale/index'
onShow(() => {
console.log('About Show', t('app.name'))
console.log(uni.getLocale())
console.log(i18n.global.locale)
// #ifdef MP-WEIXIN
// fix api
uni.setNavigationBarTitle({ title: t('app.name') })
// #endif
})
// //
const { safeAreaInsets } = uni.getSystemInfoSync() const { safeAreaInsets } = uni.getSystemInfoSync()

View File

@ -22,7 +22,10 @@
</view> </view>
<view class="text-green-500">多语言测试</view> <view class="text-green-500">多语言测试</view>
<view class="m-4">{{ $t('app.name') }}</view> <view class="m-4">{{ $t('app.name') }}</view>
<view class="italic text-gray-500">使用$t: {{ $t('weight', { heavy: 100 }) }}</view>
<view class="m-4">{{ $t('weight', { heavy: 100 }) }}</view> <view class="m-4">{{ $t('weight', { heavy: 100 }) }}</view>
<view class="italic text-gray-500">使用t: {{ t('weight', { heavy: 100 }) }}</view>
<view class="m-4">{{ t('weight', { heavy: 100 }) }}</view>
<view class="m-4">{{ t('introduction', user) }}</view> <view class="m-4">{{ t('introduction', user) }}</view>
<view class="text-green-500 mt-12">切换语言</view> <view class="text-green-500 mt-12">切换语言</view>

View File

@ -2,6 +2,7 @@ import pagesConfig from '@/pages.json'
import { isMpWeixin } from './platform' import { isMpWeixin } from './platform'
const { pages, subPackages, tabBar = { list: [] } } = { ...pagesConfig } const { pages, subPackages, tabBar = { list: [] } } = { ...pagesConfig }
export const tabBarList = tabBar.list
export const getLastPage = () => { export const getLastPage = () => {
// getCurrentPages() 至少有1个元素所以不再额外判断 // getCurrentPages() 至少有1个元素所以不再额外判断