切换导航
{{systemName}}
{{ info.Title }}
{{info.Title}}
{{ menu.Title }}
{{menu.Title}}
登录
|
退出
搜索
uniapp多语言
作者:ych
### 页面切换语言 #### 安装vue-i18n ``` npm install vue-i18n ``` #### 语言设置 main.js ``` import App from './App' import Vue from 'vue' // 引入mock import './mock/index.js' import commonMethods from './utils/common.js'; import zh from '@/lang/zh-Hans.js'; import en from '@/lang/en.js'; import VueI18n from 'vue-i18n' import store from './store'; Vue.use(VueI18n) const i18n = new VueI18n({ // 默认语言 locale: 'zh', messages: { 'zh': zh, 'en': en, } }) // 由于微信小程序的运行机制问题,需声明如下一行,H5和APP非必填 Vue.prototype._i18n = i18n Vue.prototype.store=store; Vue.prototype.$commonMethods = commonMethods; import uView from 'uview-ui' Vue.use(uView) uni.$u.setConfig({ config: { unit: 'rpx' }, props: { radio: { size: 15 } } }) Vue.config.productionTip = false App.mpType = 'app' const app = new Vue({ store, i18n, ...App }) app.$mount() import { createSSRApp } from 'vue' export function createApp() { const app = createSSRApp(App) return { app } } ``` #### lang.vue ```
{{$t('languageswitch')}}
<script> export default { data() { return { value: 'zh-Hans', range: [ { value: 'zh-Hans', text: "中文简体" }, { value: 'en', text: "英文" }, ], }; }, methods: { change(e) { console.log(e); this.$i18n.locale = e; uni.setStorageSync("lang",e); uni.reLaunch({ url: '/' }); // 刷新当前页面使新的语言生效 }, }, }; ``` #### 遍历设置tabbar ``` const tabNames = ['home', 'my']; tabNames.forEach((name, index) => { uni.setTabBarItem({ index, text: this.$t(name), }); }); ``` #### 公共方法 common.js ``` // globalMethods.js import zhCnTranslations from '@/lang/zh-Hans.js'; import enUsTranslations from '@/lang/en.js'; const translations = { 'zh-Hans': zhCnTranslations, 'en': enUsTranslations, }; function translate(key,lang) { return translations[lang][key]; } const globalMethods = { // 获取当前日期的年月日 getCurrentDate() { const now = new Date(); const year = now.getFullYear(); const month = String(now.getMonth() + 1).padStart(2, '0'); const day = String(now.getDate()).padStart(2, '0'); return `${year}-${month}-${day}`; }, // 获取一周前的时间 getWeekDate() { const now = new Date(); const oneWeekAgo = new Date(now.getTime() - 7 * 24 * 60 * 60 * 1000); const year = oneWeekAgo.getFullYear(); const month = String(oneWeekAgo.getMonth() + 1).padStart(2, '0'); const day = String(oneWeekAgo.getDate()).padStart(2, '0'); return `${year}-${month}-${day}`; }, // 获取一月前的时间 getMouthDate() { const now = new Date(); const oneMonthAgo = new Date(now.getFullYear(), now.getMonth() - 1, now.getDate()); const year = oneMonthAgo.getFullYear(); const month = String(oneMonthAgo.getMonth() + 1).padStart(2, '0'); const day = String(oneMonthAgo.getDate()).padStart(2, '0'); return `${year}-${month}-${day}`; }, // 获取三个月前的时间 getMouthsDate() { const now = new Date(); const oneMonthAgo = new Date(now.getFullYear(),now.getMonth() - 3, now.getDate()); const year = oneMonthAgo.getFullYear(); const month = String(oneMonthAgo.getMonth() + 1).padStart(2, '0'); const day = String(oneMonthAgo.getDate()).padStart(2, '0'); return `${year}-${month}-${day}`; }, // 判断是否为手机号码 isPhoneNumber(value) { const reg = /^1[3456789]\d{9}$/; return reg.test(value); }, // 将时间戳转换为指定格式的日期字符串 formatDate(timestamp, format = 'YYYY-MM-DD HH:mm:ss') { const date = new Date(timestamp); const year = date.getFullYear(); // ... 根据 format 参数进行格式化输出 return formattedDate; }, translate(key) { const currentLocale =uni.getStorageSync("lang"); // 当前语言,可根据自己需求进行设置 return translate(key,currentLocale); } // ... 在这里可以定义更多通用方法 }; export default globalMethods; ``` #### 使用公共方法 ``` this.$commonMethods.translate("home"); const tabNames = ['home', 'my']; tabNames.forEach((name, index) => { uni.setTabBarItem({ index, text: this.$t(name), }); }); ``` #### 设置顶部标题 ``` // 设置顶部标题 uni.setNavigationBarTitle({ title: this.$commonMethods.translate("home") }); ``` #### common其他写法 ##### 写法1 ``` // globalMethods.js import zhCnTranslations from '@/lang/zh-Hans.js'; import enUsTranslations from '@/lang/en.js'; function translate(key, lang) { const translations = { 'zh-Hans': zhCnTranslations, 'en': enUsTranslations, }; return translations[lang][key]; } const globalMethods = { translate(key) { const currentLocale =uni.getStorageSync("lang"); // 当前语言,可根据自己需求进行设置 console.log(translate('home',currentLocale)); // 输出:首页 console.log(translate('about',currentLocale)); // 输出:关于我们 }, // ... 在这里可以定义更多通用方法 }; export default globalMethods; ``` ##### 写法2 ``` // globalMethods.js import zhCnTranslations from '@/lang/zh-Hans.js'; import enUsTranslations from '@/lang/en.js'; function getCurrentLanguage() { return uni.getStorageSync('lang'); } function translate(key) { const currentLocale = getCurrentLanguage(); // 当前语言,可根据自己需求进行设置 const translations = { 'zh-Hans': zhCnTranslations, 'en': enUsTranslations, }; return translations[currentLocale][key]; } const globalMethods = { translate(key) { console.log(translate('home')); // 输出:首页 console.log(translate('about')); // 输出:关于我们 }, // ... 在这里可以定义更多通用方法 }; export default globalMethods; ``` ### 更多 您需要通过npm安装此插件: 如果您的项目由HX创建,根目录没有package.json的话,先通过如下命令创建package.json ``` npm init -y ``` #### 安装vue-i18n ``` npm install vue-i18n ``` 在main.js中引用vue-i18n ``` // 原有内容 import Vue from 'vue' import App from './App' // 以下为添加的内容 // 引入语言包,注意路径 import zh from '@/common/locales/zh.js'; import en from '@/common/locales/en.js'; // 引入并使用vue-i18n import VueI18n from 'vue-i18n' Vue.use(VueI18n) // 构造i18n对象 const i18n = new VueI18n({ // 默认语言,这里的local属性,对应message中的zh、en属性 locale: 'zh', // 引入语言文件 messages: { // 这里的属性名是任意的,您也可以把zh设置为cn等,只是后续切换语言时 // 要标识这里的语言属性,如:this.$i18n.locale = zh|en|zh|xxx 'zh': zh, // 这里为上面通过import引入的语言包 'en': en, } }) // 由于微信小程序的运行机制问题,需声明如下一行,H5和APP非必填 Vue.prototype._i18n = i18n // 原有内容,需在这添加i18n const app = new Vue({ // 记得在这添加i18n i18n, store, ...App }) ``` 注意 别忘了上面的最后处,需要在new Vue构造器中写入i18n 定义语言包 上面我们在main.js通过import引入了两个语言包,一般来说,需要多少种语言,就要有多少个语言包,建议语言包中通过定义不同的字段划分不同页面 所属的素材: ``` // zh.js export default { // 可以以页面为单位来写,比如首页的内容,写在index字段,个人中心写在center,公共部分写在common部分 lang: { title: 'uView UI', intro: '多平台快速开发的UI框架', }, common: { // ...... }, index: { // ...... } } ``` 使用 在实际场景中,我们可能在js中,也有可能在模板中使用,需通过$t('lang.title')的形式引用,这里的lang是我们上一步zh.js中定义的lange字段,title 自然而然就是lang对象的title属性了。 ```
{{$t('lang.intro')}}
<script> export default { onLoad() { console.log(this.$t('lang.intro')); } } ``` 语言切换 当我们点击语言切换的时候,通过this.$i18n.locale来设置新的语言,这个语言是我们定义在main.js中的message属性: 回顾:我们上面第二步在main.js中进行了如下设置,其中的message对象的zh和en就是我们所能切换的语言: ``` // main.js // 构造i18n对象 const i18n = new VueI18n({ // 默认语言,这里的local属性,对应message中的zh、en属性 locale: 'zh', // 引入语言文件 messages: { // 这里的属性名是任意的,您也可以把zh设置为zh等,只是后续切换语言时 // 要标识这里的语言属性,如:this.$i18n.locale = zh|en|zh|xxx 'zh': Chinese, 'en': English, } }) ``` 语言切换: ```
{{$t('lang.intro')}}
切换语言
<script> export default { onLoad() { console.log(this.$t('lang.intro')); }, methods: { switchLang() { // 切换为英文 this.$i18n.locale = 'en'; } } } ``` 双向绑定的陷阱 有时候,我们可能需要给data中的属性赋值多语言的值,很遗憾当您切换语言时会发现这是无法双向绑定的,体现在语言切换了,但是视图并没有更新。 这本质上是因为data中的属性是一次性赋值的,解决办法是在computed中定义相关的变量。 ```
{{intro}}
切换语言
<script> export default { data() { return { // 错误示例,切换语言时,这个intro并不会自动更新到视图 // intro: this.$t('lang.intro') } }, computed: { // 正确用法 intro() { return this.$t('lang.intro') } }, methods: { switchLang() { this.$i18n.locale = 'en'; } } } ``` 难点 上面我们解决了如何引入插件,定义和使用语言包,但是我们依然会碰到如下几个难点: 如果我需要在js中判断当前语言,进行不同的页面跳转怎么办? 如何修改导航栏的标题? 如何修改底部Tabbar导航的文字? 没关系,uView已经为您提供了完善的解决方案: 1. 如果我需要在js中判断当前语言,进行不同的页面跳转怎么办? 这个其实很简单,我们只需通过this.$i18n.locale就能获取当前的语言名称。当然,您也可以将当前的语言变量放到一个全局变量中, 关于全局变量,详见我们的另一个专题:全局变量的实现方式 2. 如何修改导航栏的标题? 修改原生导航栏,我们需要通过uni.setNavigationBarTitle()进行,既然是调用uni的接口,意味着我们无法通过双向绑定,切换语言时自动更新导航栏标题,所以 我们需要在onShow生命周期调用此接口,当每次页面出现在窗口时,重新设置导航栏(不管语言是否切换): ```
{{$t('lang.intro')}}
切换语言
<script> export default { // 在onShow生命周期设置导航栏标题 onShow() { uni.setNavigationBarTitle({ title: this.$t('lang.title') }); }, methods: { switchLang() { this.$i18n.locale = 'en'; // 当您切换了语言之后,并不会触发onShow生命周期,意味着本页的标题不会马上 // 变更,所以我们需要在切换了语言之后,手动执行一遍设置标题的接口 uni.setNavigationBarTitle({ title: this.$t('lang.title') }); } } } ``` 3. 如何修改底部Tabbar导航的文字? 我们可以通过uni.setTabBarItem()设置单个的Tabbar Item,这很简单,我们在切换语言的地方,将所有的item设置一遍即可: ```
切换语言
<script> export default { methods: { switchLang() { this.$i18n.locale = 'en'; // uni.setTabBarItem接口详见:https://uniapp.dcloud.io/api/ui/tabbar?id=settabbaritem // 说明:这种方法不适用自定义tabbar,自定义tabbar请自行根据逻辑调整 // 注意:【支付宝小程序开发工具】需要1.13版本才支持此接口的模拟,真机预览不受限制 uni.setTabBarItem({ index: 0, text: this.$t('tabbar.index'), }) uni.setTabBarItem({ index: 1, text: this.$t('tabBar.news'), }) uni.setTabBarItem({ index: 2, text: this.$t('tabBar.center'), }) } } } ```
相关推荐
uniapp 富文本折叠展开组件
uniapp定义公共函数及uniapp中使用vuex
uniapp路由跳转实现全解
vue子组件向父组件传值
vue父传子实现
uniapp下边浮动框tabbar
uniapp使用vue方式封装请求
uniapp开发实战
uview插件体积过大
评论区
先去登录
版权所有:机遇屋在线 Copyright © 2021-2025 jiyuwu Co., Ltd.
鲁ICP备16042261号-1