@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/dist/index.cjs CHANGED
@@ -1,477 +1,430 @@
1
- 'use strict';
2
-
3
- const compilerDom = require('@vue/compiler-dom');
4
- const shared = require('@intlify/shared');
5
- const parser = require('@babel/parser');
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
- const ret = { status: "ng", value: void 0 };
9
- try {
10
- const ast = parser.parse(`const a = ${expression.trim()}`);
11
- if (ast.program.body.length >= 1 && ast.program.body[0].type === "VariableDeclaration" && ast.program.body[0].declarations.length >= 1) {
12
- const node = ast.program.body[0].declarations[0].init;
13
- if (node != null && (node.type === "StringLiteral" || node.type === "ObjectExpression")) {
14
- const val = new Function(`return ${expression.trim()} `)();
15
- ret.status = "ok";
16
- ret.value = val;
17
- }
18
- }
19
- } catch (_e) {
20
- }
21
- return ret;
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
- const ret = {
25
- path: "",
26
- named: {},
27
- options: {}
28
- };
29
- try {
30
- const ast = parser.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) {
35
- ret.path = node.extra.raw;
36
- } else if (node.type === "Identifier") {
37
- ret.path = node.name;
38
- } else if (node.type === "MemberExpression") {
39
- ret.path = getObjectMemberValue(node);
40
- } else if (node.type === "ObjectExpression") {
41
- node.properties.forEach((propNode) => {
42
- if (propNode.type === "ObjectProperty") {
43
- const propKeyNode = propNode.key;
44
- if (propKeyNode.type !== "Identifier") {
45
- return;
46
- }
47
- const propValueNode = propNode.value;
48
- switch (propKeyNode.name) {
49
- case "path":
50
- ret.path = getObjectMemberValue(propValueNode);
51
- break;
52
- case "locale":
53
- ret.options.locale = getObjectMemberValue(propValueNode);
54
- break;
55
- case "choice":
56
- case "plural":
57
- ret.options.plural = getObjectMemberValue(propValueNode);
58
- break;
59
- case "args":
60
- if (propValueNode.type === "ObjectExpression") {
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
- if ((node.type === "StringLiteral" || node.type === "NumericLiteral") && node.extra) {
78
- return node.extra.raw;
79
- } else if (node.type === "Identifier") {
80
- return node.name;
81
- } else if (node.type === "MemberExpression") {
82
- const paths = [];
83
- collectMemberPath(node, paths);
84
- paths.reverse();
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
- node.properties.forEach((propNode) => {
92
- if (propNode.type === "ObjectProperty") {
93
- const propKeyNode = propNode.key;
94
- if (propKeyNode.type !== "Identifier") {
95
- return;
96
- }
97
- if (!(propKeyNode.name in target)) {
98
- target[propKeyNode.name] = {};
99
- }
100
- const propValueNode = propNode.value;
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
- if (node.type === "Identifier") {
111
- paths.push(node.name);
112
- return;
113
- } else if (node.type === "MemberExpression" && node.property.type === "Identifier") {
114
- paths.push(node.property.name);
115
- return collectMemberPath(node.object, paths);
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
- [1 /* UNEXPECTED_DIRECTIVE_EXPRESSION */]: `Unexpected directive expression: {0}`,
134
- [2 /* NOT_SUPPORTED */]: `Not supported transform: {0}`,
135
- [3 /* FAILED_VALUE_EVALUATION */]: `Failed value evaluation: {0}`,
136
- [4 /* REQUIRED_PARAMETER */]: `Required parameter: {0}`,
137
- [5 /* INVALID_PARAMETER_TYPE */]: `Required parameter: {0}`,
138
- [6 /* OVERRIDE_ELEMENT_CHILDREN */]: `v-t will override element children: {0}`,
139
- [7 /* NOT_RESOLVED_COMPOSER */]: `Not resolved vue-i18n composer: {0}`,
140
- [8 /* UNEXPECTED_ERROR */]: `Unexpected error: {0}`
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
- return shared.format(ReportMessages[code], ...args);
106
+ return (0, _intlify_shared.format)(ReportMessages[code], ...args);
144
107
  }
145
108
  function createExtensionsError(code, msg, loc) {
146
- const error = new SyntaxError(msg);
147
- error.code = code;
148
- if (loc) {
149
- error.loc = loc;
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
- const mode = options.mode && shared.isString(options.mode) && ["warn", "error"].includes(options.mode) ? options.mode : "warn";
155
- const msg = getReportMessage(code, options.args);
156
- if (mode === "warn") {
157
- console.warn("[vue-i18n-extensions] " + msg);
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
- let _indentLevel = options.indentLevel != null && shared.isNumber(options.indentLevel) ? options.indentLevel : 0;
165
- let _content = "";
166
- function push(content) {
167
- _content += content;
168
- }
169
- function _newline(n) {
170
- push("\n" + ` `.repeat(n));
171
- }
172
- function newline() {
173
- _newline(_indentLevel);
174
- }
175
- function pushline(content) {
176
- push(content);
177
- newline();
178
- }
179
- function indent(withoutNewLine) {
180
- if (withoutNewLine) {
181
- ++_indentLevel;
182
- } else {
183
- _newline(++_indentLevel);
184
- }
185
- }
186
- function deindent(withoutNewLine) {
187
- if (withoutNewLine) {
188
- --_indentLevel;
189
- } else {
190
- _newline(--_indentLevel);
191
- }
192
- }
193
- return {
194
- get indentLevel() {
195
- return _indentLevel;
196
- },
197
- get content() {
198
- return String(_content);
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
- const i18nInstance = options.i18n;
211
- const mode = shared.isString(options.mode) && ["composition", "legacy"].includes(options.mode) ? options.mode : "composition";
212
- const translationSignatures = shared.isString(options.translationSignatures) || shared.isFunction(options.translationSignatures) ? [options.translationSignatures] : shared.isArray(options.translationSignatures) ? options.translationSignatures : ["t"];
213
- translationSignatures.push(GLOBAL_TRANSLATE_SIGNATURE);
214
- return (dir, node, context) => {
215
- const { exp, loc } = dir;
216
- if (!exp) {
217
- report(ReportCodes.UNEXPECTED_DIRECTIVE_EXPRESSION, {
218
- mode: "error",
219
- args: [node.loc.source || ""],
220
- loc: node.loc
221
- });
222
- }
223
- if (node.children.length > 0) {
224
- report(ReportCodes.OVERRIDE_ELEMENT_CHILDREN, {
225
- mode: "error",
226
- args: [node.loc.source || ""],
227
- loc: node.loc
228
- });
229
- node.children.length = 0;
230
- }
231
- if (isSimpleExpressionNode(exp)) {
232
- if (isConstant(exp) && i18nInstance) {
233
- const { status, value } = evaluateValue(exp.content);
234
- if (status === "ng") {
235
- report(ReportCodes.FAILED_VALUE_EVALUATION, {
236
- args: [node.loc.source || ""],
237
- loc: node.loc
238
- });
239
- return { props: [] };
240
- }
241
- const [parsedValue, parseStatus] = parseValue(value);
242
- if (parseStatus !== ReportCodes.SUCCESS) {
243
- report(parseStatus, { args: [node.loc.source || ""], loc: node.loc });
244
- return { props: [] };
245
- }
246
- if (parsedValue == null) {
247
- report(ReportCodes.UNEXPECTED_ERROR, {
248
- args: [node.loc.source || ""],
249
- loc: node.loc
250
- });
251
- return { props: [] };
252
- }
253
- const global = getComposer(i18nInstance);
254
- if (global == null) {
255
- report(ReportCodes.NOT_RESOLVED_COMPOSER, {
256
- args: [node.loc.source || ""],
257
- loc: node.loc
258
- });
259
- return { props: [] };
260
- }
261
- const content = global.t(...makeParams(parsedValue));
262
- node.children.push({
263
- type: compilerDom.NodeTypes.TEXT,
264
- content
265
- });
266
- return { props: [] };
267
- } else {
268
- const translationParams = parseVTExpression(exp.content);
269
- const code = generateTranslationCode(
270
- context,
271
- mode,
272
- translationParams,
273
- translationSignatures
274
- );
275
- context.helper(compilerDom.TO_DISPLAY_STRING);
276
- node.children.push({
277
- type: compilerDom.NodeTypes.INTERPOLATION,
278
- content: compilerDom.createCompoundExpression([
279
- compilerDom.createSimpleExpression(code, false, loc, 0 /* NOT_CONSTANT */)
280
- ])
281
- });
282
- return { props: [] };
283
- }
284
- } else if (isCompoundExpressionNode(exp)) {
285
- const content = exp.children.map(mapNodeContentHandler).join("");
286
- const code = generateTranslationCode(
287
- context,
288
- mode,
289
- parseVTExpression(content),
290
- translationSignatures
291
- );
292
- context.helper(compilerDom.TO_DISPLAY_STRING);
293
- node.children.push({
294
- type: compilerDom.NodeTypes.INTERPOLATION,
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
- return isI18nInstance(i18n) ? getComposerInternal(i18n) : null;
306
+ return isI18nInstance(i18n) ? getComposerInternal(i18n) : null;
311
307
  }
312
308
  function isI18nInstance(i18n) {
313
- return i18n != null && "global" in i18n && "mode" in i18n;
309
+ return i18n != null && "global" in i18n && "mode" in i18n;
314
310
  }
315
311
  function getComposerInternal(i18n) {
316
- if (i18n.mode === "composition") {
317
- return isComposer(i18n.global) ? i18n.global : null;
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
- return target != null && !("__composer" in target);
316
+ return target != null && !("__composer" in target);
324
317
  }
325
318
  function isVueI18n(target) {
326
- return target != null && "__composer" in target;
319
+ return target != null && "__composer" in target;
327
320
  }
328
321
  function isSimpleExpressionNode(node) {
329
- return node != null && node.type === compilerDom.NodeTypes.SIMPLE_EXPRESSION;
322
+ return node != null && node.type === _vue_compiler_dom.NodeTypes.SIMPLE_EXPRESSION;
330
323
  }
331
324
  function isCompoundExpressionNode(node) {
332
- return node != null && node.type === compilerDom.NodeTypes.COMPOUND_EXPRESSION;
325
+ return node != null && node.type === _vue_compiler_dom.NodeTypes.COMPOUND_EXPRESSION;
333
326
  }
334
327
  function isConstant(node) {
335
- if ("isConstant" in node) {
336
- return node.isConstant;
337
- } else if ("constType" in node) {
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
- if (shared.isString(value)) {
345
- return value;
346
- } else if (shared.isSymbol(value)) {
347
- return value.description || "";
348
- } else if (isSimpleExpressionNode(value)) {
349
- return value.content;
350
- } else if (isCompoundExpressionNode(value)) {
351
- return value.children.map(mapNodeContentHandler).join("");
352
- } else if (compilerDom.isText(value)) {
353
- if (shared.isString(value.content)) {
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
- if (shared.isString(value)) {
368
- return [{ path: value }, ReportCodes.SUCCESS];
369
- } else if (shared.isObject(value)) {
370
- if (!("path" in value)) {
371
- return [null, ReportCodes.REQUIRED_PARAMETER];
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
- const { path, locale, args, choice, plural } = value;
380
- const options = {};
381
- const named = args || {};
382
- if (shared.isString(locale)) {
383
- options.locale = locale;
384
- }
385
- if (shared.isNumber(choice)) {
386
- options.plural = choice;
387
- }
388
- if (shared.isNumber(plural)) {
389
- options.plural = plural;
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
- return mode === "composition" ? generateComposableCode(context, params, translationSignatures) : generateLegacyCode(context, params);
365
+ return mode === "composition" ? generateComposableCode(context, params, translationSignatures) : generateLegacyCode(context, params);
395
366
  }
396
367
  function generateTranslationCallableSignatures(context, translationSignatures) {
397
- const baseResolver = (context2, signature) => {
398
- const { prefixIdentifiers, bindingMetadata, inline } = context2;
399
- if (inline && signature !== GLOBAL_TRANSLATE_SIGNATURE) {
400
- return signature;
401
- }
402
- const type = shared.hasOwn(bindingMetadata, signature) && bindingMetadata[signature];
403
- const bindingContext = prefixIdentifiers ? type && type.startsWith("setup") || type === compilerDom.BindingTypes.LITERAL_CONST ? "$setup." : "_ctx." : "";
404
- return `${bindingContext}${signature}`;
405
- };
406
- return translationSignatures.map((signatureOrResolver) => {
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
- const baseCode = `(${generateTranslationCallableSignatures(context, translationSignatures)})`;
415
- const builder = createContentBuilder();
416
- builder.push(`${baseCode}(`);
417
- builder.push(`${shared.toDisplayString(params.path)}`);
418
- builder.push(`, { `);
419
- generateNamedCode(builder, params.named);
420
- builder.push(` }`);
421
- builder.push(`, { `);
422
- if (params.options.locale) {
423
- builder.push(`locale: ${shared.toDisplayString(params.options.locale)}`);
424
- }
425
- if (params.options.plural) {
426
- if (params.options.locale) {
427
- builder.push(", ");
428
- }
429
- builder.push(`plural: ${shared.toDisplayString(params.options.plural)}`);
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
- const keys = Object.keys(named);
438
- keys.forEach((k) => {
439
- const v = named[k];
440
- if (shared.isObject(v)) {
441
- builder.push(`${k}: { `);
442
- generateNamedCode(builder, v);
443
- builder.push(` }`);
444
- } else {
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
- const mode = !params.options.plural ? "basic" : "plural";
451
- const baseCode = `${context.prefixIdentifiers ? "_ctx." : ""}${mode === "basic" ? "$t" : "$tc"}`;
452
- const builder = createContentBuilder();
453
- builder.push(`${baseCode}(`);
454
- builder.push(`${shared.toDisplayString(params.path)}`);
455
- if (mode === "basic") {
456
- if (shared.isString(params.options.locale)) {
457
- builder.push(`, ${shared.toDisplayString(params.options.locale)}`);
458
- }
459
- builder.push(`, { `);
460
- generateNamedCode(builder, params.named);
461
- builder.push(` }`);
462
- } else {
463
- builder.push(`, ${shared.toDisplayString(params.options.plural)}`);
464
- if (shared.isString(params.options.locale)) {
465
- builder.push(`, ${shared.toDisplayString(params.options.locale)}`);
466
- } else {
467
- builder.push(`, { `);
468
- generateNamedCode(builder, params.named);
469
- builder.push(` }`);
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;