@intlify/vue-i18n-extensions 9.0.1 → 9.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +2 -4
- package/dist/index.cjs +374 -421
- package/dist/index.d.cts +39 -41
- package/dist/index.d.mts +39 -41
- package/dist/index.mjs +373 -419
- package/package.json +49 -68
- package/dist/index.d.ts +0 -131
package/dist/index.mjs
CHANGED
|
@@ -1,475 +1,429 @@
|
|
|
1
|
-
import { NodeTypes, TO_DISPLAY_STRING, createCompoundExpression, createSimpleExpression, isText
|
|
2
|
-
import {
|
|
3
|
-
import { parse } from
|
|
4
|
-
|
|
1
|
+
import { BindingTypes, NodeTypes, TO_DISPLAY_STRING, createCompoundExpression, createSimpleExpression, isText } from "@vue/compiler-dom";
|
|
2
|
+
import { format, hasOwn, isArray, isFunction, isNumber, isObject, isString, isSymbol, toDisplayString } from "@intlify/shared";
|
|
3
|
+
import { parse } from "@babel/parser";
|
|
4
|
+
// src/transpiler.ts
|
|
5
5
|
function evaluateValue(expression) {
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
6
|
+
const ret = {
|
|
7
|
+
status: "ng",
|
|
8
|
+
value: void 0
|
|
9
|
+
};
|
|
10
|
+
try {
|
|
11
|
+
const ast = parse(`const a = ${expression.trim()}`);
|
|
12
|
+
if (ast.program.body.length >= 1 && ast.program.body[0].type === "VariableDeclaration" && ast.program.body[0].declarations.length >= 1) {
|
|
13
|
+
const node = ast.program.body[0].declarations[0].init;
|
|
14
|
+
if (node != null && (node.type === "StringLiteral" || node.type === "ObjectExpression")) {
|
|
15
|
+
const val = new Function(`return ${expression.trim()} `)();
|
|
16
|
+
ret.status = "ok";
|
|
17
|
+
ret.value = val;
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
} catch (_e) {}
|
|
21
|
+
return ret;
|
|
20
22
|
}
|
|
21
23
|
function parseVTExpression(expression) {
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
traverseObjectMember(propValueNode, ret.named);
|
|
60
|
-
}
|
|
61
|
-
break;
|
|
62
|
-
default:
|
|
63
|
-
break;
|
|
64
|
-
}
|
|
65
|
-
}
|
|
66
|
-
});
|
|
67
|
-
}
|
|
68
|
-
}
|
|
69
|
-
}
|
|
70
|
-
} catch (_e) {
|
|
71
|
-
}
|
|
72
|
-
return ret;
|
|
24
|
+
const ret = {
|
|
25
|
+
path: "",
|
|
26
|
+
named: {},
|
|
27
|
+
options: {}
|
|
28
|
+
};
|
|
29
|
+
try {
|
|
30
|
+
const ast = parse(`const a = ${expression.trim()}`);
|
|
31
|
+
if (ast.program.body.length >= 1 && ast.program.body[0].type === "VariableDeclaration" && ast.program.body[0].declarations.length >= 1) {
|
|
32
|
+
const node = ast.program.body[0].declarations[0].init;
|
|
33
|
+
if (node != null) {
|
|
34
|
+
if (node.type === "StringLiteral" && node.extra) ret.path = node.extra.raw;
|
|
35
|
+
else if (node.type === "Identifier") ret.path = node.name;
|
|
36
|
+
else if (node.type === "MemberExpression") ret.path = getObjectMemberValue(node);
|
|
37
|
+
else if (node.type === "ObjectExpression") node.properties.forEach((propNode) => {
|
|
38
|
+
if (propNode.type === "ObjectProperty") {
|
|
39
|
+
const propKeyNode = propNode.key;
|
|
40
|
+
if (propKeyNode.type !== "Identifier") return;
|
|
41
|
+
const propValueNode = propNode.value;
|
|
42
|
+
switch (propKeyNode.name) {
|
|
43
|
+
case "path":
|
|
44
|
+
ret.path = getObjectMemberValue(propValueNode);
|
|
45
|
+
break;
|
|
46
|
+
case "locale":
|
|
47
|
+
ret.options.locale = getObjectMemberValue(propValueNode);
|
|
48
|
+
break;
|
|
49
|
+
case "choice":
|
|
50
|
+
case "plural":
|
|
51
|
+
ret.options.plural = getObjectMemberValue(propValueNode);
|
|
52
|
+
break;
|
|
53
|
+
case "args": if (propValueNode.type === "ObjectExpression") traverseObjectMember(propValueNode, ret.named);
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
});
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
} catch (_e) {}
|
|
60
|
+
return ret;
|
|
73
61
|
}
|
|
74
62
|
function getObjectMemberValue(node) {
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
return paths.join(".");
|
|
84
|
-
} else {
|
|
85
|
-
return "";
|
|
86
|
-
}
|
|
63
|
+
if ((node.type === "StringLiteral" || node.type === "NumericLiteral") && node.extra) return node.extra.raw;
|
|
64
|
+
else if (node.type === "Identifier") return node.name;
|
|
65
|
+
else if (node.type === "MemberExpression") {
|
|
66
|
+
const paths = [];
|
|
67
|
+
collectMemberPath(node, paths);
|
|
68
|
+
paths.reverse();
|
|
69
|
+
return paths.join(".");
|
|
70
|
+
} else return "";
|
|
87
71
|
}
|
|
88
72
|
function traverseObjectMember(node, target) {
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
if (propValueNode.type === "ObjectExpression") {
|
|
100
|
-
traverseObjectMember(propValueNode, target[propKeyNode.name]);
|
|
101
|
-
} else {
|
|
102
|
-
target[propKeyNode.name] = getObjectMemberValue(propValueNode);
|
|
103
|
-
}
|
|
104
|
-
}
|
|
105
|
-
});
|
|
73
|
+
node.properties.forEach((propNode) => {
|
|
74
|
+
if (propNode.type === "ObjectProperty") {
|
|
75
|
+
const propKeyNode = propNode.key;
|
|
76
|
+
if (propKeyNode.type !== "Identifier") return;
|
|
77
|
+
if (!(propKeyNode.name in target)) target[propKeyNode.name] = {};
|
|
78
|
+
const propValueNode = propNode.value;
|
|
79
|
+
if (propValueNode.type === "ObjectExpression") traverseObjectMember(propValueNode, target[propKeyNode.name]);
|
|
80
|
+
else target[propKeyNode.name] = getObjectMemberValue(propValueNode);
|
|
81
|
+
}
|
|
82
|
+
});
|
|
106
83
|
}
|
|
107
84
|
function collectMemberPath(node, paths) {
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
85
|
+
if (node.type === "Identifier") {
|
|
86
|
+
paths.push(node.name);
|
|
87
|
+
return;
|
|
88
|
+
} else if (node.type === "MemberExpression" && node.property.type === "Identifier") {
|
|
89
|
+
paths.push(node.property.name);
|
|
90
|
+
return collectMemberPath(node.object, paths);
|
|
91
|
+
}
|
|
115
92
|
}
|
|
116
|
-
|
|
117
|
-
var ReportCodes = /* @__PURE__ */ ((ReportCodes2) => {
|
|
118
|
-
ReportCodes2[ReportCodes2["SUCCESS"] = 0] = "SUCCESS";
|
|
119
|
-
ReportCodes2[ReportCodes2["UNEXPECTED_DIRECTIVE_EXPRESSION"] = 1] = "UNEXPECTED_DIRECTIVE_EXPRESSION";
|
|
120
|
-
ReportCodes2[ReportCodes2["NOT_SUPPORTED"] = 2] = "NOT_SUPPORTED";
|
|
121
|
-
ReportCodes2[ReportCodes2["FAILED_VALUE_EVALUATION"] = 3] = "FAILED_VALUE_EVALUATION";
|
|
122
|
-
ReportCodes2[ReportCodes2["REQUIRED_PARAMETER"] = 4] = "REQUIRED_PARAMETER";
|
|
123
|
-
ReportCodes2[ReportCodes2["INVALID_PARAMETER_TYPE"] = 5] = "INVALID_PARAMETER_TYPE";
|
|
124
|
-
ReportCodes2[ReportCodes2["OVERRIDE_ELEMENT_CHILDREN"] = 6] = "OVERRIDE_ELEMENT_CHILDREN";
|
|
125
|
-
ReportCodes2[ReportCodes2["NOT_RESOLVED_COMPOSER"] = 7] = "NOT_RESOLVED_COMPOSER";
|
|
126
|
-
ReportCodes2[ReportCodes2["UNEXPECTED_ERROR"] = 8] = "UNEXPECTED_ERROR";
|
|
127
|
-
ReportCodes2[ReportCodes2["__EXTEND_POINT__"] = 9] = "__EXTEND_POINT__";
|
|
128
|
-
return ReportCodes2;
|
|
129
|
-
})(ReportCodes || {});
|
|
93
|
+
// src/report.ts
|
|
130
94
|
const ReportMessages = {
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
95
|
+
[1]: `Unexpected directive expression: {0}`,
|
|
96
|
+
[2]: `Not supported transform: {0}`,
|
|
97
|
+
[3]: `Failed value evaluation: {0}`,
|
|
98
|
+
[4]: `Required parameter: {0}`,
|
|
99
|
+
[5]: `Required parameter: {0}`,
|
|
100
|
+
[6]: `v-t will override element children: {0}`,
|
|
101
|
+
[7]: `Not resolved vue-i18n composer: {0}`,
|
|
102
|
+
[8]: `Unexpected error: {0}`
|
|
139
103
|
};
|
|
140
104
|
function getReportMessage(code, ...args) {
|
|
141
|
-
|
|
105
|
+
return format(ReportMessages[code], ...args);
|
|
142
106
|
}
|
|
143
107
|
function createExtensionsError(code, msg, loc) {
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
}
|
|
149
|
-
return error;
|
|
108
|
+
const error = new SyntaxError(msg);
|
|
109
|
+
error.code = code;
|
|
110
|
+
if (loc) error.loc = loc;
|
|
111
|
+
return error;
|
|
150
112
|
}
|
|
151
113
|
function report(code, options = {}) {
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
} else {
|
|
157
|
-
throw createExtensionsError(code, msg, options.loc);
|
|
158
|
-
}
|
|
114
|
+
const mode = options.mode && isString(options.mode) && ["warn", "error"].includes(options.mode) ? options.mode : "warn";
|
|
115
|
+
const msg = getReportMessage(code, options.args);
|
|
116
|
+
if (mode === "warn") console.warn("[vue-i18n-extensions] " + msg);
|
|
117
|
+
else throw createExtensionsError(code, msg, options.loc);
|
|
159
118
|
}
|
|
160
|
-
|
|
119
|
+
// src/builder.ts
|
|
120
|
+
/**
|
|
121
|
+
* Create a Content Builder
|
|
122
|
+
*
|
|
123
|
+
* @param options - Content Builder options
|
|
124
|
+
*
|
|
125
|
+
* @returns A {@link ContentBuilder} instance
|
|
126
|
+
*
|
|
127
|
+
* @public
|
|
128
|
+
*/
|
|
161
129
|
function createContentBuilder(options = {}) {
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
push,
|
|
199
|
-
newline,
|
|
200
|
-
pushline,
|
|
201
|
-
indent,
|
|
202
|
-
deindent
|
|
203
|
-
};
|
|
130
|
+
let _indentLevel = options.indentLevel != null && isNumber(options.indentLevel) ? options.indentLevel : 0;
|
|
131
|
+
let _content = "";
|
|
132
|
+
function push(content) {
|
|
133
|
+
_content += content;
|
|
134
|
+
}
|
|
135
|
+
function _newline(n) {
|
|
136
|
+
push("\n" + ` `.repeat(n));
|
|
137
|
+
}
|
|
138
|
+
function newline() {
|
|
139
|
+
_newline(_indentLevel);
|
|
140
|
+
}
|
|
141
|
+
function pushline(content) {
|
|
142
|
+
push(content);
|
|
143
|
+
newline();
|
|
144
|
+
}
|
|
145
|
+
function indent(withoutNewLine) {
|
|
146
|
+
if (withoutNewLine) ++_indentLevel;
|
|
147
|
+
else _newline(++_indentLevel);
|
|
148
|
+
}
|
|
149
|
+
function deindent(withoutNewLine) {
|
|
150
|
+
if (withoutNewLine) --_indentLevel;
|
|
151
|
+
else _newline(--_indentLevel);
|
|
152
|
+
}
|
|
153
|
+
return {
|
|
154
|
+
get indentLevel() {
|
|
155
|
+
return _indentLevel;
|
|
156
|
+
},
|
|
157
|
+
get content() {
|
|
158
|
+
return String(_content);
|
|
159
|
+
},
|
|
160
|
+
push,
|
|
161
|
+
newline,
|
|
162
|
+
pushline,
|
|
163
|
+
indent,
|
|
164
|
+
deindent
|
|
165
|
+
};
|
|
204
166
|
}
|
|
205
|
-
|
|
167
|
+
// src/transform.ts
|
|
206
168
|
const GLOBAL_TRANSLATE_SIGNATURE = "$t";
|
|
169
|
+
/**
|
|
170
|
+
* Transform `v-t` custom directive
|
|
171
|
+
*
|
|
172
|
+
* @remarks
|
|
173
|
+
* Transform that `v-t` custom directive is optimized vue-i18n code by Vue compiler.
|
|
174
|
+
* This transform can improve the performance by pre-translating, and it does support SSR.
|
|
175
|
+
*
|
|
176
|
+
* @param options - `v-t` custom directive transform options, see {@link TransformVTDirectiveOptions}
|
|
177
|
+
* @returns Directive transform
|
|
178
|
+
*
|
|
179
|
+
* @example
|
|
180
|
+
* ```js
|
|
181
|
+
* import { compile } from '@vue/compiler-dom'
|
|
182
|
+
* import { createI18n } from 'vue-i18n'
|
|
183
|
+
* import { transformVTDirective } from '@intlify/vue-i18n-extensions'
|
|
184
|
+
*
|
|
185
|
+
* // create i18n instance
|
|
186
|
+
* const i18n = createI18n({
|
|
187
|
+
* locale: 'ja',
|
|
188
|
+
* messages: {
|
|
189
|
+
* en: {
|
|
190
|
+
* hello: 'hello'
|
|
191
|
+
* },
|
|
192
|
+
* ja: {
|
|
193
|
+
* hello: 'こんにちは'
|
|
194
|
+
* }
|
|
195
|
+
* }
|
|
196
|
+
* })
|
|
197
|
+
*
|
|
198
|
+
* // get transform from `transformVTDirective` function, with `i18n` option
|
|
199
|
+
* const transformVT = transformVTDirective({ i18n })
|
|
200
|
+
*
|
|
201
|
+
* const { code } = compile(`<p v-t="'hello'"></p>`, {
|
|
202
|
+
* mode: 'function',
|
|
203
|
+
* hoistStatic: true,
|
|
204
|
+
* prefixIdentifiers: true,
|
|
205
|
+
* directiveTransforms: { t: transformVT } // <- you need to specify to `directiveTransforms` option!
|
|
206
|
+
* })
|
|
207
|
+
*
|
|
208
|
+
* console.log(code)
|
|
209
|
+
* // output ->
|
|
210
|
+
* // const { createVNode: _createVNode, openBlock: _openBlock, createBlock: _createBlock } = Vue
|
|
211
|
+
* // return function render(_ctx, _cache) {
|
|
212
|
+
* // return (_openBlock(), _createBlock(\\"div\\", null, \\"こんにちは!\\"))
|
|
213
|
+
* // }
|
|
214
|
+
* ```
|
|
215
|
+
* @public
|
|
216
|
+
*/
|
|
207
217
|
function transformVTDirective(options = {}) {
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
content: createCompoundExpression([
|
|
294
|
-
createSimpleExpression(code, false, loc, 0 /* NOT_CONSTANT */)
|
|
295
|
-
])
|
|
296
|
-
});
|
|
297
|
-
return { props: [] };
|
|
298
|
-
} else {
|
|
299
|
-
report(ReportCodes.NOT_SUPPORTED, {
|
|
300
|
-
args: [node.loc.source || ""],
|
|
301
|
-
loc: node.loc
|
|
302
|
-
});
|
|
303
|
-
return { props: [] };
|
|
304
|
-
}
|
|
305
|
-
};
|
|
218
|
+
const i18nInstance = options.i18n;
|
|
219
|
+
const mode = isString(options.mode) && ["composition", "legacy"].includes(options.mode) ? options.mode : "composition";
|
|
220
|
+
const translationSignatures = isString(options.translationSignatures) || isFunction(options.translationSignatures) ? [options.translationSignatures] : isArray(options.translationSignatures) ? options.translationSignatures : ["t"];
|
|
221
|
+
translationSignatures.push(GLOBAL_TRANSLATE_SIGNATURE);
|
|
222
|
+
return (dir, node, context) => {
|
|
223
|
+
const { exp, loc } = dir;
|
|
224
|
+
if (!exp) report(1, {
|
|
225
|
+
mode: "error",
|
|
226
|
+
args: [node.loc.source || ""],
|
|
227
|
+
loc: node.loc
|
|
228
|
+
});
|
|
229
|
+
if (node.children.length > 0) {
|
|
230
|
+
report(6, {
|
|
231
|
+
mode: "error",
|
|
232
|
+
args: [node.loc.source || ""],
|
|
233
|
+
loc: node.loc
|
|
234
|
+
});
|
|
235
|
+
node.children.length = 0;
|
|
236
|
+
}
|
|
237
|
+
if (isSimpleExpressionNode(exp)) {
|
|
238
|
+
if (isConstant(exp) && i18nInstance) {
|
|
239
|
+
const { status, value } = evaluateValue(exp.content);
|
|
240
|
+
if (status === "ng") {
|
|
241
|
+
report(3, {
|
|
242
|
+
args: [node.loc.source || ""],
|
|
243
|
+
loc: node.loc
|
|
244
|
+
});
|
|
245
|
+
return { props: [] };
|
|
246
|
+
}
|
|
247
|
+
const [parsedValue, parseStatus] = parseValue(value);
|
|
248
|
+
if (parseStatus !== 0) {
|
|
249
|
+
report(parseStatus, {
|
|
250
|
+
args: [node.loc.source || ""],
|
|
251
|
+
loc: node.loc
|
|
252
|
+
});
|
|
253
|
+
return { props: [] };
|
|
254
|
+
}
|
|
255
|
+
if (parsedValue == null) {
|
|
256
|
+
report(8, {
|
|
257
|
+
args: [node.loc.source || ""],
|
|
258
|
+
loc: node.loc
|
|
259
|
+
});
|
|
260
|
+
return { props: [] };
|
|
261
|
+
}
|
|
262
|
+
const global = getComposer(i18nInstance);
|
|
263
|
+
if (global == null) {
|
|
264
|
+
report(7, {
|
|
265
|
+
args: [node.loc.source || ""],
|
|
266
|
+
loc: node.loc
|
|
267
|
+
});
|
|
268
|
+
return { props: [] };
|
|
269
|
+
}
|
|
270
|
+
const content = global.t(...makeParams(parsedValue));
|
|
271
|
+
node.children.push({
|
|
272
|
+
type: NodeTypes.TEXT,
|
|
273
|
+
content
|
|
274
|
+
});
|
|
275
|
+
return { props: [] };
|
|
276
|
+
} else {
|
|
277
|
+
const translationParams = parseVTExpression(exp.content);
|
|
278
|
+
const code = generateTranslationCode(context, mode, translationParams, translationSignatures);
|
|
279
|
+
context.helper(TO_DISPLAY_STRING);
|
|
280
|
+
node.children.push({
|
|
281
|
+
type: NodeTypes.INTERPOLATION,
|
|
282
|
+
content: createCompoundExpression([createSimpleExpression(code, false, loc, 0)])
|
|
283
|
+
});
|
|
284
|
+
return { props: [] };
|
|
285
|
+
}
|
|
286
|
+
} else if (isCompoundExpressionNode(exp)) {
|
|
287
|
+
const content = exp.children.map(mapNodeContentHandler).join("");
|
|
288
|
+
const code = generateTranslationCode(context, mode, parseVTExpression(content), translationSignatures);
|
|
289
|
+
context.helper(TO_DISPLAY_STRING);
|
|
290
|
+
node.children.push({
|
|
291
|
+
type: NodeTypes.INTERPOLATION,
|
|
292
|
+
content: createCompoundExpression([createSimpleExpression(code, false, loc, 0)])
|
|
293
|
+
});
|
|
294
|
+
return { props: [] };
|
|
295
|
+
} else {
|
|
296
|
+
report(2, {
|
|
297
|
+
args: [node.loc.source || ""],
|
|
298
|
+
loc: node.loc
|
|
299
|
+
});
|
|
300
|
+
return { props: [] };
|
|
301
|
+
}
|
|
302
|
+
};
|
|
306
303
|
}
|
|
307
304
|
function getComposer(i18n) {
|
|
308
|
-
|
|
305
|
+
return isI18nInstance(i18n) ? getComposerInternal(i18n) : null;
|
|
309
306
|
}
|
|
310
307
|
function isI18nInstance(i18n) {
|
|
311
|
-
|
|
308
|
+
return i18n != null && "global" in i18n && "mode" in i18n;
|
|
312
309
|
}
|
|
313
310
|
function getComposerInternal(i18n) {
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
} else {
|
|
317
|
-
return isVueI18n(i18n.global) ? i18n.global.__composer : null;
|
|
318
|
-
}
|
|
311
|
+
if (i18n.mode === "composition") return isComposer(i18n.global) ? i18n.global : null;
|
|
312
|
+
else return isVueI18n(i18n.global) ? i18n.global.__composer : null;
|
|
319
313
|
}
|
|
320
314
|
function isComposer(target) {
|
|
321
|
-
|
|
315
|
+
return target != null && !("__composer" in target);
|
|
322
316
|
}
|
|
323
317
|
function isVueI18n(target) {
|
|
324
|
-
|
|
318
|
+
return target != null && "__composer" in target;
|
|
325
319
|
}
|
|
326
320
|
function isSimpleExpressionNode(node) {
|
|
327
|
-
|
|
321
|
+
return node != null && node.type === NodeTypes.SIMPLE_EXPRESSION;
|
|
328
322
|
}
|
|
329
323
|
function isCompoundExpressionNode(node) {
|
|
330
|
-
|
|
324
|
+
return node != null && node.type === NodeTypes.COMPOUND_EXPRESSION;
|
|
331
325
|
}
|
|
332
326
|
function isConstant(node) {
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
return node.constType <= 3 /* CAN_STRINGIFY */;
|
|
337
|
-
} else {
|
|
338
|
-
throw Error("unexpected error in Vue SimpleExpressionNode");
|
|
339
|
-
}
|
|
327
|
+
if ("isConstant" in node) return node.isConstant;
|
|
328
|
+
else if ("constType" in node) return node.constType <= 3;
|
|
329
|
+
else throw Error("unexpected error in Vue SimpleExpressionNode");
|
|
340
330
|
}
|
|
341
331
|
function mapNodeContentHandler(value) {
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
return value.content;
|
|
353
|
-
} else if (isSimpleExpressionNode(value.content)) {
|
|
354
|
-
return value.content.content;
|
|
355
|
-
} else if (isCompoundExpressionNode(value.content)) {
|
|
356
|
-
return value.content.children.map(mapNodeContentHandler).join("");
|
|
357
|
-
} else {
|
|
358
|
-
return "";
|
|
359
|
-
}
|
|
360
|
-
} else {
|
|
361
|
-
return "";
|
|
362
|
-
}
|
|
332
|
+
if (isString(value)) return value;
|
|
333
|
+
else if (isSymbol(value)) return value.description || "";
|
|
334
|
+
else if (isSimpleExpressionNode(value)) return value.content;
|
|
335
|
+
else if (isCompoundExpressionNode(value)) return value.children.map(mapNodeContentHandler).join("");
|
|
336
|
+
else if (isText(value)) {
|
|
337
|
+
if (isString(value.content)) return value.content;
|
|
338
|
+
else if (isSimpleExpressionNode(value.content)) return value.content.content;
|
|
339
|
+
else if (isCompoundExpressionNode(value.content)) return value.content.children.map(mapNodeContentHandler).join("");
|
|
340
|
+
else return "";
|
|
341
|
+
} else return "";
|
|
363
342
|
}
|
|
364
343
|
function parseValue(value) {
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
}
|
|
371
|
-
return [value, ReportCodes.SUCCESS];
|
|
372
|
-
} else {
|
|
373
|
-
return [null, ReportCodes.INVALID_PARAMETER_TYPE];
|
|
374
|
-
}
|
|
344
|
+
if (isString(value)) return [{ path: value }, 0];
|
|
345
|
+
else if (isObject(value)) {
|
|
346
|
+
if (!("path" in value)) return [null, 4];
|
|
347
|
+
return [value, 0];
|
|
348
|
+
} else return [null, 5];
|
|
375
349
|
}
|
|
376
350
|
function makeParams(value) {
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
}
|
|
389
|
-
return [path, named, options];
|
|
351
|
+
const { path, locale, args, choice, plural } = value;
|
|
352
|
+
const options = {};
|
|
353
|
+
const named = args || {};
|
|
354
|
+
if (isString(locale)) options.locale = locale;
|
|
355
|
+
if (isNumber(choice)) options.plural = choice;
|
|
356
|
+
if (isNumber(plural)) options.plural = plural;
|
|
357
|
+
return [
|
|
358
|
+
path,
|
|
359
|
+
named,
|
|
360
|
+
options
|
|
361
|
+
];
|
|
390
362
|
}
|
|
391
363
|
function generateTranslationCode(context, mode, params, translationSignatures) {
|
|
392
|
-
|
|
364
|
+
return mode === "composition" ? generateComposableCode(context, params, translationSignatures) : generateLegacyCode(context, params);
|
|
393
365
|
}
|
|
394
366
|
function generateTranslationCallableSignatures(context, translationSignatures) {
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
if (isFunction(signatureOrResolver)) {
|
|
406
|
-
return signatureOrResolver(context, baseResolver);
|
|
407
|
-
}
|
|
408
|
-
return baseResolver(context, signatureOrResolver);
|
|
409
|
-
}).filter(Boolean).join(" || ");
|
|
367
|
+
const baseResolver = (context, signature) => {
|
|
368
|
+
const { prefixIdentifiers, bindingMetadata, inline } = context;
|
|
369
|
+
if (inline && signature !== GLOBAL_TRANSLATE_SIGNATURE) return signature;
|
|
370
|
+
const type = hasOwn(bindingMetadata, signature) && bindingMetadata[signature];
|
|
371
|
+
return `${prefixIdentifiers ? type && type.startsWith("setup") || type === BindingTypes.LITERAL_CONST ? "$setup." : "_ctx." : ""}${signature}`;
|
|
372
|
+
};
|
|
373
|
+
return translationSignatures.map((signatureOrResolver) => {
|
|
374
|
+
if (isFunction(signatureOrResolver)) return signatureOrResolver(context, baseResolver);
|
|
375
|
+
return baseResolver(context, signatureOrResolver);
|
|
376
|
+
}).filter(Boolean).join(" || ");
|
|
410
377
|
}
|
|
411
378
|
function generateComposableCode(context, params, translationSignatures) {
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
}
|
|
429
|
-
builder.push(` }`);
|
|
430
|
-
builder.push(`)`);
|
|
431
|
-
const content = builder.content;
|
|
432
|
-
return content;
|
|
379
|
+
const baseCode = `(${generateTranslationCallableSignatures(context, translationSignatures)})`;
|
|
380
|
+
const builder = createContentBuilder();
|
|
381
|
+
builder.push(`${baseCode}(`);
|
|
382
|
+
builder.push(`${toDisplayString(params.path)}`);
|
|
383
|
+
builder.push(`, { `);
|
|
384
|
+
generateNamedCode(builder, params.named);
|
|
385
|
+
builder.push(` }`);
|
|
386
|
+
builder.push(`, { `);
|
|
387
|
+
if (params.options.locale) builder.push(`locale: ${toDisplayString(params.options.locale)}`);
|
|
388
|
+
if (params.options.plural) {
|
|
389
|
+
if (params.options.locale) builder.push(", ");
|
|
390
|
+
builder.push(`plural: ${toDisplayString(params.options.plural)}`);
|
|
391
|
+
}
|
|
392
|
+
builder.push(` }`);
|
|
393
|
+
builder.push(`)`);
|
|
394
|
+
return builder.content;
|
|
433
395
|
}
|
|
434
396
|
function generateNamedCode(builder, named) {
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
builder.push(`${k}: ${toDisplayString(v)}`);
|
|
444
|
-
}
|
|
445
|
-
});
|
|
397
|
+
Object.keys(named).forEach((k) => {
|
|
398
|
+
const v = named[k];
|
|
399
|
+
if (isObject(v)) {
|
|
400
|
+
builder.push(`${k}: { `);
|
|
401
|
+
generateNamedCode(builder, v);
|
|
402
|
+
builder.push(` }`);
|
|
403
|
+
} else builder.push(`${k}: ${toDisplayString(v)}`);
|
|
404
|
+
});
|
|
446
405
|
}
|
|
447
406
|
function generateLegacyCode(context, params) {
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
}
|
|
470
|
-
builder.push(`)`);
|
|
471
|
-
const content = builder.content;
|
|
472
|
-
return content;
|
|
407
|
+
const mode = !params.options.plural ? "basic" : "plural";
|
|
408
|
+
const baseCode = `${context.prefixIdentifiers ? "_ctx." : ""}${mode === "basic" ? "$t" : "$tc"}`;
|
|
409
|
+
const builder = createContentBuilder();
|
|
410
|
+
builder.push(`${baseCode}(`);
|
|
411
|
+
builder.push(`${toDisplayString(params.path)}`);
|
|
412
|
+
if (mode === "basic") {
|
|
413
|
+
if (isString(params.options.locale)) builder.push(`, ${toDisplayString(params.options.locale)}`);
|
|
414
|
+
builder.push(`, { `);
|
|
415
|
+
generateNamedCode(builder, params.named);
|
|
416
|
+
builder.push(` }`);
|
|
417
|
+
} else {
|
|
418
|
+
builder.push(`, ${toDisplayString(params.options.plural)}`);
|
|
419
|
+
if (isString(params.options.locale)) builder.push(`, ${toDisplayString(params.options.locale)}`);
|
|
420
|
+
else {
|
|
421
|
+
builder.push(`, { `);
|
|
422
|
+
generateNamedCode(builder, params.named);
|
|
423
|
+
builder.push(` }`);
|
|
424
|
+
}
|
|
425
|
+
}
|
|
426
|
+
builder.push(`)`);
|
|
427
|
+
return builder.content;
|
|
473
428
|
}
|
|
474
|
-
|
|
475
429
|
export { transformVTDirective };
|