This repository was archived by the owner on May 19, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 43
/
Copy pathinstall.js
144 lines (123 loc) · 3.99 KB
/
install.js
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/* eslint-disable import/no-mutable-exports */
import deepmerge from 'deepmerge';
import component from './component';
import directive from './directive';
import waitDirective from './wait';
export let Vue;
export function install(_Vue) {
if (install.installed) {
return;
}
install.installed = true;
Vue = _Vue;
const getByKey = (i18nOptions, i18nextOptions) => key => {
if (
i18nOptions &&
i18nOptions.keyPrefix &&
!key.includes(i18nextOptions.nsSeparator)
) {
return `${i18nOptions.keyPrefix}.${key}`;
}
return key;
};
const getComponentNamespace = vm => {
const namespace = vm.$options.name || vm.$options._componentTag;
if (namespace) {
return {
namespace,
loadNamespace: true
};
}
return {
namespace: `${Math.random()}`
};
};
Vue.mixin({
beforeCreate() {
const options = this.$options;
if (options.i18n) {
this._i18n = options.i18n;
} else if (options.parent && options.parent.$i18n) {
this._i18n = options.parent.$i18n;
}
let inlineTranslations = {};
if (this._i18n) {
const getNamespace =
this._i18n.options.getComponentNamespace || getComponentNamespace;
const { namespace, loadNamespace } = getNamespace(this);
if (options.__i18n) {
options.__i18n.forEach(resource => {
inlineTranslations = deepmerge(
inlineTranslations,
JSON.parse(resource)
);
});
}
if (options.i18nOptions) {
const {
lng = null,
keyPrefix = null,
messages
} = this.$options.i18nOptions;
let { namespaces } = this.$options.i18nOptions;
namespaces = namespaces || this._i18n.i18next.options.defaultNS;
if (typeof namespaces === 'string') namespaces = [namespaces];
const namespacesToLoad = namespaces.concat([namespace]);
if (messages) {
inlineTranslations = deepmerge(inlineTranslations, messages);
}
this._i18nOptions = { lng, namespaces: namespacesToLoad, keyPrefix };
this._i18n.i18next.loadNamespaces(namespaces);
} else if (options.parent && options.parent._i18nOptions) {
this._i18nOptions = { ...options.parent._i18nOptions };
this._i18nOptions.namespaces = [
namespace,
...this._i18nOptions.namespaces
];
} else if (options.__i18n) {
this._i18nOptions = { namespaces: [namespace] };
}
if (loadNamespace && this._i18n.options.loadComponentNamespace) {
this._i18n.i18next.loadNamespaces([namespace]);
}
const languages = Object.keys(inlineTranslations);
languages.forEach(lang => {
this._i18n.i18next.addResourceBundle(
lang,
namespace,
{ ...inlineTranslations[lang] },
true,
false
);
});
}
const getKey = getByKey(
this._i18nOptions,
this._i18n ? this._i18n.i18next.options : {}
);
if (this._i18nOptions && this._i18nOptions.namespaces) {
const { lng, namespaces } = this._i18nOptions;
const fixedT = this._i18n.i18next.getFixedT(lng, namespaces);
this._getI18nKey = (key, i18nextOptions) =>
fixedT(getKey(key), i18nextOptions, this._i18n.i18nLoadedAt);
} else {
this._getI18nKey = (key, i18nextOptions) =>
this._i18n.t(getKey(key), i18nextOptions, this._i18n.i18nLoadedAt);
}
}
});
// extend Vue.js
if (!Object.prototype.hasOwnProperty.call(Vue.prototype, '$i18n')) {
Object.defineProperty(Vue.prototype, '$i18n', {
get() {
return this._i18n;
}
});
}
Vue.prototype.$t = function t(key, options) {
return this._getI18nKey(key, options);
};
Vue.component(component.name, component);
Vue.directive('t', directive);
Vue.directive('waitForT', waitDirective);
}