uniapp国际化(html、js、tabbar和navigationBarTitleText)

本文介绍如何在uniapp结合Vue3的项目中实现多语言支持,覆盖HTML页面、JS提示及导航栏等,实现语言实时切换。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

如果你写的app是用uniapp+vue3来搭建的,这篇国际化一定适合你!

问题描述:因为app有海外用户所以需要配置国际化语言满足客户需求。

解决方案: 在uniapp中内置i18n多语言的配置,只要用户能看见的字(包括html页面,js的一些提示代码,tabbar导航和navigationBarTitleText)都要实现国际化,并且可以做到实时切换语言。

安装

如果是创建项目的时候选择的模板是uni-ui项目,则不需要npm vue-i18n。

npm install vue-i18n --save

下面开始i18n的配置,参考网址:uni-app官网

文件目录:

 1.配置语言包

代码配置

为了方便把多语言的配置代码写在了locale/index.ts里面,之后在mian.js中引入

import {
	createI18n
} from 'vue-i18n'

import en from './en.json'
import zhHans from './zh-Hans.json'
import zhHant from './zh-Hant.json'


const messages = {
	en,
	'zh-Hans': zhHans,
	'zh-Hant': zhHant
}

const i18n = createI18n({
	locale: uni.getLocale() || 'zh-Hant', // 获取已设置的语言,如果没有默认繁体
	messages
})

export default i18n

在main.js中引入(因为项目是用的vue3,可以只配置vue3)

import App from './App'
import i18n from '@/locale/index'

// vue2
// #ifndef VUE3
import Vue from 'vue'
Vue.config.productionTip = false
App.mpType = 'app'
const app = new Vue({
    i18n,
	...App
})
app.$mount()
// #endif

// vue3
// #ifdef VUE3
import {
	createSSRApp
} from 'vue'
export function createApp() {
	const app = createSSRApp(App)
	app.use(i18n)
	return {
		app
	}
}
// #endif

在html页面使用 

$t是i18n自带的全局配置的方法,可以在html中直接使用

<view class="title">{{$t('index.hello')}}</view>

在js或者ts中使用

vue2在js中多语言用的是this.$t(),但是vue3中this为undefined,所以我这里用的是下面的方法

<script setup lang="ts">
    import { useI18n } from 'vue-i18n'
    const { t } = useI18n()

    const test = () => {
        console.log(t('index.home'))
    }
</script>

在pages.json中使用 %% 占位

{
	"pages": [ //pages数组中第一项表示应用启动页,参考:https://uniapp.dcloud.io/collocation/pages
		{
			"path": "pages/index/index",
			"style": {
				"navigationBarTitleText": "%index.home%"
			}
		},
		{
			"path": "pages/about/about",
			"style": {
				"navigationBarTitleText": "%index.about%"
			}
		}
	],
	"globalStyle": {
		"navigationBarTextStyle": "black",
		"navigationBarTitleText": "uni-app",
		"navigationBarBackgroundColor": "#F8F8F8",
		"backgroundColor": "#F8F8F8"
	},
	"uniIdRouter": {},
	"tabBar": {
		"list": [
			{
				"pagePath": "pages/index/index",
				"text": "%index.home%"
			},
			{
				"pagePath": "pages/about/about",
				"text": "%index.about%"
			}
		]
	}
}

切换多语言

通过uni.setLocale('设置的值')设置语言,可选值包括但不限于: zh-Hans(简体中文)、zh-Hant(繁体中文)、en(英文),详情请参考上面的官网。

需要注意的是: uni.setLocale()方法会重启该应用。

### 创建 UniApp 微信小程序中的自定义 tabBar #### 配置 `app.json` 在配置文件 `app.json` 中,移除默认的 tabBar 设置以便完全控制底部导航栏的行为外观[^1]。 ```json { "pages": [ "pages/index/index", "pages/logs/logs" ], "window": { "backgroundTextStyle": "light", "navigationBarBackgroundColor": "#fff", "navigationBarTitleText": "WeChat", "navigationBarTextStyle": "black" } } ``` #### 编写自定义 tabBar 组件 创建一个新的 Vue 文件作为自定义 tabBar 组件,在此例子中命名为 `tabbar.vue` 并放置于 `components/tabbar/` 目录下。这个组件负责渲染所有的 tab 按钮并处理点击事件来切换页面[^3]。 ```vue <template> <view class="custom-tab-bar"> <!-- 这里是简化版的模板 --> <button v-for="(item, index) in items" :key="index" @click="switchTab(item.pagePath)"> {{ item.text }} </button> </view> </template> <script> export default { data() { return { items: [ { text: '首页', pagePath: '/pages/index/index' }, { text: '日志', pagePath: '/pages/logs/logs' } ] }; }, methods: { switchTab(path) { uni.switchTab({ url: path }); } } }; </script> <style scoped> .custom-tab-bar { display: flex; justify-content: space-around; padding: 8px 0; background-color: #f7f7f7; } button { border: none; background-color: transparent; font-size: 16px; } </style> ``` #### 注册全局组件 打开项目的入口文件 `main.js` 或者按照框架规定的位置引入编写的 tabBar 组件,并注册为全局可用的标签名 `<tabbar>`。 ```javascript import Vue from 'vue'; import App from './App'; // 导入自定义 tabBar 组件 import tabbar from "./components/tabbar/tabbar.vue"; Vue.config.productionTip = false; // 将其注册成全局组件 Vue.component('tabbar', tabbar); App.mpType = 'app'; const app = new Vue({ ...App }); app.$mount(); ``` #### 使用自定义 tabBar 最后一步是在各个页面中显示自定义的 tabBar。可以在每个页面对应的 `.vue` 文件内的 `<template>` 标签内加入 `<tabbar></tabbar>` 来实例化该组件[^2]。 ```html <!-- pages/index/index.vue --> <template> <view class="container"> <text>这是首页的内容。</text> <!-- 显示自定义 tabBar --> <tabbar /> </view> </template> ``` 通过上述操作就可以成功地在一个基于 UniApp 构建的微信小程序项目中实现自定义 tabBar 功能了。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值