在項目中集成依賴
$ cnpm i vue-i18n --save
注入 vue 實例中
在src目錄創建一個文件夾 language
//language/index.js
import Vue from 'vue'
import VueI18n from 'vue-i18n';
import store from "@/store.js";
import ENLocale from './EN'//英文
import CNLocale from './CN'//中文
Vue.use(VueI18n)
const messages = {
EN: {
...ENLocale,
},
CN: {
...CNLocale,
}
}
const i18n = new VueI18n({
locale: store.state.app.activeLanguage || 'CN',
messages
})
export default i18n
cn.js 配置語言包
export default {
index: '首頁',
}
main.js 中掛載
import i18n from "@/language";
...
new Vue({
router,
store,
i18n,
render: h => h(App)
}).$mount('#app')
單文件使用
<h1>{{$t('index')}}</h1>
<span v-text="$t('index')"></span>
切換語言
this.$i18n.locale="EN";