使用 vue-i18n 切換中英文效果
vue-i18n 倉庫地址:https://github.com/kazupon/vue-i18n
兼容性:
支持 Vue.js 2.x 以上版本
安裝方法:(此處只演示 npm)
npm install vue-i18n
使用方法:
1、在 main.js 中引入 vue-i18n (前提是要先引入 vue)
import VueI18n from 'vue-i18n' Vue.use(VueI18n)
2、準(zhǔn)備本地的翻譯信息
const messages = {
zh: {
message: {
hello: '好好學(xué)習(xí),天天向上!'
}
},
en: {
message: {
hello: 'good good study, day day up!'
}
}
}
3、創(chuàng)建帶有選項(xiàng)的 VueI18n 實(shí)例
const i18n = new VueI18n({
locale: 'en', // 語言標(biāo)識(shí)
messages
})
4、把 i18n 掛載到 vue 根實(shí)例上
const app = new Vue({
router,
i18n,
...App
}).$mount('#app')
5、在 HTML 模板中使用
<div id="app">
<h1 style="font-size: 16px; text-align: center;">{{ $t("message.hello") }}</h1>
</div>
查看運(yùn)行效果:

我們剛才選定的語言標(biāo)識(shí)是 “en” 英語,現(xiàn)在改成 “zh” 中文,并查看效果
const i18n = new VueI18n({
locale: 'zh', // 語言標(biāo)識(shí)
messages
})

這樣就可以輕松實(shí)現(xiàn)國(guó)際化了,實(shí)際開發(fā)中,頁面內(nèi)容肯定是很多的,我們可以把對(duì)應(yīng)語言的信息保存為不同的 json對(duì)象
const i18n = new VueI18n({
locale: 'en', // 語言標(biāo)識(shí)
messages: {
'zh': require('./common/lang/zh'),
'en': require('./common/lang/en')
}
})
zh.js
// 注意:一定是 exports,不是 export,否則會(huì)報(bào)錯(cuò),報(bào)錯(cuò)信息是下列的中的內(nèi)容不是 string
module.exports = {
message: {
title: '運(yùn)動(dòng)品牌'
},
placeholder: {
enter: '請(qǐng)輸入您喜歡的品牌'
},
brands: {
nike: '耐克',
adi: '阿迪達(dá)斯',
nb: '新百倫',
ln: '李寧'
}
}
en.js
module.exports = {
message: {
title: 'Sport Brands'
},
placeholder: {
enter: 'Please type in your favorite brand'
},
brands: {
nike: 'Nike',
adi: 'Adidas',
nb: 'New Banlance',
ln: 'LI Ning'
}
}
接下來,在HTML 模板中使用,要特別注意在 js 中的國(guó)際化寫法
// HTML
<div id="app">
<div style="margin: 20px;">
<h1>{{$t("message.title")}}</h1>
<input style="width: 300px;" class="form-control" :placeholder="$t('placeholder.enter')">
<ul>
<li v-for="brand in brands">{{brand}}</li>
</ul>
</div>
</div>
// JS
data () {
return {
brands: [this.$t('brands.nike'), this.$t('brands.adi'), this.$t('brands.nb'), this.$t('brands.ln')]
}
},
查看編譯效果:

現(xiàn)在換成英文的:

在上面的操作中,我們都是通過手動(dòng)修改 locale 的屬性值來切換語言,實(shí)際上,更希望瀏覽器自動(dòng)識(shí)別,這里可以借助于 cookie
1、新建一個(gè) cookie.js 文件,用于讀取cookie
function getCookie(name,defaultValue) {
var arr, reg = new RegExp("(^| )" + name + "=([^;]*)(;|$)");
if (arr = document.cookie.match(reg))
return unescape(arr[2]);
else
return defaultValue;
}
export {
getCookie
}
2、在 main.js 中引入這個(gè)js,并通過 PLAY_LANG 屬性來獲取瀏覽器的語言
const i18n = new VueI18n({
locale: getCookie('PLAY_LANG','zh'), // 語言標(biāo)識(shí)
messages: {
'zh': require('./common/lang/zh'),
'en': require('./common/lang/en')
}
})
這里需要注意兩個(gè)極易出錯(cuò)的地方:
(1)、將 $t() 寫成了 $()

(2)、json 中在同一個(gè)對(duì)象里有同名屬性

vue-i18n 提供了一個(gè)全局配置參數(shù)叫 “l(fā)ocale”,通過改變 locale 的值可以實(shí)現(xiàn)不同語種的切換

下面的案例借用了 Element UI 的彈窗樣式,上面的步驟不再贅述,直接上核心代碼
// template
<h2>{{$t('test')}}</h2>
<button type="button" class="btn btn-success" @click="changeLocale">中文/EN</button>
// js方法
changeLocale () {
this.$confirm(this.$t('layer.toggle'), this.$t('layer.tips'), {
confirmButtonText: this.$t('button.ok'),
cancelButtonText: this.$t('button.cancel'),
type: 'warning'
}).then(() => {
let locale = this.$i18n.locale
locale === 'zh' ? this.$i18n.locale = 'en' : this.$i18n.locale = 'zh'
}).catch(() => {
this.$message({
type: 'info',
})
})
},
效果:

總結(jié)
以上所述是小編給大家介紹的使用 vue-i18n 切換中英文效果,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
相關(guān)文章
vue實(shí)現(xiàn)滾動(dòng)條到頂部或者到指定位置
這篇文章主要介紹了vue實(shí)現(xiàn)滾動(dòng)條到頂部或者到指定位置,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-08-08
Vue.js中使用iView日期選擇器并設(shè)置開始時(shí)間結(jié)束時(shí)間校驗(yàn)功能
本文通過實(shí)例代碼給大家介紹了Vue.js中使用iView日期選擇器并設(shè)置開始時(shí)間結(jié)束時(shí)間校驗(yàn)功能,需要的朋友可以參考下2018-08-08
vue+swiper實(shí)現(xiàn)組件化開發(fā)的實(shí)例代碼
這篇文章主要介紹了vue+swiper實(shí)現(xiàn)組件化開發(fā)的相關(guān)資料,需要的朋友可以參考下2017-10-10
vue中@click和@click.native.prevent的區(qū)別
這篇文章主要介紹了vue中@click和@click.native.prevent的區(qū)別,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-04-04
使用elementUI table展開行內(nèi)嵌套table問題
這篇文章主要介紹了使用elementUI table展開行內(nèi)嵌套table問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-04-04
vue3中<script?setup>?和?setup函數(shù)的區(qū)別對(duì)比
這篇文章主要介紹了vue3中<script?setup>?和?setup函數(shù)的區(qū)別,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-04-04

