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