uni-app 语言化 Vue-i18n

说到vue-i18n,我们并不陌生,国际化多语言实现。

1.安装依赖包

//vue-i18n
npm install vue-i18n
//除了安装依赖包以外,还可以直接<script>标签直接引入,这种方式自行百度

2.注入vue实例中,项目中实现调用api和模板语法

(1)在main.js中引入vue-i18n

import Vue from 'vue'
import App from './App'
import store from './store'
import VueI18n from 'vue-i18n' 

Vue.use(VueI18n)
Vue.config.productionTip = false
Vue.prototype.$store = store

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]
}

App.mpType = 'app'

const app = new Vue({
    i18n, //重要!!!!
    store,
    ...App
})

app.$mount()

(2)en.js与zh.js

//语言包
export const index = {
    //英文语言包
    "statistics":'Statistics',
    "message":'Message',
        ......
}

(3)页面渲染

//computed监听
computed:{
   i18n() {  
     return this.$i18nMsg()  
   }
},
//template中使用
{{i18n.index.statistics}}//一般
{{i18n.index[item.name]}}//v-for循环中
:placeholder="i18n.index.input" //input placeholder提示信息

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。