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