all files / src/ i18n.js

100% Statements 25/25
100% Branches 12/12
100% Functions 7/7
100% Lines 24/24
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63        50×             50× 50× 50×   50×   50× 49×   50× 48×     50×       51× 51× 51× 51× 51× 51×         270×       657×       157×       657×                
import { install, Vue } from './install';
 
export default class VueI18n {
  constructor(i18next, opts = {}) {
    const options = {
      bindI18n: 'languageChanged loaded',
      bindStore: 'added removed',
      loadComponentNamespace: false,
      ...opts
    };
 
    this._vm = null;
    this.i18next = i18next;
    this.options = options;
 
    this.onI18nChanged = this.onI18nChanged.bind(this);
 
    if (options.bindI18n) {
      this.i18next.on(options.bindI18n, this.onI18nChanged);
    }
    if (options.bindStore && this.i18next.store) {
      this.i18next.store.on(options.bindStore, this.onI18nChanged);
    }
 
    this.resetVM({ i18nLoadedAt: new Date() });
  }
 
  resetVM(data) {
    const oldVM = this._vm;
    const { silent } = Vue.config;
    Vue.config.silent = true;
    this._vm = new Vue({ data });
    Vue.config.silent = silent;
    if (oldVM) {
      Vue.nextTick(() => oldVM.$destroy());
    }
  }
 
  get i18nLoadedAt() {
    return this._vm.$data.i18nLoadedAt;
  }
 
  set i18nLoadedAt(date) {
    this._vm.$set(this._vm, 'i18nLoadedAt', date);
  }
 
  t(key, options) {
    return this.i18next.t(key, options);
  }
 
  onI18nChanged() {
    this.i18nLoadedAt = new Date();
  }
}
 
VueI18n.install = install;
VueI18n.version = __VERSION__;
 
/* istanbul ignore if */
if (typeof window !== 'undefined' && window.Vue) {
  window.Vue.use(VueI18n);
}