@@ -37,7 +37,12 @@ type VTDirectiveValue = {
37
37
*
38
38
* @public
39
39
*/
40
- export interface TransformVTDirectiveOptions {
40
+ export interface TransformVTDirectiveOptions <
41
+ Messages = { } ,
42
+ DateTimeFormats = { } ,
43
+ NumberFormats = { } ,
44
+ Legacy extends boolean = true
45
+ > {
41
46
/**
42
47
* I18n instance
43
48
*
@@ -46,18 +51,27 @@ export interface TransformVTDirectiveOptions {
46
51
* The translation will use the global resources registered in the I18n instance,
47
52
* that is, `v-t` diretive transform is also a limitation that the resources of each component cannot be used.
48
53
*/
49
- i18n ?: I18n
54
+ i18n ?: I18n < Messages , DateTimeFormats , NumberFormats , Legacy >
50
55
/**
51
56
* I18n Mode
52
57
*
53
58
* @remarks
54
59
* Specify the API style of vue-i18n. If you use legacy API style (e.g. `$t`) at vue-i18n, you need to specify `legacy`.
55
60
*
56
- * @default 'composable '
61
+ * @default 'composition '
57
62
*/
58
63
mode ?: I18nMode
59
64
}
60
65
66
+ // compatibility for this commit(v3.0.3)
67
+ // https://github.com/vuejs/vue-next/commit/90bdf59f4c84ec0af9bab402c37090d82806cfc1
68
+ const enum ConstantTypes {
69
+ NOT_CONSTANT = 0 ,
70
+ CAN_SKIP_PATCH ,
71
+ CAN_HOIST ,
72
+ CAN_STRINGIFY
73
+ }
74
+
61
75
/**
62
76
* Transform `v-t` custom directive
63
77
*
@@ -106,14 +120,24 @@ export interface TransformVTDirectiveOptions {
106
120
* ```
107
121
* @public
108
122
*/
109
- export function transformVTDirective (
110
- options : TransformVTDirectiveOptions = { }
123
+ export function transformVTDirective <
124
+ Messages = { } ,
125
+ DateTimeFormats = { } ,
126
+ NumberFormats = { } ,
127
+ Legacy extends boolean = true
128
+ > (
129
+ options : TransformVTDirectiveOptions <
130
+ Messages ,
131
+ DateTimeFormats ,
132
+ NumberFormats ,
133
+ Legacy
134
+ > = { }
111
135
) : DirectiveTransform {
112
136
const i18nInstance = options . i18n
113
137
const mode =
114
- isString ( options . mode ) && [ 'composable ' , 'legacy' ] . includes ( options . mode )
138
+ isString ( options . mode ) && [ 'composition ' , 'legacy' ] . includes ( options . mode )
115
139
? options . mode
116
- : 'composable '
140
+ : 'composition '
117
141
118
142
return ( dir , node , context ) => {
119
143
const { exp, loc } = dir
@@ -150,7 +174,7 @@ export function transformVTDirective(
150
174
}
151
175
152
176
if ( isSimpleExpressionNode ( exp ) ) {
153
- if ( exp . isConstant && i18nInstance ) {
177
+ if ( isConstant ( exp ) && i18nInstance ) {
154
178
const { status, value } = evaluateValue ( exp . content )
155
179
if ( status === 'ng' ) {
156
180
report ( ReportCodes . FAILED_VALUE_EVALUATION , {
@@ -166,7 +190,12 @@ export function transformVTDirective(
166
190
return { props : [ ] }
167
191
}
168
192
169
- const content = i18nInstance . global . t ( ...makeParams ( parsedValue ! ) )
193
+ const global =
194
+ i18nInstance . mode === 'composition'
195
+ ? ( i18nInstance . global as any ) // eslint-disable-line @typescript-eslint/no-explicit-any
196
+ : ( i18nInstance . global as any ) . __composer // eslint-disable-line @typescript-eslint/no-explicit-any
197
+ const content = global . t ( ...makeParams ( parsedValue ! ) )
198
+
170
199
node . children . push ( {
171
200
type : NodeTypes . TEXT ,
172
201
content
@@ -179,7 +208,13 @@ export function transformVTDirective(
179
208
node . children . push ( {
180
209
type : NodeTypes . INTERPOLATION ,
181
210
content : createCompoundExpression ( [
182
- createSimpleExpression ( code , false , loc )
211
+ createSimpleExpression (
212
+ code ,
213
+ false ,
214
+ loc ,
215
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
216
+ ConstantTypes . NOT_CONSTANT as any
217
+ )
183
218
] )
184
219
} as InterpolationNode )
185
220
return { props : [ ] }
@@ -195,7 +230,13 @@ export function transformVTDirective(
195
230
node . children . push ( {
196
231
type : NodeTypes . INTERPOLATION ,
197
232
content : createCompoundExpression ( [
198
- createSimpleExpression ( code , false , loc )
233
+ createSimpleExpression (
234
+ code ,
235
+ false ,
236
+ loc ,
237
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
238
+ ConstantTypes . NOT_CONSTANT as any
239
+ )
199
240
] )
200
241
} as InterpolationNode )
201
242
return { props : [ ] }
@@ -221,6 +262,19 @@ function isCompoundExpressionNode(
221
262
return node != null && node . type === NodeTypes . COMPOUND_EXPRESSION
222
263
}
223
264
265
+ function isConstant ( node : SimpleExpressionNode ) : boolean {
266
+ if ( 'isConstant' in node ) {
267
+ // for v3.0.3 earlier
268
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
269
+ return ( node as any ) . isConstant
270
+ } else if ( 'constType' in node ) {
271
+ // for v3.0.3 or later
272
+ return ( node . constType as number ) <= ConstantTypes . CAN_STRINGIFY
273
+ } else {
274
+ throw Error ( 'unexpected error' )
275
+ }
276
+ }
277
+
224
278
function mapNodeContentHanlder (
225
279
value :
226
280
| string
@@ -292,7 +346,7 @@ function generateTranslationCode(
292
346
mode : I18nMode ,
293
347
params : TranslationParams
294
348
) : string {
295
- return mode === 'composable '
349
+ return mode === 'composition '
296
350
? generateComposableCode ( context , params )
297
351
: generateLegacyCode ( context , params )
298
352
}
0 commit comments