3
3
Object . defineProperty ( exports , "__esModule" , {
4
4
value : true
5
5
} ) ;
6
+ exports . module = undefined ;
7
+
8
+ var _stringify = require ( 'babel-runtime/core-js/json/stringify' ) ;
9
+
10
+ var _stringify2 = _interopRequireDefault ( _stringify ) ;
11
+
6
12
exports . directive = directive ;
7
13
8
14
var _util = require ( './util' ) ;
9
15
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 } ; }
11
17
12
18
function directive ( vnode , dir ) {
13
- var _vm$$i18n ;
14
-
15
- var value = dir . value ;
19
+ const value = dir . value ;
16
20
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 ) {
27
23
( 0 , _util . warn ) ( 'not support value type' ) ;
28
24
return ;
29
25
}
30
26
31
- var vm = vnode . context ;
27
+ const vm = vnode . context ;
32
28
if ( ! path ) {
33
29
( 0 , _util . warn ) ( 'required `path` in v-t directive' ) ;
34
30
return ;
@@ -44,16 +40,68 @@ function directive(vnode, dir) {
44
40
return ;
45
41
}
46
42
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 ) ;
49
69
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 '' ;
55
79
}
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 ) ;
56
104
}
57
105
58
- vnode . children = [ vm . _v ( ( _vm$$i18n = vm . $i18n ) . t . apply ( _vm$$i18n , [ path ] . concat ( _toConsumableArray ( params ) ) ) ) ] ;
106
+ return params ;
59
107
}
0 commit comments