1、安裝
npm install vue-i18n --save
2、創建存放語言包和i18n入口文件
a、在src下創建i18n目錄
b、在src/i18n/創建i18n.js (入口)
c、在src/i18n/創建langs目錄存放語言包
d、在src/i18n/langs/創建en.js(英文) 和 zh.js(中文)兩個文件
i18n.js

1 import Vue from 'vue' 2 import locale from 'element-ui/lib/locale'; 3 import VueI18n from 'vue-i18n' 4 //import messages from './langs' 5 6 //自定義中英文包 7 import zh from './langs/zh' 8 import en from './langs/en' 9 10 //element 中英文包 11 import enLocale from 'element-ui/lib/locale/lang/en' 12 import zhLocale from 'element-ui/lib/locale/lang/zh-CN' 13 Vue.use(VueI18n) 14 15 const messages = { 16 en: Object.assign(en, enLocale), 17 zh: Object.assign(zh, zhLocale) 18 } 19 //從localStorage中拿到用戶的語言選擇,如果沒有,那默認中文。 20 /*const i18n = new VueI18n({ 21 locale: localStorage.lang || 'zh', 22 messages, 23 })*/ 24 const i18n = new VueI18n({ 25 locale: localStorage.getItem('locale') || 'en', 26 messages 27 }) 28 locale.i18n((key, value) => i18n.t(key, value)) //為了實現element插件的多語言切換 29 console.log('%c當前緩存語言是:'+(localStorage.getItem('locale')=='en'?'English':'中文')+'','color:#fff;background:green;padding:4px;border-radius:5px'); 30 export default i18n
en.js

1 import enLocale from 'element-ui/lib/locale/lang/en' 2 const en = { 3 //所有側導航的name 4 slideBar:{ 5 overall:'Overview', 6 servicevolume:'Service Volume', 7 visitvolume:'Visit Volume', 8 users:'User Volume', 9 multimediausage:'Multimedia Volume', 10 servicefficiency:'Service Efficiency', 11 solvedstatus:'Helpful Rate', 12 transferredrate:'Transfer Rate', 13 cast:'CSAT', 14 kgperformance:'KG Performance', 15 question:'Questions', 16 outofscopeanalysis:'Question Distribution', 17 sessionflow:'Session Flow', 18 handingtime:'Handling Time', 19 handingturns:'Handling Turns', 20 leavingstatus:'Leaving Status', 21 customerjourney:'Customer Journey', 22 download:"DownLoad" 23 }, 24 25 //所有的篩選器name 26 select:{ 27 country:'Country', 28 datarange:'Date Range', 29 timeUnit:'Time Unit', 30 channel:"Channel", 31 kgLevel:"KG Level", 32 domain:"Domain", 33 intent:"Intent", 34 slot:"Order" 35 }, 36 37 //頁面中圖表指標 的name 38 indicatorBar:{ 39 totalVisits:"Total Visits", 40 totalUsers:"Total Users", 41 helpfulRate:"Helpful Rate", 42 transferRate:"Transfer Rate", 43 CSATRate:"CSAT", 44 newUsers:"New Users", 45 repeatUser:"Repeat Users", 46 solvedCases:"Helpful Cases", 47 solvedRate:"Helpful Rate", 48 transferredCases:"Transfer Cases", 49 trnasferredRate:"Transfer Rate" 50 }, 51 52 //頁面中tab選項卡name 53 Tab:{ 54 totalVolume:"Total Volume", 55 channel:"Channel Comparison", 56 user:"User Distribution", 57 userVolume:"User Volume Trend", 58 repeatUser:"Repeat User Distribution", 59 solvedRate:"Helpful Rate", 60 solvedUnsolvedQuestions:"Solved/Unsolved Questions", 61 transfered:"Transfer Rate", 62 transferedQuestion:'Transfer/Not Transfer Questions' 63 }, 64 //下載頁 65 download:{ 66 tab:{ 67 tabOne:"ChatLog Data", 68 tabTwo:"Visit Data", 69 tabThree:"User Data" 70 }, 71 ele:{ 72 datapickerName:"Period", 73 selectName:"Visit Access Channel", 74 until:"until" 75 }, 76 btn:{ 77 download:"DownLoad" 78 } 79 }, 80 81 //element 組件 82 element:{ 83 loadingText:"Loading..." 84 }, 85 86 //時間單位(備用) 87 dateLabel:{ 88 years:"years", 89 month:"month", 90 week:"week", 91 day:"day" 92 } 93 } 94 export default en;
zh.js

1 const cn = { 2 //所有側導航的name 3 slideBar: { 4 overall: '整體概覽', 5 servicevolume: '服務數量', 6 visitvolume: '訪問量', 7 users: '用戶量', 8 multimediausage: '輸入類型', 9 servicefficiency: '服務效果', 10 solvedstatus: '解決情況', 11 transferredrate: '轉人工表現', 12 cast: '用戶滿意度', 13 kgperformance: '知識表現', 14 question: '問題分布', 15 outofscopeanalysis: '對話流程', 16 sessionflow: '對話流程', 17 handingtime: '解決時長', 18 handingturns: '解決輪數', 19 leavingstatus: '結束離開狀態', 20 customerjourney: '用戶路徑', 21 download:"下載" 22 }, 23 24 //所有的篩選器name 25 select: { 26 country: '國家', 27 datarange: '時間區間', 28 timeUnit: '時間單位', 29 channel: "渠道", 30 kgLevel: "知識層級", 31 domain: "域", 32 intent: "意圖", 33 slot:"排序" 34 }, 35 //頁面中圖表指標 的name 36 indicatorBar: { 37 totalVisits: "訪問總量", 38 totalUsers: "用戶總量", 39 helpfulRate: "解決率", 40 transferRate: "轉人工率", 41 CSATRate: "用戶滿意度", 42 newUsers: "新用戶", 43 repeatUser: "重復訪問用戶", 44 solvedCases: "解決數量", 45 solvedRate: "解決率", 46 transferredCases: "轉人工數量", 47 trnasferredRate: "轉人工率" 48 }, 49 //頁面中tab選項卡name 50 Tab: { 51 totalVolume: "訪問總量", 52 channel: "訪問渠道對比", 53 user: "用戶國家分布", 54 userVolume: "用戶訪問趨勢", 55 repeatUser: "重復用戶分布", 56 solvedRate: "解決率", 57 solvedUnsolvedQuestions: "解決/未解決的問題分布", 58 transfered: "轉人工率", 59 transferedQuestion:'轉人工/未轉人工的問題分布' 60 }, 61 //下載頁 62 download:{ 63 tab:{ 64 tabOne:"日志下載", 65 tabTwo:"訪問數據下載", 66 tabThree:"用戶數據下載" 67 }, 68 ele:{ 69 datapickerName:"時間區間", 70 selectName:"訪問渠道", 71 until:"至" 72 }, 73 btn:{ 74 download:"下載" 75 } 76 }, 77 78 //element 組件 79 element:{ 80 loadingText:"拼命加載中..." 81 }, 82 83 //時間單位(備用) 84 dateLabel:{ 85 years:"年", 86 month:"月", 87 week:"周", 88 day:"日" 89 } 90 } 91 92 export default cn;
3、main.js中引用src/i18n/i18n.js入口文件

1 //引入vue 和 router模塊 2 import Vue from 'vue' 3 import App from './App' 4 import router from './router' 5 //引入element-ui 6 import ElementUI from 'element-ui' 7 import 'element-ui/lib/theme-chalk/index.css' 8 9 import i18n from './i18n/i18n' 10 11 Vue.use(ElementUI) 12 Vue.config.productionTip = false 13 14 let gvm = new Vue({ 15 el: '#app', 16 router, 17 i18n, 18 components: { App }, 19 template: '<App/>' 20 })
4、找一個vue文件(header.vue)
a、創建模板--> 下拉組件,有中英文兩個選項

1 <template> 2 <div class="navbar"> 3 <span>Concept Tree Engineer</span> 4 <p></p> 5 <el-select style="width: 87px;" size="mini" v-model="value" @change="toggleLang" placeholder="請選擇"> 6 <el-option 7 v-for="item in options" 8 :key="item.value" 9 :label="item.label" 10 :value="item.value" 11 > 12 </el-option> 13 </el-select> 14 </div> 15 </template>
b、創建script (配置默認為英文、配置下拉選擇切換語言、配置首次進入獲取用戶緩存的語言進行顯示)

1 <script> 2 export default { 3 name: 'Navbar', 4 data() { 5 return { 6 options: [{ 7 value: 'en', 8 label: 'English' 9 }, { 10 value: 'zh', 11 label: '中文' 12 }], 13 value: 'en' 14 } 15 }, 16 mounted(){ 17 //用戶每次刷新頁面都判斷 是否緩存過 語言,緩存過的話 選擇其中顯示的應該是緩存的語言 18 localStorage.getItem('locale') == 'en' ? this.value = 'en' : this.value = 'zh' 19 }, 20 methods: { 21 toggleLang(lang) { 22 const loading = this.$loading({ 23 lock: true, 24 text: this.$t("element.loadingText"), 25 spinner: 'el-icon-loading', 26 background: 'rgba(0, 0, 0, 0.7)' 27 }); 28 setTimeout(function(){ 29 loading.close(); 30 },600) 31 if(lang == 'zh') { 32 localStorage.setItem('locale', 'zh') 33 this.$i18n.locale = localStorage.getItem('locale') 34 } else if(lang == 'en') { 35 localStorage.setItem('locale', 'en') 36 this.$i18n.locale = localStorage.getItem('locale') 37 } 38 } 39 } 40 } 41 </script>
總結:
1、this.options 配置的是下拉框顯示的option 和 選擇后的value
this.value 是用戶選擇后雙向綁定的 options.[index].value
2、toggleLang(lang):用戶選擇后,調用toggleLang方法,判斷是 選擇的是英文 還是 中文。
如果是中文:將中文緩存起來,並且賦值給全局的i18n對象的locale。完成切換。
如果不是:將英文緩存起來,並且賦值給全局的i18n對象的locale。完成切換。
3、mounted():vue生命周期鈎子掛載完成后,獲取緩存中的語言代碼,綁定給下拉的vue。作用是,根據緩存的語言代碼,設置下拉選項默認顯示什么。
最后:展示成果
本文檔意在與幫助初學者快速構建vue項目,雖沒什么技術含量。但不要隨意轉載,如需轉載保存,請署上 轉載地址。謝謝配合。有問題隨時交流,不怕打擾。