1.安裝vue-i18n
2.在main.js里面引用
import VueI18n from 'vue-i18n'Vue.use(VueI18n)
3.實例化i18n,并配置默認的語言模式,以及對應的文件(也是在main.js里使用)
如下。cn 中文包對應的是cn.js
en 對應的是英文 en.js 包
const i18n = new VueI18n({ //定義默認語言 locale: 'cn',  messages:{  'cn': require('./common/lang/cn'),  'en': require('./common/lang/en') }})4.cn.js 怎么寫?
module.exports = {  placeholder: {    phone: '手機號',    input_code: '輸入驗證碼',    passwordSix: '請輸入6到18位密碼'  },  sidebar: {    MyAccount: '賬戶信息',    PersonalInformation: '個人信息',    Message: '我的消息',    MyWallet: '我的錢包',    MyProject: '我的方案'  },  home: {    SendCode: 'Send verification code success'    }}當然 en.js 也需要配置一份
module.exports = {  placeholder: {    phone: 'Phone Number',    input_code: 'Verification code',    passwordSix: 'Please enter 6 to 18 Bit passwords'  },  sidebar: {    MyAccount: 'My Account',    PersonalInformation: 'Personal Information',    Message: 'Message',    MyWallet: 'My Wallet',    MyProject: 'My Project'  },  home: {    SendCode: 'send Code Success功'    }}5.如何在template中使用?
需要這樣渲染出來
{{ $t("sidebar.MyWallet") }}<li>{{ $t("sidebar.MyWallet") }}</li>當然placeholder也是可以通過他來更改的。
<input type="text" v-model="phoneNumber" :placeholder="$t('placeholder.phone')"> 對應好配置好的placeholder就行。
中/English 切換函數
tag () {  if (this.$i18n.locale === 'en') {    this.$i18n.locale = 'cn'  } else {    this.$i18n.locale = 'en'  }  }在js里如何拿配置過的語言來使用?
this.$t("sidebar.MyAccount")這里我們使用了mint-ui框架中的Toast消息提示框,想讓它根據語言環境來顯示不同的提示語。
雙語言前
Toast({message: '驗證碼發送成功'})更改為雙語言后
Toast({message: this.$t("home.SendCode")})總結
以上所述是小編給大家介紹的vue 國際化 vue-i18n 雙語言 語言包,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對錯新站長站網站的支持!
 
  | 
新聞熱點
疑難解答
圖片精選