本文主要是介绍多语言vue-i18n (vue2,uniapp),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
安装vue-i18n
npm install vue-i18n@8 --save
// npm install vue-i18n–save 9版本需要vue3.0
// 在vue2环境下,默认安装 npm install vue-i18n 的版本是 vue-i18n@9.2.2,
// 报错信息里提示这个版本要求是vue3,所以我们安装适合vue2版本的vue-i18n 依赖包。
// 运行指令查看所有版本
// npm view vue-i18n versions --json
创建语言文件
zh.js
//zh.js
const zh={lang:'中文'
}
export default zh
yn.js
//yn.js
const yn={lang:'Trung Quốc'
}
export default yn
index.js
// index.js
import Vue from 'vue'
import VueI18n from 'vue-i18n'
Vue.use(VueI18n)
// 引入各个语言配置文件
import zhLocale from './config/zh'
import ynLocale from './config/yn'// 创建vue-i18n实例i18n
const i18n = new VueI18n({// 设置默认语言locale: localStorage.getItem('language') || 'zh', // 语言标识// 添加多语言(每一个语言标示对应一个语言文件)messages: {'zh': zhLocale,'yn': ynLocale}
})// 暴露i18n
export default i18n
main.js
import Vue from 'vue'
import App from './App.vue'
// 引入i18n
import i18n from './index'new Vue({i18n,render: h => h(App)
}).$mount('#app')
vue页面使用
模板上使用: $t('xxx') js里面使用:this.$t('xxx') this.$i18n.locale 获取语言类型
<template><div><p>{{ $t('lang') }}</p><button @click="ceshi">切换</button></div>
</template><script>export default {methods: {ceshi(){//切换语言if(this.$i18n.locale=='yn'){this.$i18n.locale = 'zh';localStorage.setItem('language','zh')}else{this.$i18n.locale = 'yn';localStorage.setItem('language','yn')}}}}
</script>
菜单栏切换语言
深层数据切换语言,使用递归,可以结合vuex使用
<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>Document</title>
</head>
<body><script>const menuData = [{name: {en: 'Home',zh: '首页'},age:20,children: [{name: {en: 'News',zh: '新闻'},age:21,children: []},{name: {en: 'About',zh: '关于我们'},age:22,children: []}]},{name: {en: 'Services',zh: '服务'},age:23,children: [{name: {en: 'Web Design',zh: '网页设计'},age:24,children: []},{name: {en: 'Programming',zh: '编程'},age:25,children: []}]}
];function switchLanguage(data, lang) {
if (Array.isArray(data)) {if(data.length==0) return []} else {if(data==null) return []}return data.map(item => {return {...item,//name: item.name[lang],//如果是相同的属性就会替换children: switchLanguage(item.children, lang)};});
}// 切换到中文
const chineseMenuData = switchLanguage(menuData, 'en');console.log(chineseMenuData);</script>
</body>
</html>
uniapp多语言切换vue2 自带了vue-i18n
main.js
import Vue from 'vue'
import App from './App'
import VueI18n from 'vue-i18n'// v8.x// 国际化 json 文件,文件内容详见下面的示例
import zh from './zh.json'
import yn from './yn.json'
const messages = {'zh': zh,'yn': yn
}let i18nConfig = {locale:uni.getStorageSync('language') || 'zh',// 获取已设置的语言messages
}Vue.use(VueI18n)
const i18n = new VueI18n(i18nConfig)Vue.config.productionTip = falseApp.mpType = 'app'const app = new Vue({i18n,...App
})
app.$mount()
zh.json
{"login.lang": "语言选择","login.zh": "中文","login.yn": "越南语"
}
yn.json
{"login.lang": "Lựa chọn ngôn ngữ","login.zh": "Trung Quốc","login.yn": "Tiếng Việt"
}
vue页面使用
模板上使用: $t('xxx') js里面使用:this.$t('xxx') this.$i18n.locale 获取语言类型
<template><view><view class="flex align-center px-5 mb-4"><view>{{$t('login.lang')}}:</view><picker @change="bindPickerChange" :value="index11" :range="array" class="flex-1"><view class="uni-input">{{array[index11]}}</view></picker></view></view>
</template>
<script>export default {computed:{array(){return [this.$t('login.zh'),this.$t('login.yn')]},index11(){let inopif(this.$i18n.locale == 'zh'){inop=0}else{inop=1}return inop}},methods: {bindPickerChange: function(e) {this.index = e.detail.valueif(e.detail.value==0){this.$i18n.locale = 'zh';uni.setStorageSync('language','zh')}else{this.$i18n.locale = 'yn';uni.setStorageSync('language','yn')}}, }}
</script>
这篇关于多语言vue-i18n (vue2,uniapp)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!