vue使用i18n實(shí)現(xiàn)國際化的方法詳解
一、前言
在4k廣告機(jī)中需要實(shí)現(xiàn)多語言切換,這時候接觸到國際化,前端框架無數(shù),其中幾種熱門的框架都有相匹配的國際化插件工具。比如:
- vue + vue-i18n
- angular + angular-translate
- react + react-intl
- jquery + jquery.i18n.property
在4K廣告機(jī)項(xiàng)目中使用的前端框架為Vue,故而我們將使用vue-i18n這個插件進(jìn)行國際化功能的實(shí)現(xiàn)。
二、最終實(shí)現(xiàn)效果
三、國際化的實(shí)現(xiàn)
1、首先在自己的項(xiàng)目中安裝 vue-i18n依賴包。這里使用NPM進(jìn)行安裝,如果沒有科學(xué)上網(wǎng)請使用CNPM進(jìn)行安裝。
npm install vue-i18n --save-dev
2、將i18n引入vue實(shí)例中,在項(xiàng)目中實(shí)現(xiàn)調(diào)用API和模板語法?,F(xiàn)在在main.js中引入 vue-i18n。
import VueI18n from 'vue-i18n' //引入vue-i18n Vue.use(VueI18n); //通過插件的形式掛載 /*---------基本使用-----------*/ const i18n = new VueI18n({ locale: 'CN', // 語言標(biāo)識 messages : { en: { message: { hello: 'hello world' } }, cn: { message: { hello: '你好、世界' } } } }) /*---------使用語言包-----------*/ const i18n = new VueI18n({ locale: 'zh', // 語言標(biāo)識 //this.$i18n.locale // 通過切換locale的值來實(shí)現(xiàn)語言切換 messages: { 'zh': require('./common/lang/zh'), // 中文語言包 'en': require('./common/lang/en') // 英文語言包 } }) /* eslint-disable no-new */ new Vue({ el: '#app', i18n, //掛載到實(shí)例,一定得在這個位置,而不是comonents中 template: '<App/>', components: { App } });
上面的代碼正式將 vue-i18n 引入 vue 項(xiàng)目中,創(chuàng)建一個 i18n 實(shí)例對象,方便全局調(diào)用。我們通過 this.$i18n.locale 來進(jìn)行語言的切換。
3、接下來我們就需要新建兩個js文件(或者josn文件)作為語言包。
其中en.js語言包中代碼為:
module.exports = { message: { login: 'Login', Username: 'Username', Password: 'Password', Captcha: 'Captcha', Language: 'Language', zh: 'Chinese', en: 'English' } }
其中zh.js語言包中代碼為:
module.exports = { message: { login: '登錄', Username: '用戶名', Password: '密碼', Captcha: '驗(yàn)證碼', Language: '語言', zh: '中文', en: '英文' } }
最后我們只需要通過觸發(fā)事件的形式,來控制 locale 的值,去調(diào)用對應(yīng)的語言包就可以實(shí)現(xiàn)國際化啦。
4、組件中觸發(fā)事件切換 locale 的值,從而實(shí)現(xiàn)語言的切換。template代碼:
<div class="lang"> <el-radio-group v-model="language" size="mini"> <el-radio v-for="item of lang" :label="item.value" border>{{item.label}}</el-radio> </el-radio-group> </div>
scrpit代碼:
import Vue from 'vue' export default { mounted() { this.$i18n.locale === 'zh' ? this.language = 0 : this.language = 1 //數(shù)據(jù)加載時判斷當(dāng)前屬于哪種語言,為其單選按鈕賦值 }, data() { return { language: 0, lang: [{ label: this.$t('message.zh'), //模板語法的一種 value: 0 }, { label: this.$t('message.en'), value: 1 }], } }, watch: { //偵聽器 language: function (val) { //偵聽單選按鈕的變化,從而進(jìn)行切換語言 val === 0 ? this.$i18n.locale = 'zh' : this.$i18n.locale = 'en'; Vue.set(this.lang, 0, {label: this.$t('message.zh'), value: 0}); Vue.set(this.lang, 1, {label: this.$t('message.en'), value: 1}) /** this.lang: [{ label: this.$t('message.zh'), //如果不使用Vue.set,也可以使用更新數(shù)據(jù)的方法 value: 0 }, { label: this.$t('message.en'), value: 1 }] **/ } }, }
注意:由于 JavaScript 的限制,Vue 不能檢測當(dāng)前變動的數(shù)組,只渲染一次,如果數(shù)據(jù)不更新視圖就不更新的組件,如果切換語言則需要更新一下數(shù)據(jù)才能切換組件內(nèi)的多語言。
四、vue-i18n 數(shù)據(jù)渲染的模板語法
模板語法暫時分為三種:
//vue組件模板的使用 <div>{{$t('message.zh')}}</div> //vue組件模板數(shù)據(jù)綁定的使用 <input :placeholder="$t('message.zh')"></input> //vue組件data中賦值的使用 data:{ msg:this.$t('message.zh'); }
五、Element UI組件庫與vue-i18n的兼容問題
由于項(xiàng)目中使用了Element UI組件庫,它里面內(nèi)置的一些文字也是需要國際化,好在Element UI是有國際化的支持。但是Element UI默認(rèn)只兼容vue-i18n的5.x版本,而現(xiàn)在vue-i18n的版本已經(jīng)到了7.x,Element UI官方文檔中“國際化”一節(jié)中對此有具體說明。下面將手動設(shè)置內(nèi)容貼出來:
import Vue from 'vue' import ElementUI from 'element-ui' import VueI18n from 'vue-i18n' import enLocale from 'element-ui/lib/locale/lang/en' //引入Element UI的英文包 import zhLocale from 'element-ui/lib/locale/lang/zh-CN' //引入Element UI的中文包 Vue.use(VueI18n); Vue.use(ElementUI, { i18n: (key, value) => i18n.t(key, value) }); //兼容i18n 7.x版本設(shè)置 const i18n = new VueI18n({ locale: 'zh', // 語言標(biāo)識 messages: { zh: Object.assign(require('@/components/common/lang/zh'), zhLocale), //這里需要注意一下,是如何導(dǎo)入多個語言包的 en: Object.assign(require('@/components/common/lang/en'), enLocale), } });
注意:關(guān)于導(dǎo)入多個語言包時遇到的問題,我是在Element UI 國際化文檔中發(fā)現(xiàn)的解決辦法。
六、路由與面包屑導(dǎo)航國際化的語法問題
在對面包屑導(dǎo)航進(jìn)行國際化時不知道如何進(jìn)行。在網(wǎng)上翻閱了一些資料,得到如下代碼,完美解決問題:
router.js(路由配置文件)
{ path: '/index', name: 'nav.Home', //直接點(diǎn)出對應(yīng)的文字 component: (resolve) => require(['@/components/index'], resolve) }
Breadcrumb.vue(面包屑導(dǎo)航組件)
<div id="Breadcrumb"> <el-breadcrumb separator-class="el-icon-arrow-right"> <el-breadcrumb-item :to="{ path: '/index' }">{{$t('nav.Home')}}</el-breadcrumb-item> /*注意{{$t(item.name)}}*/ <el-breadcrumb-item v-for="item in $route.matched" :to="{ path: item.path}">{{$t(item.name)}}</el-breadcrumb-item> </el-breadcrumb> </div>
七、至此,國際化的坑算是踩完了。
總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,謝謝大家對腳本之家的支持。
相關(guān)文章
Vue單頁面應(yīng)用中實(shí)現(xiàn)Markdown渲染
這篇文章主要介紹了Vue單頁面應(yīng)用中如何實(shí)現(xiàn)Markdown渲染,幫助大家更好的理解和使用vue,感興趣的朋友可以了解下2021-02-02Vue3將虛擬節(jié)點(diǎn)渲染到網(wǎng)頁初次渲染詳解
這篇文章主要為大家介紹了Vue3將虛擬節(jié)點(diǎn)渲染到網(wǎng)頁初次渲染詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-03-03vue Treeselect下拉樹只能選擇第N級元素實(shí)現(xiàn)代碼
這篇文章主要介紹了vue Treeselect下拉樹只能選擇第N級元素實(shí)現(xiàn)代碼,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-08-08