@@ -37,7 +37,12 @@ type VTDirectiveValue = {
3737 *
3838 * @public
3939 */
40- export interface TransformVTDirectiveOptions {
40+ export interface TransformVTDirectiveOptions <
41+ Messages = { } ,
42+ DateTimeFormats = { } ,
43+ NumberFormats = { } ,
44+ Legacy extends boolean = true
45+ > {
4146 /**
4247 * I18n instance
4348 *
@@ -46,18 +51,27 @@ export interface TransformVTDirectiveOptions {
4651 * The translation will use the global resources registered in the I18n instance,
4752 * that is, `v-t` diretive transform is also a limitation that the resources of each component cannot be used.
4853 */
49- i18n ?: I18n
54+ i18n ?: I18n < Messages , DateTimeFormats , NumberFormats , Legacy >
5055 /**
5156 * I18n Mode
5257 *
5358 * @remarks
5459 * Specify the API style of vue-i18n. If you use legacy API style (e.g. `$t`) at vue-i18n, you need to specify `legacy`.
5560 *
56- * @default 'composable '
61+ * @default 'composition '
5762 */
5863 mode ?: I18nMode
5964}
6065
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+
6175/**
6276 * Transform `v-t` custom directive
6377 *
@@ -106,14 +120,24 @@ export interface TransformVTDirectiveOptions {
106120 * ```
107121 * @public
108122 */
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+ > = { }
111135) : DirectiveTransform {
112136 const i18nInstance = options . i18n
113137 const mode =
114- isString ( options . mode ) && [ 'composable ' , 'legacy' ] . includes ( options . mode )
138+ isString ( options . mode ) && [ 'composition ' , 'legacy' ] . includes ( options . mode )
115139 ? options . mode
116- : 'composable '
140+ : 'composition '
117141
118142 return ( dir , node , context ) => {
119143 const { exp, loc } = dir
@@ -150,7 +174,7 @@ export function transformVTDirective(
150174 }
151175
152176 if ( isSimpleExpressionNode ( exp ) ) {
153- if ( exp . isConstant && i18nInstance ) {
177+ if ( isConstant ( exp ) && i18nInstance ) {
154178 const { status, value } = evaluateValue ( exp . content )
155179 if ( status === 'ng' ) {
156180 report ( ReportCodes . FAILED_VALUE_EVALUATION , {
@@ -166,7 +190,12 @@ export function transformVTDirective(
166190 return { props : [ ] }
167191 }
168192
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+
170199 node . children . push ( {
171200 type : NodeTypes . TEXT ,
172201 content
@@ -179,7 +208,13 @@ export function transformVTDirective(
179208 node . children . push ( {
180209 type : NodeTypes . INTERPOLATION ,
181210 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+ )
183218 ] )
184219 } as InterpolationNode )
185220 return { props : [ ] }
@@ -195,7 +230,13 @@ export function transformVTDirective(
195230 node . children . push ( {
196231 type : NodeTypes . INTERPOLATION ,
197232 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+ )
199240 ] )
200241 } as InterpolationNode )
201242 return { props : [ ] }
@@ -221,6 +262,19 @@ function isCompoundExpressionNode(
221262 return node != null && node . type === NodeTypes . COMPOUND_EXPRESSION
222263}
223264
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+
224278function mapNodeContentHanlder (
225279 value :
226280 | string
@@ -292,7 +346,7 @@ function generateTranslationCode(
292346 mode : I18nMode ,
293347 params : TranslationParams
294348) : string {
295- return mode === 'composable '
349+ return mode === 'composition '
296350 ? generateComposableCode ( context , params )
297351 : generateLegacyCode ( context , params )
298352}
0 commit comments