uniapp 多语音切换

2021-06-23  本文已影响0人  实质xing

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()  
   }
}
上一篇 下一篇

猜你喜欢

热点阅读