Skip to content

Commit 578839c

Browse files
committed
⭐ new: i18n compiler module
1 parent 701e30b commit 578839c

File tree

7 files changed

+282
-8208
lines changed

7 files changed

+282
-8208
lines changed

lib/index.js

Lines changed: 71 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3,32 +3,28 @@
33
Object.defineProperty(exports, "__esModule", {
44
value: true
55
});
6+
exports.module = undefined;
7+
8+
var _stringify = require('babel-runtime/core-js/json/stringify');
9+
10+
var _stringify2 = _interopRequireDefault(_stringify);
11+
612
exports.directive = directive;
713

814
var _util = require('./util');
915

10-
function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } else { return Array.from(arr); } }
16+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
1117

1218
function directive(vnode, dir) {
13-
var _vm$$i18n;
14-
15-
var value = dir.value;
19+
const value = dir.value;
1620

17-
var path = void 0,
18-
locale = void 0,
19-
args = void 0;
20-
if (typeof value === 'string') {
21-
path = value;
22-
} else if ((0, _util.isPlainObject)(value)) {
23-
path = value.path;
24-
locale = value.locale;
25-
args = value.args;
26-
} else {
21+
const { path, locale, args } = parseValue(value);
22+
if (!path && !locale && !args) {
2723
(0, _util.warn)('not support value type');
2824
return;
2925
}
3026

31-
var vm = vnode.context;
27+
const vm = vnode.context;
3228
if (!path) {
3329
(0, _util.warn)('required `path` in v-t directive');
3430
return;
@@ -44,16 +40,68 @@ function directive(vnode, dir) {
4440
return;
4541
}
4642

47-
var params = [];
48-
locale && params.push(locale);
43+
const params = makeParams(locale, args);
44+
vnode.children = [vm._v(vm.$i18n.t(path, ...params))];
45+
}
46+
47+
function _module(i18n) {
48+
return {
49+
transformNode(el) {
50+
const exp = (0, _util.getAttr)(el, 'v-t');
51+
if (!exp) {
52+
return;
53+
}
54+
55+
const { status, value } = (0, _util.evaluateValue)(exp);
56+
if (status === 'ng') {
57+
(0, _util.warn)('pre-localization with v-t support only static params');
58+
return;
59+
}
60+
61+
const { path, locale, args } = parseValue(value);
62+
if (!path && !locale && !args) {
63+
(0, _util.warn)('not support value type');
64+
return;
65+
}
66+
67+
const params = makeParams(locale, args);
68+
el.i18n = i18n.t(path, ...params);
4969

50-
if (args) {
51-
if (Array.isArray(args)) {
52-
params = params.concat(args);
53-
} else if ((0, _util.isPlainObject)(args)) {
54-
params.push(args);
70+
(0, _util.removeAttr)(el, 'v-t');
71+
},
72+
73+
genData(el) {
74+
if (el.i18n) {
75+
(0, _util.addProp)(el, 'textContent', `_s(${(0, _stringify2.default)(el.i18n)})`); // generate via 'domProps'
76+
el.children = []; // clear children, due to be inserted with textContet
77+
}
78+
return '';
5579
}
80+
};
81+
}
82+
83+
exports.module = _module;
84+
function parseValue(value) {
85+
let path, locale, args;
86+
87+
if (typeof value === 'string') {
88+
path = value;
89+
} else if ((0, _util.isPlainObject)(value)) {
90+
path = value.path;
91+
locale = value.locale;
92+
args = value.args;
93+
}
94+
95+
return { path, locale, args };
96+
}
97+
98+
function makeParams(locale, args) {
99+
const params = [];
100+
101+
locale && params.push(locale);
102+
if (args && (Array.isArray(args) || (0, _util.isPlainObject)(args))) {
103+
params.push(args);
56104
}
57105

58-
vnode.children = [vm._v((_vm$$i18n = vm.$i18n).t.apply(_vm$$i18n, [path].concat(_toConsumableArray(params))))];
106+
return params;
59107
}

lib/util.js

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ Object.defineProperty(exports, "__esModule", {
55
});
66
exports.warn = warn;
77
exports.isPlainObject = isPlainObject;
8+
exports.addProp = addProp;
9+
exports.getAttr = getAttr;
10+
exports.removeAttr = removeAttr;
11+
exports.evaluateValue = evaluateValue;
812
function warn(msg, err) {
913
if (typeof console !== 'undefined') {
1014
console.warn('[vue-i18n-extensions] ' + msg);
@@ -15,8 +19,38 @@ function warn(msg, err) {
1519
}
1620
}
1721

18-
var toString = Object.prototype.toString;
19-
var OBJECT_STRING = '[object Object]';
22+
const toString = Object.prototype.toString;
23+
const OBJECT_STRING = '[object Object]';
2024
function isPlainObject(obj) {
2125
return toString.call(obj) === OBJECT_STRING;
26+
}
27+
28+
function addProp(el, name, value) {
29+
(el.props || (el.props = [])).push({ name, value });
30+
}
31+
32+
function getAttr(el, name) {
33+
return el.attrsMap[name];
34+
}
35+
36+
function removeAttr(el, name) {
37+
if (el.attrsMap[name] !== null) {
38+
const list = el.attrsList;
39+
for (let i = 0, l = list.length; i < l; i++) {
40+
if (list[i].name === name) {
41+
list.splice(i, 1);
42+
break;
43+
}
44+
}
45+
}
46+
}
47+
48+
function evaluateValue(expression) {
49+
const ret = { status: 'ng', value: undefined };
50+
try {
51+
const val = new Function('return ' + expression)();
52+
ret.status = 'ok';
53+
ret.value = val;
54+
} catch (e) {}
55+
return ret;
2256
}

0 commit comments

Comments
 (0)