說到vue-i18n,我們并不陌生融师,國際化多語言實現(xiàn)奠旺。
1.安裝依賴包
//vue-i18n
npm install vue-i18n
//除了安裝依賴包以外宦棺,還可以直接<script>標簽直接引入舌缤,這種方式自行百度
2.注入vue實例中镀岛,項目中實現(xiàn)調(diào)用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, //重要!S淹浴!驾锰!
store,
...App
})
app.$mount()
(2)en.js與zh.js
//語言包
export const index = {
//英文語言包
"statistics":'Statistics',
"message":'Message',
......
}
(3)頁面渲染
//computed監(jiān)聽
computed:{
i18n() {
return this.$i18nMsg()
}
},
//template中使用
{{i18n.index.statistics}}//一般
{{i18n.index[item.name]}}//v-for循環(huán)中
:placeholder="i18n.index.input" //input placeholder提示信息