Skip to content

Commit 11f6d01

Browse files
committed
⭐ new: vue-i18n custom directive for server-side-rendering
1 parent daaa54f commit 11f6d01

File tree

7 files changed

+8462
-2
lines changed

7 files changed

+8462
-2
lines changed

lib/index.js

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
'use strict';
2+
3+
Object.defineProperty(exports, "__esModule", {
4+
value: true
5+
});
6+
exports.directive = directive;
7+
8+
var _util = require('./util');
9+
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); } }
11+
12+
function directive(vnode, dir) {
13+
var _vm$$i18n;
14+
15+
var value = dir.value;
16+
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 {
27+
(0, _util.warn)('not support value type');
28+
return;
29+
}
30+
31+
var vm = vnode.context;
32+
if (!path) {
33+
(0, _util.warn)('required `path` in v-t directive');
34+
return;
35+
}
36+
37+
if (!vm) {
38+
(0, _util.warn)('not exist Vue instance in VNode context');
39+
return;
40+
}
41+
42+
if (!vm.$i18n) {
43+
(0, _util.warn)('not exist VueI18n instance in Vue instance');
44+
return;
45+
}
46+
47+
var params = [];
48+
locale && params.push(locale);
49+
50+
if (args) {
51+
if (Array.isArray(args)) {
52+
params = params.concat(args);
53+
} else if ((0, _util.isPlainObject)(args)) {
54+
params.push(args);
55+
}
56+
}
57+
58+
vnode.children = [vm._v((_vm$$i18n = vm.$i18n).t.apply(_vm$$i18n, [path].concat(_toConsumableArray(params))))];
59+
}

lib/util.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
'use strict';
2+
3+
Object.defineProperty(exports, "__esModule", {
4+
value: true
5+
});
6+
exports.warn = warn;
7+
exports.isPlainObject = isPlainObject;
8+
function warn(msg, err) {
9+
if (typeof console !== 'undefined') {
10+
console.warn('[vue-i18n-extensions] ' + msg);
11+
/* istanbul ignore if */
12+
if (err) {
13+
console.warn(err.stack);
14+
}
15+
}
16+
}
17+
18+
var toString = Object.prototype.toString;
19+
var OBJECT_STRING = '[object Object]';
20+
function isPlainObject(obj) {
21+
return toString.call(obj) === OBJECT_STRING;
22+
}

0 commit comments

Comments
 (0)