組件庫中使用 vue-i18n 國際化的案例詳解
前言
看了 vue-i18n 以及網(wǎng)上大部分相關(guān)文章,大多都是在講 vue-i18n 在頁面(項目)中的應用。至少目前,還沒有看到哪篇文章是講 vue-i18n 在組件庫中的應用的,特別是再結(jié)合 element-ui 的國際化。
以下是個人在封裝的組件庫中使用 vue-i18n 的記錄,也供有相關(guān)需求的同學參考。
項目中使用 i18n
不贅述,這里只是做個比較參考,直接抄官方文檔
import Vue from 'vue'
import VueI18n from 'vue-i18n'
Vue.use(VueI18n)
// 準備翻譯的語言環(huán)境信息
const messages = {
en: {
message: {
hello: 'hello world'
}
},
ja: {
message: {
hello: 'こんにちは、世界'
}
}
}
// 通過選項創(chuàng)建 VueI18n 實例
const i18n = new VueI18n({
locale: 'ja', // 設置地區(qū)
messages, // 設置地區(qū)信息
})
// 通過 `i18n` 選項創(chuàng)建 Vue 實例
new Vue({ i18n }).$mount('#app')<template>
<div id="app">
<p>{{ $t("message.hello") }}</p>
</div>
</template>
復制代碼組件庫中使用 i18n
需求
- 組件庫代碼中配置
i18n,并在組件中注冊和使用 - 頁面中若使用
i18n,可以合并和覆蓋組件庫的i18n配置 element-ui的i18n在組件庫中注冊,頁面無需再注冊- 頁面切換語言時,組件庫和
element-ui的語言也一起切換
未添加 i18n 前的組件庫
正常組件庫的入口文件,是一個很標準的 export ,供頁面進行 Vue.use() 調(diào)用。
這個模塊沒有特殊意義,只是作為基礎(chǔ)代碼展示,以及與下面加入 i18n 的代碼做對比。
const components = {}; // 具體封裝的組件這里不做贅述
const install = function (Vue, options = {}) {
Object.keys(components).forEach((key) => {
Vue.component(key, components[key]);
});
};
/* istanbul ignore if */
if (typeof window !== 'undefined' && window.Vue) {
install(window.Vue);
}
export default {
version,
install,
...components
};組件庫 i18n 的配置文件
先在 ./lang 路徑下新建語言文件,如 en.js、zh-CN.js 等(數(shù)據(jù)格式參考 vue-i18n)
import Vue from 'vue';
import VueI18n from 'vue-i18n';
Vue.use(VueI18n)
// 配置從文件讀取
const req = require.context('./lang', false, /.js$/)
const modules = req.keys().map(k => {
let name = k.match(/./(.+).js/)[1]
return {
name: name,
webLocale: req(k).default,
elementLocale: require(`element-ui/lib/locale/lang/${name}`).default // 加入 element 的 i18n
}
})
// 注冊 i18n
let i18n = new VueI18n({
locale: 'zh-CN',
messages: getMessages(),
})
// 對外暴露的合并配置項的方法
export function i18nLocale(config, lang = 'zh-CN') {
i18n = new VueI18n({
locale: lang,
messages: getMessages(config),
})
return i18n;
}
// 合并方法
function getMessages(config = []) {
return modules.reduce((sum, item) => {
let conf = config.find(m => m.locale === item.name) || {};
let locale = conf.locale || item.name;
sum[locale] = {
...item.elementLocale, // element 的語言配置
...item.webLocale, // 組件庫的語言配置
...conf.message, // 頁面的語言配置
}
return sum;
}, {})
}
export default i18n;組件庫的輸出文件
import i18n, { vueI18nLocale } from './locale';
import element from 'element-ui';
const components = {}; // 具體封裝的組件這里不做贅述
// 傳入一個 options ,為了將 element 的國際化合并方法傳入組件庫
// 經(jīng)實驗,element 的國際化合并在頁面觸發(fā)有效,在組件庫中觸發(fā)無效,故此操作
const install = function (Vue, options = {}) {
Object.keys(components).forEach((key) => {
Vue.component(key, components[key]);
});
// 在頁面使用組件庫,進行 Vue.use 的時候注冊,能保證相同的 Vue 實例
// 這句是關(guān)鍵!不能使用 Vue.prototype.$i18n = i18n; 會報錯,報錯原因是與 vue-i18n 內(nèi)部的變量重名,故這里使用 $i18n_ 代替,但這不影響頁面使用 $i18n
Object.defineProperty(Vue.prototype, '$i18n_', {
get() {
// 此 this 為頁面 vue 實例,若頁面配置了國際化,則使用頁面的實例,否則用組件庫的國際化
return this.$i18n || i18n;
},
configurable: true
});
// 在此注冊 element,并將頁面?zhèn)魅氲膰H化合并方法,繼續(xù)傳入到 element
Vue.use(element, {
i18n: options.i18n || ((key, value) => i18n.t(key, value))
});
};
/* istanbul ignore if */
if (typeof window !== 'undefined' && window.Vue) {
install(window.Vue);
}
export default {
version,
vueI18nLocale, // 導出合并語言配置項的方法
install,
...components
};組件庫中使用 i18n
因為命名問題,只能使用 this.$i18n_ 而不是 this.$i18n
<div v-bind="$i18n_.t('textPart.textNum')"></div>項目中使用組件庫和 i18n
import Vue from 'vue';
import web from '../src/index'; // 組件庫,無需再引用 element
// 頁面的國際化配置,同樣需要新建對應的語言文件
const req = require.context('./lang', false, /.js$/);
const localeConfig = req.keys().map((k) => {
let name = k.match(/./(.+).js/)[1];
return {
locale: name, // 需要與組件庫的語言類型一一對應
message: req(k).default
};
});
// 使用組件庫暴露的合并配置項方法,獲得新的 i18n 實例
// 該 i18n 包含了組件庫的 i18n 和 頁面的 i18n
const i18n = web.i18nLocale(localeConfig, 'zh-CN');
// 注冊組件庫的同時,傳入 element 的國際化合并方法
// 此時,該 i18n 包含了 element、組件庫、頁面 的 i18n
Vue.use(web, {
i18n: (key, value) => i18n.t(key, value)
});
export default new Vue({
el: '#app',
router,
i18n, // 將 i18n 注冊到頁面 vue 實例
components: { App },
template: '<App/>'
});頁面展示和切換 i18n
這里能展示 element、組件庫、頁面 的語言變量,切換語言也是三者一起切換
<template>
<div>
<!-- 兩種寫法返回值相同 -->
<div v-bind="$t('textPart.textNum')"></div>
<div v-bind="$i18n.t('textPart.textNum')"></div>
<div @click="changeI18n">切換語言</div>
</div>
</template>
<script>
export default {
data() {
return {};
},
methods: {
changeI18n() {
// 這里的 this.$i18n 包含了 element、組件庫、頁面 的 i18n
// 然后這里還有一個 this.$i18n_ ,是單獨組件庫的 i18n
this.$i18n.locale = 'en';
}
}
};最后
至此,實現(xiàn)了 vue-i18n 在組件庫中的應用,同時聯(lián)合了 element-ui 實現(xiàn)了三方 i18n 的展示和切換。
最關(guān)鍵的是 Object.defineProperty 這個方法,以及 element 國際化的雙層傳參,前者解決了組件庫與頁面 i18n 的統(tǒng)一,后者將 element 的 i18n 納入整個體系當中。事后回想,可謂精妙。
到此這篇關(guān)于組件庫中使用 vue-i18n 國際化的文章就介紹到這了,更多相關(guān)vue-i18n 國際化內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
基于Vue自定義指令實現(xiàn)按鈕級權(quán)限控制思路詳解
這篇文章主要介紹了基于vue自定義指令實現(xiàn)按鈕級權(quán)限控制,本文給大家介紹的非常詳細,感興趣的朋友跟隨腳本之家小編一起學習吧2018-05-05
vue實現(xiàn)的網(wǎng)易云音樂在線播放和下載功能案例
這篇文章主要介紹了vue實現(xiàn)的網(wǎng)易云音樂在線播放和下載功能,結(jié)合具體實例形式分析了網(wǎng)易云音樂相關(guān)接口調(diào)用與操作技巧,需要的朋友可以參考下2019-02-02
解決vscode進行vue格式化,會自動補分號和雙引號的問題
這篇文章主要介紹了解決vscode進行vue格式化,會自動補分號和雙引號的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-10-10
vue elementui form表單驗證的實現(xiàn)
這篇文章主要介紹了vue elementui form表單驗證的實現(xiàn),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-11-11

