美文网首页
uniapp 多语音切换

uniapp 多语音切换

作者: 实质xing | 来源:发表于2021-06-23 10:52 被阅读0次

1、npm安装

npm install vue-i18n

2、新建lang目录、语言包js


image.png
image.png

3、在main.js配置

import VueI18n from 'vue-i18n'
Vue.use(VueI18n)
const i18n = new VueI18n({
    locale : 'en-US', //语言标识
    messages: {
        'en-US' : require('common/lang/en.js') , //英文语言包
        'zh-CN' : require('common/lang/zh.js')  //中文简体语言包
    }
})

Vue.prototype._i18n = i18n
//默认是什么语言
Vue.prototype.$i18nMsg = function(){
    return i18n.messages[i18n.locale]
}
const app = new Vue({
    i18n
})

4、页面使用

//template中使用
{{i18n.index.statistics}}

//js:
//computed监听
computed:{
   i18n() {  
     return this.$i18nMsg()  
   }
}

相关文章

网友评论

      本文标题:uniapp 多语音切换

      本文链接:https://www.haomeiwen.com/subject/kkxbyltx.html