index.js
1.68 KB
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
'use strict';
exports.__esModule = true;
exports.i18n = exports.use = exports.t = undefined;
var _zhCN = require('element-ui/lib/locale/lang/zh-CN');
var _zhCN2 = _interopRequireDefault(_zhCN);
var _vue = require('vue');
var _vue2 = _interopRequireDefault(_vue);
var _deepmerge = require('deepmerge');
var _deepmerge2 = _interopRequireDefault(_deepmerge);
var _format = require('./format');
var _format2 = _interopRequireDefault(_format);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
var format = (0, _format2.default)(_vue2.default);
var lang = _zhCN2.default;
var merged = false;
var i18nHandler = function i18nHandler() {
var vuei18n = Object.getPrototypeOf(this || _vue2.default).$t;
if (typeof vuei18n === 'function' && !!_vue2.default.locale) {
if (!merged) {
merged = true;
_vue2.default.locale(_vue2.default.config.lang, (0, _deepmerge2.default)(lang, _vue2.default.locale(_vue2.default.config.lang) || {}, { clone: true }));
}
return vuei18n.apply(this, arguments);
}
};
var t = exports.t = function t(path, options) {
var value = i18nHandler.apply(this, arguments);
if (value !== null && value !== undefined) return value;
var array = path.split('.');
var current = lang;
for (var i = 0, j = array.length; i < j; i++) {
var property = array[i];
value = current[property];
if (i === j - 1) return format(value, options);
if (!value) return '';
current = value;
}
return '';
};
var use = exports.use = function use(l) {
lang = l || lang;
};
var i18n = exports.i18n = function i18n(fn) {
i18nHandler = fn || i18nHandler;
};
exports.default = { use: use, t: t, i18n: i18n };