亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

組件庫中使用 vue-i18n 國際化的案例詳解

 更新時(shí)間:2023年04月27日 09:26:18   作者:奔跑的狗子  
這篇文章主要介紹了組件庫中使用 vue-i18n 國際化,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下

前言

看了 vue-i18n 以及網(wǎng)上大部分相關(guān)文章,大多都是在講 vue-i18n 在頁面(項(xiàng)目)中的應(yīng)用。至少目前,還沒有看到哪篇文章是講 vue-i18n 在組件庫中的應(yīng)用的,特別是再結(jié)合 element-ui 的國際化。

以下是個(gè)人在封裝的組件庫中使用 vue-i18n 的記錄,也供有相關(guān)需求的同學(xué)參考。

項(xiàng)目中使用 i18n

不贅述,這里只是做個(gè)比較參考,直接抄官方文檔

import Vue from 'vue'
import VueI18n from 'vue-i18n'
Vue.use(VueI18n)
// 準(zhǔn)備翻譯的語言環(huán)境信息
const messages = {
  en: {
    message: {
      hello: 'hello world'
    }
  },
  ja: {
    message: {
      hello: 'こんにちは、世界'
    }
  }
}
// 通過選項(xiàng)創(chuàng)建 VueI18n 實(shí)例
const i18n = new VueI18n({
  locale: 'ja', // 設(shè)置地區(qū)
  messages, // 設(shè)置地區(qū)信息
})
// 通過 `i18n` 選項(xiàng)創(chuàng)建 Vue 實(shí)例
new Vue({ i18n }).$mount('#app')
<template>  
  <div id="app">
    <p>{{ $t("message.hello") }}</p>
  </div>
</template>
復(fù)制代碼

組件庫中使用 i18n

需求

  • 組件庫代碼中配置 i18n ,并在組件中注冊和使用
  • 頁面中若使用 i18n,可以合并和覆蓋組件庫的 i18n 配置
  • element-uii18n 在組件庫中注冊,頁面無需再注冊
  • 頁面切換語言時(shí),組件庫和 element-ui 的語言也一起切換

未添加 i18n 前的組件庫

正常組件庫的入口文件,是一個(gè)很標(biāo)準(zhǔn)的 export ,供頁面進(jìn)行 Vue.use() 調(diào)用。

這個(gè)模塊沒有特殊意義,只是作為基礎(chǔ)代碼展示,以及與下面加入 i18n 的代碼做對(duì)比。

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(),
})
// 對(duì)外暴露的合并配置項(xiàng)的方法
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 = {}; // 具體封裝的組件這里不做贅述
// 傳入一個(gè) options ,為了將 element 的國際化合并方法傳入組件庫
// 經(jīng)實(shí)驗(yàn),element 的國際化合并在頁面觸發(fā)有效,在組件庫中觸發(fā)無效,故此操作
const install = function (Vue, options = {}) {
  Object.keys(components).forEach((key) => {
    Vue.component(key, components[key]);
  });
  // 在頁面使用組件庫,進(jìn)行 Vue.use 的時(shí)候注冊,能保證相同的 Vue 實(shí)例
  // 這句是關(guān)鍵!不能使用 Vue.prototype.$i18n = i18n; 會(huì)報(bào)錯(cuò),報(bào)錯(cuò)原因是與 vue-i18n 內(nèi)部的變量重名,故這里使用 $i18n_ 代替,但這不影響頁面使用 $i18n
  Object.defineProperty(Vue.prototype, '$i18n_', {
    get() {
      // 此 this 為頁面 vue 實(shí)例,若頁面配置了國際化,則使用頁面的實(shí)例,否則用組件庫的國際化
      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, // 導(dǎo)出合并語言配置項(xiàng)的方法
  install,
  ...components
};

組件庫中使用 i18n

因?yàn)槊麊栴},只能使用 this.$i18n_ 而不是 this.$i18n

<div v-bind="$i18n_.t('textPart.textNum')"></div>

項(xiàng)目中使用組件庫和 i18n

import Vue from 'vue';
import web from '../src/index'; // 組件庫,無需再引用 element
// 頁面的國際化配置,同樣需要新建對(duì)應(yīng)的語言文件
const req = require.context('./lang', false, /.js$/);
const localeConfig = req.keys().map((k) => {
  let name = k.match(/./(.+).js/)[1];
  return {
    locale: name, // 需要與組件庫的語言類型一一對(duì)應(yīng)
    message: req(k).default
  };
});
// 使用組件庫暴露的合并配置項(xiàng)方法,獲得新的 i18n 實(shí)例
// 該 i18n 包含了組件庫的 i18n 和 頁面的 i18n 
const i18n = web.i18nLocale(localeConfig, 'zh-CN');
// 注冊組件庫的同時(shí),傳入 element 的國際化合并方法
// 此時(shí),該 i18n 包含了 element、組件庫、頁面 的 i18n
Vue.use(web, {
  i18n: (key, value) => i18n.t(key, value)
});
export default new Vue({
  el: '#app',
  router,
  i18n, // 將 i18n 注冊到頁面 vue 實(shí)例
  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
        // 然后這里還有一個(gè) this.$i18n_ ,是單獨(dú)組件庫的 i18n
        this.$i18n.locale = 'en';
      }
    }
  };

最后

至此,實(shí)現(xiàn)了 vue-i18n 在組件庫中的應(yīng)用,同時(shí)聯(lián)合了 element-ui 實(shí)現(xiàn)了三方 i18n 的展示和切換。

最關(guān)鍵的是 Object.defineProperty 這個(gè)方法,以及 element 國際化的雙層傳參,前者解決了組件庫與頁面 i18n 的統(tǒng)一,后者將 elementi18n 納入整個(gè)體系當(dāng)中。事后回想,可謂精妙。

到此這篇關(guān)于組件庫中使用 vue-i18n 國際化的文章就介紹到這了,更多相關(guān)vue-i18n 國際化內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論