@menapo/vi18n 0.1.1 → 0.2.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 +19 -0
- package/dist/{apply-Bre-lY9J.js → apply-Bg-3rB2m.js} +69 -89
- package/dist/{captured-Bj8bO6vd.js → captured-DfLk50Ji.js} +196 -217
- package/dist/{check-BIKEuu6B.js → check-CunekuYs.js} +76 -85
- package/dist/cli.js +2 -3
- package/dist/{collect-Cx4_SKOe.js → collect-CYATDjdo.js} +1 -2
- package/dist/collisions-CDSdtLQm.js +215 -0
- package/dist/{compile-Cc97JaW2.js → compile-DqkofbiO.js} +376 -331
- package/dist/compile-hva0qjhZ.js +240 -0
- package/dist/concurrency-BCY_4zcr.js +1 -0
- package/dist/concurrency-Dy2ny50V.js +107 -0
- package/dist/config.d.ts +1 -5
- package/dist/config.js +4 -5
- package/dist/{types-D3QGM8XO.js → diagnostics-LgIWyeSp.js} +28 -27
- package/dist/{dir-zw6bGMKQ.js → dir-TGLYuOZb.js} +34 -37
- package/dist/{duplicates-yuRljQ6h.js → duplicates-8cudcHNj.js} +10 -27
- package/dist/engine-ecRD8-IE.js +556 -0
- package/dist/engine.d.ts +1 -5
- package/dist/engine.js +297 -168
- package/dist/exchange-DjwlrTQ9.js +41 -0
- package/dist/{exclude-Du-Zd_qI.js → exclude-BFETmvde.js} +2 -3
- package/dist/{export-C9Sy6UWv.js → export-D2tsqWIr.js} +17 -38
- package/dist/{extract-C3S8btx0.js → extract-BxYchVmY.js} +114 -82
- package/dist/{formats-9unqj_fZ.js → formats-BaryZ2N7.js} +352 -276
- package/dist/fs-ezCMZeDV.js +52 -0
- package/dist/i18next-COO6o0tX.js +1907 -0
- package/dist/i18next-Dr7DCQlQ.js +1 -0
- package/dist/{import-nANvQutw.js → import-vSC29Cn4.js} +81 -75
- package/dist/index.d.ts +1 -13
- package/dist/index.js +14 -16
- package/dist/{init-C2ScuW6a.js → init-Cc7A-8VX.js} +466 -501
- package/dist/{library-D5CWvTQs.js → library-tp_xqzSY.js} +975 -451
- package/dist/lingui-BnnlqGXT.js +902 -0
- package/dist/lint.d.ts +0 -4
- package/dist/lint.js +70 -82
- package/dist/{load-gW-oJt1Q.js → load-BSIeGJ5v.js} +275 -148
- package/dist/{load-CQRz4PpM.js → load-SSrAeJVB.js} +1214 -1116
- package/dist/{locales-DX4MOBFE.js → locales-D0U-yVRm.js} +271 -237
- package/dist/{lsp-RMMMc88P.js → lsp-DvC11pBZ.js} +4 -5
- package/dist/{match-BGr8EXB3.js → match-BLDcj610.js} +11 -4
- package/dist/{mcp-CHVwBoRP.js → mcp-D2zzOTBy.js} +4 -5
- package/dist/{merge-driver-BTX87aVm.js → merge-driver-BLI8OtAc.js} +138 -122
- package/dist/migrate-1r43OBfe.js +1 -0
- package/dist/migrate-ChDGhlC-.js +2169 -0
- package/dist/{openai-CTaMDcl-.js → openai-CLn4puY8.js} +128 -87
- package/dist/{output-DWE3SIUA.js → output-D5tAt8q4.js} +15 -9
- package/dist/parse.d.ts +1 -3
- package/dist/parse.js +7 -8
- package/dist/{pipeline-C9Dbotkh.js → pipeline-BJSkkRYC.js} +387 -359
- package/dist/{pipeline-DbX6rlo5.js → pipeline-BeCc441z.js} +1 -1
- package/dist/project-B5KIO0q-.js +544 -0
- package/dist/{pseudo-DRv30iN5.js → pseudo-B2DUpoLu.js} +126 -3
- package/dist/{raw-C4Y6vyGY.js → raw-YsYZMS9n.js} +52 -29
- package/dist/react-intl-C8JehBBL.js +634 -0
- package/dist/rename-ZVGq9ZG1.js +577 -0
- package/dist/repeated-uuI1tUGg.js +85 -0
- package/dist/restore-xlBIrtRt.js +199 -0
- package/dist/rewrite-BwzXoBiA.js +415 -0
- package/dist/{rich-DCUKl5HM.js → rich-ApY1Dm4S.js} +35 -28
- package/dist/rolldown-runtime-C1I3wEg2.js +43 -0
- package/dist/{rolldown-runtime-D7D4PA-g.js → rolldown-runtime-D8jQpDG7.js} +1 -2
- package/dist/run-CQ9pDLSa.js +674 -0
- package/dist/scan-worker.d.ts +2 -4
- package/dist/scan-worker.js +1 -1
- package/dist/{scoped-BMYpSthj.js → scoped-h3KE8XWW.js} +3 -4
- package/dist/{server-Da1Ot9IN.js → server-Br8tAuG2.js} +312 -386
- package/dist/{server-deBOq2vq.js → server-DJRpUQVZ.js} +1059 -333
- package/dist/server.d.ts +1 -17
- package/dist/server.js +419 -379
- package/dist/{sitemap-DJXzKoza.js → sitemap-CSAkm6oi.js} +21 -33
- package/dist/{sources-D19czDSA.js → sources-DNVbYsgs.js} +713 -373
- package/dist/{status-zr4jY59Y.js → status-q6M3rBrh.js} +30 -30
- package/dist/{stdio-FPOKQAgu.js → stdio-D9kTzyqB.js} +2 -4
- package/dist/{stdio-CVaJfxA7.js → stdio-FWIVU3P4.js} +1 -2
- package/dist/sync-BNIAwMhR.js +1352 -0
- package/dist/{t-DC-c9zE5.js → t-gvEXx25s.js} +76 -53
- package/dist/test.d.ts +5 -16
- package/dist/test.js +126 -56
- package/dist/translate-BP4iVhOR.js +336 -0
- package/dist/{ts-BaU5yOf0.js → ts-BX6kwig5.js} +25 -14
- package/dist/{types-DtvQ7UdD.js → types-3kwyaCAm.js} +1 -2
- package/dist/{types-DsdWBG0b.d.ts → types-B4wNX6PA.d.ts} +6 -8
- package/dist/{types-CgUCkFU1.d.ts → types-_4UdFhR9.d.ts} +24 -21
- package/dist/types-xR2exI3L.js +42 -0
- package/dist/{units-CQ5Q99RC.js → units-DPPfCN7j.js} +7 -16
- package/dist/{url-CRfCOhUI.js → url-Bpc6wLea.js} +24 -11
- package/dist/{url-CsBkRaJy.js → url-D2_VlEeH.js} +25 -12
- package/dist/{validate-BxpupdAf.js → validate-CgzaDdOY.js} +690 -377
- package/dist/{validate-D6DJepZv.js → validate-PGgMsasz.js} +3 -5
- package/dist/{version-Byqi2guX.js → version-DC-J5WvV.js} +3 -5
- package/dist/{vite-scan-CT_vsp4g.js → vite-scan-kM8Ta3rO.js} +87 -92
- package/dist/vite.d.ts +2 -6
- package/dist/vite.js +6500 -6032
- package/dist/vue-i18n-_uIXlFVh.js +806 -0
- package/dist/{which-BOemZ5Q8.js → which-DFWk_KwN.js} +44 -37
- package/dist/{worker-84U6uYB3.js → worker-zlhNj4y3.js} +454 -608
- package/package.json +5 -5
- package/dist/check-HsmmpJZg.js +0 -1
- package/dist/codec-DuvU2VQy.js +0 -207
- package/dist/collisions-CfpZU8e4.js +0 -217
- package/dist/compile-BaDh0cHG.js +0 -245
- package/dist/concurrency-C9UUWuDC.js +0 -96
- package/dist/concurrency-DONL7t4V.js +0 -1
- package/dist/engine-C2ENBRJe.js +0 -531
- package/dist/i18next-9HAoxwUI.js +0 -1
- package/dist/i18next-BEJgv_S8.js +0 -1596
- package/dist/lingui-zUiCl9Um.js +0 -833
- package/dist/messages-BfjIExyB.js +0 -166
- package/dist/migrate-IpLDMXmq.js +0 -1484
- package/dist/migrate-rCQuOGsf.js +0 -1
- package/dist/project-Bw9UPIdx.js +0 -1046
- package/dist/project-C4GpaN-n.js +0 -381
- package/dist/react-intl-BP0u-D5m.js +0 -519
- package/dist/restore-CiYv-jxS.js +0 -175
- package/dist/rewrite-EsIw_lsq.js +0 -803
- package/dist/run-BEcnBbu-.js +0 -694
- package/dist/runtime-DTMmliT1.js +0 -64
- package/dist/sync-B9V2KFwb.js +0 -658
- package/dist/translate-CQnJfLjO.js +0 -299
- package/dist/types-wSh9fCBY.js +0 -11
- package/dist/vue-i18n-g0iGy6CZ.js +0 -651
|
@@ -0,0 +1,902 @@
|
|
|
1
|
+
import { g as parsePo } from "./library-tp_xqzSY.js";
|
|
2
|
+
import { n as isDiagnosticError } from "./diagnostics-LgIWyeSp.js";
|
|
3
|
+
import { s as decodeEntities } from "./worker-zlhNj4y3.js";
|
|
4
|
+
import { C as langFromId, w as parseModule } from "./sources-DNVbYsgs.js";
|
|
5
|
+
import { b as printICU } from "./validate-CgzaDdOY.js";
|
|
6
|
+
import { _ as linguiId, c as newNaming, d as richCall, f as selfClosingTags, l as reactTypes, o as elementHandler, p as siteHandlers, s as entry, t as MODULE_EXTENSIONS, u as renameTags, v as parseJson } from "./i18next-COO6o0tX.js";
|
|
7
|
+
import { B as objectProperties, C as attributeString, D as inJsxChildren, E as identifierName, F as jsxNameNodes, I as keyName, M as jsxAttributes, P as jsxName, S as attributeExpression, V as stringValue, W as unwrap, w as childEntries, x as analyzeModule, z as memberParts } from "./migrate-ChDGhlC-.js";
|
|
8
|
+
import { t as ModuleBuilder } from "./rewrite-BwzXoBiA.js";
|
|
9
|
+
import { extname } from "node:path";
|
|
10
|
+
import { readFileSync } from "node:fs";
|
|
11
|
+
// src/cli/commands/migrate/lingui.ts
|
|
12
|
+
/**
|
|
13
|
+
* `vi18n migrate --from lingui` (INTERNALS §2.14): Lingui 3–5 macros and
|
|
14
|
+
* runtime.
|
|
15
|
+
*
|
|
16
|
+
* The source text is what the macros compile to, built the way Lingui
|
|
17
|
+
* builds it (`${name}` → `{name}`, other expressions → `{0}`, `plural()` /
|
|
18
|
+
* `select()` / `selectOrdinal()` and `<Plural>`… as ICU, JSX elements as
|
|
19
|
+
* `<0>…</0>`), so translations are found under the id Lingui gives it
|
|
20
|
+
* (`generateMessageId(message, context)`, or the explicit `id`). Catalogs
|
|
21
|
+
* are PO (`#. js-lingui-explicit-id` marks an explicit id), Lingui JSON or
|
|
22
|
+
* minimal JSON; a fuzzy translation is kept for review, not shipped. A
|
|
23
|
+
* `context` is a translator note and, when two messages share a text but
|
|
24
|
+
* not their translations, the stem of their manual ids (PLAN §5.6).
|
|
25
|
+
* `t`, `msg`/`defineMessage`, `<Trans>`, `<Plural>`, `i18n._()` become
|
|
26
|
+
* `t()`, `msg()`, `rich()`; macro imports, `useLingui()` bindings and
|
|
27
|
+
* `<I18nProvider>` go once unused.
|
|
28
|
+
*/
|
|
29
|
+
function isOldSource(source) {
|
|
30
|
+
return source.startsWith("@lingui/");
|
|
31
|
+
}
|
|
32
|
+
const MACRO_SOURCES = /* @__PURE__ */ new Set([
|
|
33
|
+
"@lingui/macro",
|
|
34
|
+
"@lingui/core/macro",
|
|
35
|
+
"@lingui/react/macro"
|
|
36
|
+
]);
|
|
37
|
+
var MessageText = class MessageText {
|
|
38
|
+
text = "";
|
|
39
|
+
shared;
|
|
40
|
+
constructor(shared) {
|
|
41
|
+
this.shared = shared ?? {
|
|
42
|
+
values: [],
|
|
43
|
+
elements: [],
|
|
44
|
+
consumed: [],
|
|
45
|
+
positional: 0,
|
|
46
|
+
problem: null
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
get values() {
|
|
50
|
+
return this.shared.values;
|
|
51
|
+
}
|
|
52
|
+
get elements() {
|
|
53
|
+
return this.shared.elements;
|
|
54
|
+
}
|
|
55
|
+
get problem() {
|
|
56
|
+
return this.shared.problem;
|
|
57
|
+
}
|
|
58
|
+
/** A part of the same message (a plural case): same values and counters, its own text. */
|
|
59
|
+
nested() {
|
|
60
|
+
return new MessageText(this.shared);
|
|
61
|
+
}
|
|
62
|
+
/** `{name}` for an identifier, `{0}`, `{1}`… for other expressions, as Lingui names them. */
|
|
63
|
+
argument(expression) {
|
|
64
|
+
const n = unwrap(expression);
|
|
65
|
+
if (n.type === "Identifier") {
|
|
66
|
+
const name = n.name;
|
|
67
|
+
if (!this.values.some((v) => v.name === name)) this.values.push({
|
|
68
|
+
name,
|
|
69
|
+
expression: n
|
|
70
|
+
});
|
|
71
|
+
return name;
|
|
72
|
+
}
|
|
73
|
+
const name = String(this.shared.positional++);
|
|
74
|
+
this.values.push({
|
|
75
|
+
name,
|
|
76
|
+
expression: n
|
|
77
|
+
});
|
|
78
|
+
return name;
|
|
79
|
+
}
|
|
80
|
+
fail(problem) {
|
|
81
|
+
this.shared.problem ??= problem;
|
|
82
|
+
}
|
|
83
|
+
built() {
|
|
84
|
+
return {
|
|
85
|
+
message: this.text,
|
|
86
|
+
values: this.values,
|
|
87
|
+
elements: this.elements,
|
|
88
|
+
consumed: this.shared.consumed,
|
|
89
|
+
problem: this.problem
|
|
90
|
+
};
|
|
91
|
+
}
|
|
92
|
+
};
|
|
93
|
+
/** Lingui's whitespace rule for JSX messages: a line break and the indentation after it are one space. */
|
|
94
|
+
function normalizeWhitespace(text) {
|
|
95
|
+
return text.replace(/(?:\\(?:\r\n|\r|\n))+\s+/g, " ").replace(/(?:\r\n|\r|\n)+\s+/g, " ").trim();
|
|
96
|
+
}
|
|
97
|
+
/** `=N` for numeric choice keys (`0`, `_0`), CLDR categories as they are, select values without `_`. */
|
|
98
|
+
function choiceKey(key) {
|
|
99
|
+
if (/^\d+$/.test(key)) return `=${key}`;
|
|
100
|
+
if (/^_\d+$/.test(key)) return `=${key.slice(1)}`;
|
|
101
|
+
if (key.startsWith("_")) return key.slice(1);
|
|
102
|
+
return key;
|
|
103
|
+
}
|
|
104
|
+
/**
|
|
105
|
+
* The source text of an entry with an explicit id: its source-locale
|
|
106
|
+
* translation (the msgid is the id there), null in other locales.
|
|
107
|
+
*/
|
|
108
|
+
function explicitIdMessage(isSourceLocale, translation) {
|
|
109
|
+
return isSourceLocale && translation !== "" ? translation : null;
|
|
110
|
+
}
|
|
111
|
+
/** The function a macro use calls: a tagged template's tag or a call's callee. */
|
|
112
|
+
function macroCallee(node) {
|
|
113
|
+
if (node.type === "TaggedTemplateExpression") return node.tag;
|
|
114
|
+
if (node.type === "CallExpression") return node.callee;
|
|
115
|
+
return null;
|
|
116
|
+
}
|
|
117
|
+
function createLinguiAdapter() {
|
|
118
|
+
return new LinguiAdapter();
|
|
119
|
+
}
|
|
120
|
+
const NO_NOTES = {
|
|
121
|
+
id: null,
|
|
122
|
+
context: null,
|
|
123
|
+
comment: null
|
|
124
|
+
};
|
|
125
|
+
/** Lingui numbers tags (`<0>`), which vi18n reads as text: name them. */
|
|
126
|
+
const numbered = (text) => text.replace(/<(\/?)(\d+)(\/?)>/g, (_, a, n, b) => `<${a}t${n}${b}>`);
|
|
127
|
+
/** A PO entry as a Lingui catalog entry, under the id Lingui gives it. */
|
|
128
|
+
function poEntry(e, file, isSourceLocale) {
|
|
129
|
+
const explicit = e.contexts.some((c) => c.trim() === "js-lingui-explicit-id");
|
|
130
|
+
const idNote = e.contexts.map((c) => /^js-lingui-id:\s*(\S+)/.exec(c.trim())?.[1]).find((x) => x !== void 0);
|
|
131
|
+
const context = e.manualId ?? e.foreignContext;
|
|
132
|
+
const msgid = e.source ?? "";
|
|
133
|
+
return [explicit ? msgid : idNote ?? linguiId(msgid, context), {
|
|
134
|
+
message: explicit ? explicitIdMessage(isSourceLocale, e.translation) : msgid,
|
|
135
|
+
translation: e.translation,
|
|
136
|
+
fuzzy: e.fuzzy,
|
|
137
|
+
context,
|
|
138
|
+
file,
|
|
139
|
+
line: e.line
|
|
140
|
+
}];
|
|
141
|
+
}
|
|
142
|
+
/** A Lingui JSON record (`{ message, translation, fuzzy | flags, context }`) as a catalog entry. */
|
|
143
|
+
function jsonEntry(record, file, line) {
|
|
144
|
+
const flags = record.flags;
|
|
145
|
+
return {
|
|
146
|
+
message: typeof record.message === "string" ? record.message : null,
|
|
147
|
+
translation: typeof record.translation === "string" ? record.translation : "",
|
|
148
|
+
fuzzy: record.fuzzy === true || Array.isArray(flags) && flags.includes("fuzzy"),
|
|
149
|
+
context: typeof record.context === "string" ? record.context : null,
|
|
150
|
+
file,
|
|
151
|
+
line
|
|
152
|
+
};
|
|
153
|
+
}
|
|
154
|
+
/** A translation as converted: fuzzy ones kept for review, the rest with their tags renamed. */
|
|
155
|
+
function translationOf(found, rename) {
|
|
156
|
+
const at = {
|
|
157
|
+
unconverted: found.translation,
|
|
158
|
+
file: found.file,
|
|
159
|
+
line: found.line
|
|
160
|
+
};
|
|
161
|
+
if (found.fuzzy) return {
|
|
162
|
+
...at,
|
|
163
|
+
reason: "Lingui marked it fuzzy"
|
|
164
|
+
};
|
|
165
|
+
const converted = rename(numbered(found.translation));
|
|
166
|
+
return typeof converted === "string" ? { icu: converted } : {
|
|
167
|
+
...at,
|
|
168
|
+
reason: converted.error
|
|
169
|
+
};
|
|
170
|
+
}
|
|
171
|
+
/** The Lingui catalogs of a run, and the messages its macros define. */
|
|
172
|
+
var LinguiAdapter = class {
|
|
173
|
+
catalogExtensions = [".po", ".json"];
|
|
174
|
+
catalogsRequired = false;
|
|
175
|
+
extensions = MODULE_EXTENSIONS;
|
|
176
|
+
/** locale → Lingui id → entry. */
|
|
177
|
+
catalogs = /* @__PURE__ */ new Map();
|
|
178
|
+
/** v3 minimal JSON keys catalogs by message text: locale → text → translation. */
|
|
179
|
+
byText = /* @__PURE__ */ new Map();
|
|
180
|
+
namings = /* @__PURE__ */ new Map();
|
|
181
|
+
/** Source-catalog entries some call site found, for the unused-key report. */
|
|
182
|
+
found = /* @__PURE__ */ new Set();
|
|
183
|
+
sourceLocale = "";
|
|
184
|
+
mentions(text) {
|
|
185
|
+
return text.includes("@lingui/");
|
|
186
|
+
}
|
|
187
|
+
load(files, env) {
|
|
188
|
+
const sourceLocale = this.sourceLocale = env.config.sourceLocale;
|
|
189
|
+
const sorted = [...files].sort((a, b) => Number(b.locale === sourceLocale) - Number(a.locale === sourceLocale));
|
|
190
|
+
for (const file of sorted) this.readCatalog(file, env);
|
|
191
|
+
}
|
|
192
|
+
planFile(file, text, env) {
|
|
193
|
+
let program;
|
|
194
|
+
try {
|
|
195
|
+
program = parseModule(text, langFromId(file), file).program;
|
|
196
|
+
} catch (error) {
|
|
197
|
+
if (!isDiagnosticError(error)) throw error;
|
|
198
|
+
env.diagnostics.push({
|
|
199
|
+
...error.diagnostic,
|
|
200
|
+
file: env.rel(file)
|
|
201
|
+
});
|
|
202
|
+
return null;
|
|
203
|
+
}
|
|
204
|
+
return new LinguiModule(this, env, {
|
|
205
|
+
file,
|
|
206
|
+
text,
|
|
207
|
+
program
|
|
208
|
+
}).plan();
|
|
209
|
+
}
|
|
210
|
+
unusedKeys() {
|
|
211
|
+
const out = [];
|
|
212
|
+
for (const [id, e] of this.catalogs.get(this.sourceLocale) ?? []) if (!this.found.has(e)) out.push(e.message !== null && e.message !== id ? `${id} (${e.message})` : id);
|
|
213
|
+
return out;
|
|
214
|
+
}
|
|
215
|
+
textsOf(locale) {
|
|
216
|
+
let texts = this.byText.get(locale);
|
|
217
|
+
if (texts === void 0) this.byText.set(locale, texts = /* @__PURE__ */ new Map());
|
|
218
|
+
return texts;
|
|
219
|
+
}
|
|
220
|
+
put(locale, id, entryValue) {
|
|
221
|
+
let map = this.catalogs.get(locale);
|
|
222
|
+
if (map === void 0) this.catalogs.set(locale, map = /* @__PURE__ */ new Map());
|
|
223
|
+
map.set(id, entryValue);
|
|
224
|
+
const texts = this.textsOf(locale);
|
|
225
|
+
if (entryValue.message !== null && !texts.has(entryValue.message)) texts.set(entryValue.message, entryValue);
|
|
226
|
+
}
|
|
227
|
+
readCatalog(file, env) {
|
|
228
|
+
let text;
|
|
229
|
+
try {
|
|
230
|
+
text = readFileSync(file.file, "utf8");
|
|
231
|
+
} catch (error) {
|
|
232
|
+
const message = `cannot read: ${error.message}`;
|
|
233
|
+
env.report("catalog-parse", message, { file: file.file }, "error");
|
|
234
|
+
return;
|
|
235
|
+
}
|
|
236
|
+
if (extname(file.file) === ".po") this.readPo(file, text, env);
|
|
237
|
+
else this.readJson(file, text, env);
|
|
238
|
+
}
|
|
239
|
+
readPo(file, text, env) {
|
|
240
|
+
const parsed = parsePo(text, {
|
|
241
|
+
locale: file.locale,
|
|
242
|
+
layout: "source",
|
|
243
|
+
isSourceLocale: false
|
|
244
|
+
});
|
|
245
|
+
for (const d of parsed.diagnostics) if (d.severity === "error") env.diagnostics.push({
|
|
246
|
+
...d,
|
|
247
|
+
file: env.rel(file.file)
|
|
248
|
+
});
|
|
249
|
+
const isSource = file.locale === this.sourceLocale;
|
|
250
|
+
for (const e of parsed.catalog.entries) this.put(file.locale, ...poEntry(e, file.file, isSource));
|
|
251
|
+
}
|
|
252
|
+
readJson(file, text, env) {
|
|
253
|
+
const parsed = parseJson(text);
|
|
254
|
+
if (parsed.problem !== null) {
|
|
255
|
+
const at = {
|
|
256
|
+
file: file.file,
|
|
257
|
+
line: parsed.problem.line
|
|
258
|
+
};
|
|
259
|
+
env.report("catalog-parse", parsed.problem.message, at, "error");
|
|
260
|
+
return;
|
|
261
|
+
}
|
|
262
|
+
if (typeof parsed.value !== "object" || parsed.value === null || Array.isArray(parsed.value)) {
|
|
263
|
+
env.report("catalog-parse", "a Lingui JSON catalog is an object of messages by id", { file: file.file }, "error");
|
|
264
|
+
return;
|
|
265
|
+
}
|
|
266
|
+
const lines = text.split("\n");
|
|
267
|
+
for (const [id, value] of Object.entries(parsed.value)) {
|
|
268
|
+
const line = lines.findIndex((l) => l.includes(`${JSON.stringify(id)}:`)) + 1;
|
|
269
|
+
if (typeof value === "string") {
|
|
270
|
+
const at = {
|
|
271
|
+
translation: value,
|
|
272
|
+
fuzzy: false,
|
|
273
|
+
context: null,
|
|
274
|
+
file: file.file,
|
|
275
|
+
line
|
|
276
|
+
};
|
|
277
|
+
this.put(file.locale, id, {
|
|
278
|
+
message: null,
|
|
279
|
+
...at
|
|
280
|
+
});
|
|
281
|
+
this.textsOf(file.locale).set(id, {
|
|
282
|
+
message: id,
|
|
283
|
+
...at
|
|
284
|
+
});
|
|
285
|
+
} else if (typeof value === "object" && value !== null) this.put(file.locale, id, jsonEntry(value, file.file, line));
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
/** The catalog entry of a message in a locale: by id, else (v3) by its text. */
|
|
289
|
+
lookup(locale, id, { message, context }) {
|
|
290
|
+
let entryFound = this.catalogs.get(locale)?.get(id);
|
|
291
|
+
if (entryFound === void 0 && message !== null) {
|
|
292
|
+
const byMessage = this.byText.get(locale)?.get(message);
|
|
293
|
+
if (byMessage !== void 0 && (byMessage.context ?? null) === context) entryFound = byMessage;
|
|
294
|
+
}
|
|
295
|
+
if (entryFound !== void 0 && locale === this.sourceLocale) {
|
|
296
|
+
this.found.add(entryFound);
|
|
297
|
+
const other = this.catalogs.get(locale)?.get(entryFound.message ?? "");
|
|
298
|
+
if (other !== void 0) this.found.add(other);
|
|
299
|
+
}
|
|
300
|
+
return entryFound;
|
|
301
|
+
}
|
|
302
|
+
/**
|
|
303
|
+
* Registers a message. `message` is the macro's text (null for a runtime
|
|
304
|
+
* lookup by explicit id, whose text comes from the source catalog).
|
|
305
|
+
*/
|
|
306
|
+
define(env, input, origin, tags) {
|
|
307
|
+
const id = input.id ?? linguiId(input.message ?? "", input.context);
|
|
308
|
+
const sourceEntry = this.lookup(this.sourceLocale, id, input);
|
|
309
|
+
if (env.message(id) !== void 0) return { key: id };
|
|
310
|
+
const source = input.message ?? sourceEntry?.message ?? (sourceEntry?.translation || null);
|
|
311
|
+
if (source === null) return { problem: `${id} has no message in the code or the ${this.sourceLocale} catalog` };
|
|
312
|
+
const naming = newNaming();
|
|
313
|
+
const rename = (text, isSource) => {
|
|
314
|
+
if (tags === null) return text;
|
|
315
|
+
let nodes;
|
|
316
|
+
try {
|
|
317
|
+
nodes = env.codec.parse(text);
|
|
318
|
+
} catch (error) {
|
|
319
|
+
if (!isDiagnosticError(error)) throw error;
|
|
320
|
+
return { error: error.diagnostic.message };
|
|
321
|
+
}
|
|
322
|
+
return printICU(renameTags(nodes, naming, tags.pathOf, tags.elementAt, isSource));
|
|
323
|
+
};
|
|
324
|
+
const sourceText = rename(numbered(source), true);
|
|
325
|
+
if (typeof sourceText !== "string") return { problem: `${id}: ${sourceText.error}` };
|
|
326
|
+
const notes = [input.comment, input.context].filter((n) => n !== null && n !== "");
|
|
327
|
+
if (!env.define({
|
|
328
|
+
key: id,
|
|
329
|
+
named: input.id !== null,
|
|
330
|
+
source: sourceText,
|
|
331
|
+
context: notes.length === 0 ? null : notes.join("; "),
|
|
332
|
+
hint: input.context,
|
|
333
|
+
translations: this.translations(id, input, (text) => rename(text, false)),
|
|
334
|
+
file: origin.file,
|
|
335
|
+
line: origin.line
|
|
336
|
+
})) return { problem: `${id} could not be converted` };
|
|
337
|
+
if (tags !== null) this.namings.set(id, naming);
|
|
338
|
+
return { key: id };
|
|
339
|
+
}
|
|
340
|
+
translations(id, input, rename) {
|
|
341
|
+
const translations = /* @__PURE__ */ new Map();
|
|
342
|
+
for (const locale of this.catalogs.keys()) {
|
|
343
|
+
if (locale === this.sourceLocale) continue;
|
|
344
|
+
const found = this.lookup(locale, id, input);
|
|
345
|
+
if (found !== void 0 && found.translation !== "") translations.set(locale, translationOf(found, rename));
|
|
346
|
+
}
|
|
347
|
+
return translations;
|
|
348
|
+
}
|
|
349
|
+
};
|
|
350
|
+
function valuesCode(r, values) {
|
|
351
|
+
const entries = values.map(({ name, expression }) => entry(name, r.src(expression.start, expression.end)));
|
|
352
|
+
return entries.length === 0 ? "" : `, { ${entries.join(", ")} }`;
|
|
353
|
+
}
|
|
354
|
+
/** Attributes of a choice component that are not its cases. */
|
|
355
|
+
const NOT_CASES = /* @__PURE__ */ new Set([
|
|
356
|
+
"value",
|
|
357
|
+
"id",
|
|
358
|
+
"comment",
|
|
359
|
+
"context",
|
|
360
|
+
"render"
|
|
361
|
+
]);
|
|
362
|
+
/** Instance methods with no mechanical counterpart, and what to do instead. */
|
|
363
|
+
function instanceNote(property) {
|
|
364
|
+
if (property === "activate") return "i18n.activate(locale): use setLocale(locale) from @menapo/vi18n";
|
|
365
|
+
if (property === "load" || property === "loadAndActivate") return `i18n.${property}(): vi18n loads catalogs itself; delete this once nothing uses Lingui`;
|
|
366
|
+
return null;
|
|
367
|
+
}
|
|
368
|
+
/** `{ id, context, comment }` of a JSX element's string attributes. */
|
|
369
|
+
function elementNotes(element) {
|
|
370
|
+
const attributes = jsxAttributes(element);
|
|
371
|
+
const read = (name) => attributes.named.has(name) ? attributeString(attributes.named.get(name)?.value ?? null) : null;
|
|
372
|
+
return {
|
|
373
|
+
id: read("id"),
|
|
374
|
+
context: read("context"),
|
|
375
|
+
comment: read("comment")
|
|
376
|
+
};
|
|
377
|
+
}
|
|
378
|
+
/** Whether a JSX child is the line-break whitespace Lingui drops. */
|
|
379
|
+
const isBreak = (c) => c.type === "JSXText" && c.value.trim() === "" && c.value.includes("\n");
|
|
380
|
+
/** One module's Lingui macros and runtime calls, planned as rewrites. */
|
|
381
|
+
var LinguiModule = class {
|
|
382
|
+
adapter;
|
|
383
|
+
env;
|
|
384
|
+
file;
|
|
385
|
+
text;
|
|
386
|
+
program;
|
|
387
|
+
info;
|
|
388
|
+
builder;
|
|
389
|
+
tsx;
|
|
390
|
+
macroT;
|
|
391
|
+
macroMsg;
|
|
392
|
+
choiceMacros = /* @__PURE__ */ new Map();
|
|
393
|
+
transMacro;
|
|
394
|
+
choiceComponents = /* @__PURE__ */ new Map();
|
|
395
|
+
providers;
|
|
396
|
+
runtimeI18n;
|
|
397
|
+
linguiT = /* @__PURE__ */ new Set();
|
|
398
|
+
linguiUnderscore = /* @__PURE__ */ new Set();
|
|
399
|
+
linguiInstances = /* @__PURE__ */ new Set();
|
|
400
|
+
/** Locals holding `msg`/`defineMessage` results: their members are `msg()` refs now. */
|
|
401
|
+
messageTables = /* @__PURE__ */ new Set();
|
|
402
|
+
relativeImports = /* @__PURE__ */ new Set();
|
|
403
|
+
/** Choice macros and components a message absorbed: not sites of their own. */
|
|
404
|
+
absorbed = /* @__PURE__ */ new Set();
|
|
405
|
+
constructor(adapter, env, { file, text, program }) {
|
|
406
|
+
this.adapter = adapter;
|
|
407
|
+
this.env = env;
|
|
408
|
+
this.file = file;
|
|
409
|
+
this.text = text;
|
|
410
|
+
this.program = program;
|
|
411
|
+
this.info = analyzeModule(program);
|
|
412
|
+
this.builder = new ModuleBuilder({
|
|
413
|
+
file,
|
|
414
|
+
text,
|
|
415
|
+
info: this.info,
|
|
416
|
+
isOldSource
|
|
417
|
+
});
|
|
418
|
+
this.tsx = extname(file) === ".tsx";
|
|
419
|
+
this.macroT = this.imported("t", true);
|
|
420
|
+
this.macroMsg = /* @__PURE__ */ new Set([...this.imported("msg", true), ...this.imported("defineMessage", true)]);
|
|
421
|
+
for (const [name, format] of [
|
|
422
|
+
["plural", "plural"],
|
|
423
|
+
["select", "select"],
|
|
424
|
+
["selectOrdinal", "selectordinal"]
|
|
425
|
+
]) for (const local of this.imported(name, true)) this.choiceMacros.set(local, format);
|
|
426
|
+
this.transMacro = this.imported("Trans", true);
|
|
427
|
+
for (const [name, format] of [
|
|
428
|
+
["Plural", "plural"],
|
|
429
|
+
["Select", "select"],
|
|
430
|
+
["SelectOrdinal", "selectordinal"]
|
|
431
|
+
]) for (const local of this.imported(name, true)) this.choiceComponents.set(local, format);
|
|
432
|
+
this.providers = this.imported("I18nProvider");
|
|
433
|
+
this.runtimeI18n = this.imported("i18n");
|
|
434
|
+
}
|
|
435
|
+
plan() {
|
|
436
|
+
const useLingui = this.imported("useLingui");
|
|
437
|
+
for (const binding of this.info.bindings) {
|
|
438
|
+
const init = binding.declarator?.init;
|
|
439
|
+
if (init === null || init === void 0) continue;
|
|
440
|
+
this.hookBinding(binding, unwrap(init), useLingui);
|
|
441
|
+
if (this.holdsMsg(init)) this.messageTables.add(binding.name);
|
|
442
|
+
}
|
|
443
|
+
for (const declaration of this.info.imports) {
|
|
444
|
+
if (!declaration.source.startsWith(".")) continue;
|
|
445
|
+
for (const s of declaration.specifiers) this.relativeImports.add(s.local);
|
|
446
|
+
}
|
|
447
|
+
this.visit(this.program);
|
|
448
|
+
return this.builder.finish();
|
|
449
|
+
}
|
|
450
|
+
imported(name, macro = null) {
|
|
451
|
+
const locals = /* @__PURE__ */ new Set();
|
|
452
|
+
for (const declaration of this.info.imports) {
|
|
453
|
+
if (!isOldSource(declaration.source)) continue;
|
|
454
|
+
if (macro !== null && MACRO_SOURCES.has(declaration.source) !== macro) continue;
|
|
455
|
+
for (const s of declaration.specifiers) if (s.imported === name) locals.add(s.local);
|
|
456
|
+
}
|
|
457
|
+
return locals;
|
|
458
|
+
}
|
|
459
|
+
/** A `useLingui()` binding: its `t`, `_` or `i18n`. */
|
|
460
|
+
hookBinding(binding, call, useLingui) {
|
|
461
|
+
const callee = call.type === "CallExpression" ? identifierName(call.callee) : null;
|
|
462
|
+
if (callee === null || !useLingui.has(callee)) return;
|
|
463
|
+
this.builder.hooks.push(binding);
|
|
464
|
+
const slot = binding.slot;
|
|
465
|
+
const key = slot !== null && slot.type === "Property" ? keyName(slot) : null;
|
|
466
|
+
if (key === "t") this.linguiT.add(binding);
|
|
467
|
+
else if (key === "_") this.linguiUnderscore.add(binding);
|
|
468
|
+
else if (key === "i18n") this.linguiInstances.add(binding);
|
|
469
|
+
}
|
|
470
|
+
/** Whether an initializer holds a `msg`/`defineMessage` result anywhere inside. */
|
|
471
|
+
holdsMsg(n) {
|
|
472
|
+
if (this.isMsgMacro(n)) return true;
|
|
473
|
+
for (const [, child] of childEntries(n)) if (this.holdsMsg(child)) return true;
|
|
474
|
+
return false;
|
|
475
|
+
}
|
|
476
|
+
isMsgMacro(n) {
|
|
477
|
+
const callee = macroCallee(n);
|
|
478
|
+
const name = callee === null ? null : identifierName(callee);
|
|
479
|
+
return name !== null && this.macroMsg.has(name);
|
|
480
|
+
}
|
|
481
|
+
lineOf(offset) {
|
|
482
|
+
return this.text.slice(0, offset).split("\n").length;
|
|
483
|
+
}
|
|
484
|
+
define(input, offset, tags) {
|
|
485
|
+
const origin = {
|
|
486
|
+
file: this.file,
|
|
487
|
+
line: this.lineOf(offset)
|
|
488
|
+
};
|
|
489
|
+
return this.adapter.define(this.env, input, origin, tags);
|
|
490
|
+
}
|
|
491
|
+
resolve(name, offset) {
|
|
492
|
+
let best = null;
|
|
493
|
+
for (const b of this.info.bindings) {
|
|
494
|
+
if (b.name !== name || offset < b.scope.start || offset >= b.scope.end) continue;
|
|
495
|
+
if (best === null || b.scope.end - b.scope.start < best.scope.end - best.scope.start) best = b;
|
|
496
|
+
}
|
|
497
|
+
return best;
|
|
498
|
+
}
|
|
499
|
+
isMacroT(callee) {
|
|
500
|
+
const n = unwrap(callee);
|
|
501
|
+
if (n.type === "CallExpression") return this.isMacroT(n.callee);
|
|
502
|
+
if (n.type !== "Identifier") return false;
|
|
503
|
+
const binding = this.resolve(n.name, n.start);
|
|
504
|
+
if (binding === null) return false;
|
|
505
|
+
return this.macroT.has(n.name) ? binding.kind === "import" : this.linguiT.has(binding);
|
|
506
|
+
}
|
|
507
|
+
calleeNodes(callee) {
|
|
508
|
+
const n = unwrap(callee);
|
|
509
|
+
if (n.type === "CallExpression") return this.calleeNodes(n.callee);
|
|
510
|
+
return n.type === "Identifier" ? [n] : [];
|
|
511
|
+
}
|
|
512
|
+
template(quasi, out) {
|
|
513
|
+
const quasis = quasi.quasis;
|
|
514
|
+
const expressions = quasi.expressions;
|
|
515
|
+
for (let i = 0; i < quasis.length; i++) {
|
|
516
|
+
out.text += quasis[i].value.cooked ?? "";
|
|
517
|
+
const expression = expressions[i];
|
|
518
|
+
if (expression !== void 0) this.placeholder(expression, out);
|
|
519
|
+
}
|
|
520
|
+
}
|
|
521
|
+
placeholder(expression, out) {
|
|
522
|
+
const n = unwrap(expression);
|
|
523
|
+
const callee = n.type === "CallExpression" ? identifierName(n.callee) : null;
|
|
524
|
+
const format = callee === null ? void 0 : this.choiceMacros.get(callee);
|
|
525
|
+
if (format === void 0) {
|
|
526
|
+
out.text += `{${out.argument(n)}}`;
|
|
527
|
+
return;
|
|
528
|
+
}
|
|
529
|
+
this.absorbed.add(n);
|
|
530
|
+
out.shared.consumed.push(unwrap(n.callee));
|
|
531
|
+
this.choice(n, format, out);
|
|
532
|
+
}
|
|
533
|
+
/** A case's text: a string as is, a template or other expression as the macros build it. */
|
|
534
|
+
caseText(v, inner) {
|
|
535
|
+
const literal = stringValue(v);
|
|
536
|
+
if (literal !== null) inner.text = literal;
|
|
537
|
+
else if (v.type === "TemplateLiteral") this.template(v, inner);
|
|
538
|
+
else this.placeholder(v, inner);
|
|
539
|
+
}
|
|
540
|
+
/** `plural(value, { one: "…", other: \`…\` })` as `{value, plural, one {…} other {…}}`. */
|
|
541
|
+
choice(call, format, out) {
|
|
542
|
+
const [value, options] = call.arguments;
|
|
543
|
+
const props = options === void 0 ? null : objectProperties(options);
|
|
544
|
+
if (value === void 0 || props === null || props.spreads.length > 0) {
|
|
545
|
+
out.fail(`${format}() needs a value and an object literal of cases`);
|
|
546
|
+
return;
|
|
547
|
+
}
|
|
548
|
+
const name = out.argument(value);
|
|
549
|
+
const cases = [];
|
|
550
|
+
for (const [key, property] of props.properties) {
|
|
551
|
+
const v = unwrap(property.value);
|
|
552
|
+
if (key === "offset") {
|
|
553
|
+
cases.unshift(`offset:${String(v.value)}`);
|
|
554
|
+
continue;
|
|
555
|
+
}
|
|
556
|
+
const inner = out.nested();
|
|
557
|
+
this.caseText(v, inner);
|
|
558
|
+
cases.push(`${choiceKey(key)} {${inner.text}}`);
|
|
559
|
+
}
|
|
560
|
+
out.text += `{${name}, ${format}, ${cases.join(" ")}}`;
|
|
561
|
+
}
|
|
562
|
+
/** JSX children of `<Trans>` (or a choice component's JSX option). */
|
|
563
|
+
jsx(children, out) {
|
|
564
|
+
for (const child of children) if (child.type === "JSXText") out.text += decodeEntities(child.value);
|
|
565
|
+
else if (child.type === "JSXExpressionContainer") this.jsxExpression(child, out);
|
|
566
|
+
else if (child.type === "JSXElement") this.jsxElement(child, out);
|
|
567
|
+
else if (child.type === "JSXFragment") this.jsx(child.children, out);
|
|
568
|
+
}
|
|
569
|
+
jsxExpression(child, out) {
|
|
570
|
+
const expression = unwrap(child.expression);
|
|
571
|
+
if (expression.type !== "JSXEmptyExpression") this.caseText(expression, out);
|
|
572
|
+
}
|
|
573
|
+
/** An element in a message: a choice component inlined, else a numbered tag. */
|
|
574
|
+
jsxElement(child, out) {
|
|
575
|
+
const format = this.choiceComponents.get(jsxName(child));
|
|
576
|
+
if (format !== void 0) {
|
|
577
|
+
this.absorbed.add(child);
|
|
578
|
+
out.shared.consumed.push(...jsxNameNodes(child));
|
|
579
|
+
this.choiceComponent(child, format, out);
|
|
580
|
+
return;
|
|
581
|
+
}
|
|
582
|
+
const index = out.elements.length;
|
|
583
|
+
out.elements.push(child);
|
|
584
|
+
if (child.children.filter((c) => !isBreak(c)).length === 0) {
|
|
585
|
+
out.text += `<${index}/>`;
|
|
586
|
+
return;
|
|
587
|
+
}
|
|
588
|
+
out.text += `<${index}>`;
|
|
589
|
+
this.jsx(child.children, out);
|
|
590
|
+
out.text += `</${index}>`;
|
|
591
|
+
}
|
|
592
|
+
/** `<Plural value={n} one="…" other={<b>…</b>} />` as an ICU plural. */
|
|
593
|
+
choiceComponent(element, format, out) {
|
|
594
|
+
const attributes = jsxAttributes(element);
|
|
595
|
+
const value = attributeExpression(attributes.named.get("value")?.value ?? null);
|
|
596
|
+
if (value === null || attributes.spreads.length > 0) {
|
|
597
|
+
out.fail(`<${jsxName(element)}> needs a value={…} and no spread props`);
|
|
598
|
+
return;
|
|
599
|
+
}
|
|
600
|
+
const name = out.argument(value);
|
|
601
|
+
const cases = [];
|
|
602
|
+
for (const [key, { value: attribute }] of attributes.named) {
|
|
603
|
+
if (NOT_CASES.has(key)) continue;
|
|
604
|
+
if (key === "offset") {
|
|
605
|
+
const offset = attributeString(attribute) ?? String(attributeExpression(attribute)?.value);
|
|
606
|
+
cases.unshift(`offset:${offset}`);
|
|
607
|
+
continue;
|
|
608
|
+
}
|
|
609
|
+
const inner = out.nested();
|
|
610
|
+
if (!this.attributeCase(attribute, inner)) continue;
|
|
611
|
+
cases.push(`${choiceKey(key)} {${inner.text}}`);
|
|
612
|
+
}
|
|
613
|
+
out.text += `{${name}, ${format}, ${cases.join(" ")}}`;
|
|
614
|
+
}
|
|
615
|
+
/** A choice component's case attribute as text; false when it holds no expression. */
|
|
616
|
+
attributeCase(attribute, inner) {
|
|
617
|
+
const literal = attributeString(attribute);
|
|
618
|
+
if (literal !== null) {
|
|
619
|
+
inner.text = literal;
|
|
620
|
+
return true;
|
|
621
|
+
}
|
|
622
|
+
const expression = attributeExpression(attribute);
|
|
623
|
+
if (expression === null) return false;
|
|
624
|
+
const n = unwrap(expression);
|
|
625
|
+
if (n.type === "JSXElement" || n.type === "JSXFragment") this.jsx([n], inner);
|
|
626
|
+
else this.caseText(n, inner);
|
|
627
|
+
return true;
|
|
628
|
+
}
|
|
629
|
+
/** `{ id, message, context, comment }` of a macro descriptor (message a string or template). */
|
|
630
|
+
descriptor(node, out) {
|
|
631
|
+
const props = objectProperties(node);
|
|
632
|
+
if (props === null || props.spreads.length > 0) return null;
|
|
633
|
+
const field = (name) => {
|
|
634
|
+
const property = props.properties.get(name);
|
|
635
|
+
if (property === void 0) return null;
|
|
636
|
+
return stringValue(property.value) ?? void 0;
|
|
637
|
+
};
|
|
638
|
+
const id = field("id");
|
|
639
|
+
const context = field("context");
|
|
640
|
+
const comment = field("comment");
|
|
641
|
+
if (id === void 0 || context === void 0 || comment === void 0) return null;
|
|
642
|
+
const messageProperty = props.properties.get("message");
|
|
643
|
+
if (messageProperty === void 0) return {
|
|
644
|
+
id,
|
|
645
|
+
message: null,
|
|
646
|
+
context,
|
|
647
|
+
comment
|
|
648
|
+
};
|
|
649
|
+
const value = unwrap(messageProperty.value);
|
|
650
|
+
const literal = stringValue(value);
|
|
651
|
+
if (literal !== null) out.text = literal;
|
|
652
|
+
else if (value.type === "TemplateLiteral") this.template(value, out);
|
|
653
|
+
else return null;
|
|
654
|
+
return {
|
|
655
|
+
id,
|
|
656
|
+
message: out.text,
|
|
657
|
+
context,
|
|
658
|
+
comment
|
|
659
|
+
};
|
|
660
|
+
}
|
|
661
|
+
/** A `t` macro (template or descriptor) or a standalone choice macro, as `t()`. */
|
|
662
|
+
tSite(node, built, notes, consume) {
|
|
663
|
+
if (built.problem !== null) {
|
|
664
|
+
this.builder.note(node.start, `${built.problem}; not rewritten`);
|
|
665
|
+
return;
|
|
666
|
+
}
|
|
667
|
+
const defined = this.define({
|
|
668
|
+
...notes,
|
|
669
|
+
message: built.message
|
|
670
|
+
}, node.start, null);
|
|
671
|
+
if ("problem" in defined) {
|
|
672
|
+
this.builder.note(node.start, `${defined.problem}; not rewritten`);
|
|
673
|
+
return;
|
|
674
|
+
}
|
|
675
|
+
this.builder.site(node, (r) => `${r.fn.t}(${r.message(defined.key)}${valuesCode(r, built.values)})`, {
|
|
676
|
+
keys: [defined.key],
|
|
677
|
+
uses: ["t"],
|
|
678
|
+
consume: [...consume, ...built.consumed]
|
|
679
|
+
});
|
|
680
|
+
}
|
|
681
|
+
/** `<Trans>` (or a choice component) whose message is `built`, as `rich()` or `t()`. */
|
|
682
|
+
jsxSite(element, built, notes) {
|
|
683
|
+
const tag = `<${jsxName(element)}>`;
|
|
684
|
+
if (built.problem !== null) {
|
|
685
|
+
this.builder.note(element.start, `${tag}: ${built.problem}; not rewritten`);
|
|
686
|
+
return;
|
|
687
|
+
}
|
|
688
|
+
const elements = built.elements;
|
|
689
|
+
const pathOf = (name) => /^t\d+$/.test(name) ? name.slice(1) : null;
|
|
690
|
+
const elementAt = (path) => elements[Number(path)] ?? null;
|
|
691
|
+
const defined = this.define({
|
|
692
|
+
...notes,
|
|
693
|
+
message: built.message
|
|
694
|
+
}, element.start, {
|
|
695
|
+
pathOf,
|
|
696
|
+
elementAt
|
|
697
|
+
});
|
|
698
|
+
if ("problem" in defined) {
|
|
699
|
+
this.builder.note(element.start, `${defined.problem}; ${tag} was not rewritten`);
|
|
700
|
+
return;
|
|
701
|
+
}
|
|
702
|
+
const message = this.env.message(defined.key);
|
|
703
|
+
const naming = this.adapter.namings.get(defined.key);
|
|
704
|
+
if (message === void 0 || naming === void 0) return;
|
|
705
|
+
const { handlers, missing } = siteHandlers(naming, elementAt);
|
|
706
|
+
if (missing.length > 0) {
|
|
707
|
+
const problem = `${tag}: a tag maps to no element here; not rewritten`;
|
|
708
|
+
this.builder.note(element.start, problem, [defined.key]);
|
|
709
|
+
return;
|
|
710
|
+
}
|
|
711
|
+
const selfClosing = selfClosingTags(this.env.codec.parse(message.canonical));
|
|
712
|
+
const rich = handlers.size > 0;
|
|
713
|
+
const types = rich && this.tsx ? reactTypes(this.builder) : null;
|
|
714
|
+
const info = this.info;
|
|
715
|
+
this.builder.site(element, (r) => {
|
|
716
|
+
const handlerEntries = [...handlers].map(([name, handler]) => entry(name, elementHandler(handler, selfClosing.has(name), r, types)));
|
|
717
|
+
const values = built.values.map(({ name, expression }) => entry(name, r.src(expression.start, expression.end)));
|
|
718
|
+
const call = rich ? richCall(r, defined.key, [...handlerEntries, ...values], types) : `${r.fn.t}(${r.message(defined.key)}${valuesCode(r, built.values)})`;
|
|
719
|
+
return inJsxChildren(info, element) ? `{${call}}` : call;
|
|
720
|
+
}, {
|
|
721
|
+
keys: [defined.key],
|
|
722
|
+
uses: [rich ? "rich" : "t"],
|
|
723
|
+
consume: [...jsxNameNodes(element), ...built.consumed]
|
|
724
|
+
});
|
|
725
|
+
}
|
|
726
|
+
transElement(element) {
|
|
727
|
+
const attributes = jsxAttributes(element);
|
|
728
|
+
const { named } = attributes;
|
|
729
|
+
if (attributes.spreads.length > 0 || named.has("render") || named.has("component")) {
|
|
730
|
+
this.builder.note(element.start, "<Trans> with spread props, render or component was not rewritten");
|
|
731
|
+
return;
|
|
732
|
+
}
|
|
733
|
+
const out = new MessageText();
|
|
734
|
+
this.jsx(element.children, out);
|
|
735
|
+
out.text = normalizeWhitespace(out.text);
|
|
736
|
+
this.jsxSite(element, out.built(), elementNotes(element));
|
|
737
|
+
}
|
|
738
|
+
choiceElement(element, format) {
|
|
739
|
+
const out = new MessageText();
|
|
740
|
+
this.choiceComponent(element, format, out);
|
|
741
|
+
this.jsxSite(element, out.built(), elementNotes(element));
|
|
742
|
+
}
|
|
743
|
+
/** `msg`/`defineMessage` as `msg()`: only without placeholders (a vi18n descriptor holds no values). */
|
|
744
|
+
msgSite(node, out, notes) {
|
|
745
|
+
if (out.problem !== null || out.values.length > 0) {
|
|
746
|
+
this.builder.note(node.start, out.problem ?? "msg/defineMessage with placeholders captures values, which a vi18n msg() descriptor does not: pass them to t() where it is rendered; not rewritten");
|
|
747
|
+
return;
|
|
748
|
+
}
|
|
749
|
+
const defined = this.define({
|
|
750
|
+
...notes,
|
|
751
|
+
message: out.text
|
|
752
|
+
}, node.start, null);
|
|
753
|
+
if ("problem" in defined) {
|
|
754
|
+
this.builder.note(node.start, `${defined.problem}; not rewritten`);
|
|
755
|
+
return;
|
|
756
|
+
}
|
|
757
|
+
const callee = macroCallee(node);
|
|
758
|
+
this.builder.site(node, (r) => `${r.fn.msg}(${r.message(defined.key)})`, {
|
|
759
|
+
keys: [defined.key],
|
|
760
|
+
uses: ["msg"],
|
|
761
|
+
consume: this.calleeNodes(callee)
|
|
762
|
+
});
|
|
763
|
+
}
|
|
764
|
+
/** The nodes a runtime `i18n._` / `i18n.t` / `_` callee consumes; null when it is not Lingui's. */
|
|
765
|
+
runtimeCallee(callee) {
|
|
766
|
+
if (callee.type === "Identifier") {
|
|
767
|
+
const binding = this.resolve(callee.name, callee.start);
|
|
768
|
+
return binding !== null && this.linguiUnderscore.has(binding) ? [callee] : null;
|
|
769
|
+
}
|
|
770
|
+
const member = memberParts(callee);
|
|
771
|
+
if (member === null || member.property !== "_" && member.property !== "t") return null;
|
|
772
|
+
let root = unwrap(member.object);
|
|
773
|
+
while (root.type === "MemberExpression") root = unwrap(root.object);
|
|
774
|
+
const name = identifierName(root);
|
|
775
|
+
if (name === null) return null;
|
|
776
|
+
const binding = this.resolve(name, root.start);
|
|
777
|
+
if (!(this.runtimeI18n.has(name) || binding !== null && this.linguiInstances.has(binding))) return null;
|
|
778
|
+
const object = unwrap(member.object);
|
|
779
|
+
return object.type === "Identifier" ? [object] : [];
|
|
780
|
+
}
|
|
781
|
+
/** A runtime call's message: a defined key, null for a ref passed through, or a problem to report. */
|
|
782
|
+
runtimeMessage(node, n) {
|
|
783
|
+
const id = stringValue(n);
|
|
784
|
+
if (id !== null) {
|
|
785
|
+
const defined = this.define({
|
|
786
|
+
id,
|
|
787
|
+
message: null,
|
|
788
|
+
context: null,
|
|
789
|
+
comment: null
|
|
790
|
+
}, node.start, null);
|
|
791
|
+
return "problem" in defined ? { problem: `${defined.problem}; not rewritten` } : defined;
|
|
792
|
+
}
|
|
793
|
+
if (n.type === "ObjectExpression") {
|
|
794
|
+
const out = new MessageText();
|
|
795
|
+
const d = this.descriptor(n, out);
|
|
796
|
+
if (d === null || out.values.length > 0) return { problem: "a runtime descriptor vi18n migrate cannot read was not rewritten" };
|
|
797
|
+
const defined = this.define(d, node.start, null);
|
|
798
|
+
return "problem" in defined ? { problem: `${defined.problem}; not rewritten` } : defined;
|
|
799
|
+
}
|
|
800
|
+
let root = n;
|
|
801
|
+
while (root.type === "MemberExpression") root = unwrap(root.object);
|
|
802
|
+
const name = identifierName(root);
|
|
803
|
+
if (name !== null && (this.messageTables.has(name) || this.relativeImports.has(name)) || this.isMsgMacro(n)) return { key: null };
|
|
804
|
+
return { problem: "i18n._() with a message vi18n migrate cannot trace was not rewritten: pass a msg() descriptor to t()" };
|
|
805
|
+
}
|
|
806
|
+
/** `i18n._(…)` / `i18n.t(…)` / `_(…)` at runtime. */
|
|
807
|
+
runtimeCall(node) {
|
|
808
|
+
const consume = this.runtimeCallee(unwrap(node.callee));
|
|
809
|
+
if (consume === null) return false;
|
|
810
|
+
const args = node.arguments;
|
|
811
|
+
const first = args[0];
|
|
812
|
+
if (first === void 0 || first.type === "SpreadElement") return false;
|
|
813
|
+
const values = args[1] ?? null;
|
|
814
|
+
const message = this.runtimeMessage(node, unwrap(first));
|
|
815
|
+
if ("problem" in message) {
|
|
816
|
+
this.builder.note(node.start, message.problem);
|
|
817
|
+
return true;
|
|
818
|
+
}
|
|
819
|
+
const { key } = message;
|
|
820
|
+
this.builder.site(node, (r) => {
|
|
821
|
+
const code = key === null ? r.src(first.start, first.end) : r.message(key);
|
|
822
|
+
return `${r.fn.t}(${code}${values === null ? "" : `, ${r.src(values.start, values.end)}`})`;
|
|
823
|
+
}, {
|
|
824
|
+
keys: key === null ? [] : [key],
|
|
825
|
+
uses: ["t"],
|
|
826
|
+
consume
|
|
827
|
+
});
|
|
828
|
+
return true;
|
|
829
|
+
}
|
|
830
|
+
unwrapProvider(element) {
|
|
831
|
+
const opening = element.openingElement;
|
|
832
|
+
const closing = element.closingElement;
|
|
833
|
+
const children = element.children.filter((c) => !(c.type === "JSXText" && c.value.trim() === ""));
|
|
834
|
+
this.builder.consumeRange(opening);
|
|
835
|
+
this.builder.consume(jsxNameNodes(element));
|
|
836
|
+
const nested = inJsxChildren(this.info, element);
|
|
837
|
+
this.builder.replace(element, (r) => {
|
|
838
|
+
if (closing === null) return nested ? "" : "null";
|
|
839
|
+
if (nested) return r.src(opening.end, closing.start).trim();
|
|
840
|
+
if (children.length === 1 && children[0].type === "JSXElement") return r.src(children[0].start, children[0].end);
|
|
841
|
+
return `<>${r.src(opening.end, closing.start)}</>`;
|
|
842
|
+
});
|
|
843
|
+
}
|
|
844
|
+
taggedTemplate(node) {
|
|
845
|
+
const tag = node.tag;
|
|
846
|
+
const name = identifierName(tag);
|
|
847
|
+
const isT = this.isMacroT(tag);
|
|
848
|
+
if (!isT && (name === null || !this.macroMsg.has(name))) return;
|
|
849
|
+
const out = new MessageText();
|
|
850
|
+
this.template(node.quasi, out);
|
|
851
|
+
if (isT) this.tSite(node, out.built(), NO_NOTES, this.calleeNodes(tag));
|
|
852
|
+
else this.msgSite(node, out, NO_NOTES);
|
|
853
|
+
}
|
|
854
|
+
/** `t({ … })`, `msg({ … })`, a standalone choice macro; else a runtime call or setup. */
|
|
855
|
+
call(node) {
|
|
856
|
+
const callee = node.callee;
|
|
857
|
+
const name = identifierName(callee);
|
|
858
|
+
const arg = node.arguments[0];
|
|
859
|
+
const objectArg = arg !== void 0 && unwrap(arg).type === "ObjectExpression";
|
|
860
|
+
if (this.isMacroT(callee) && unwrap(callee).type === "Identifier" && objectArg) {
|
|
861
|
+
const out = new MessageText();
|
|
862
|
+
const d = this.descriptor(unwrap(arg), out);
|
|
863
|
+
if (d === null) this.builder.note(node.start, "t({…}) with a computed descriptor was not rewritten");
|
|
864
|
+
else this.tSite(node, {
|
|
865
|
+
...out.built(),
|
|
866
|
+
message: d.message ?? ""
|
|
867
|
+
}, d, this.calleeNodes(callee));
|
|
868
|
+
} else if (name !== null && this.macroMsg.has(name) && arg !== void 0) {
|
|
869
|
+
const out = new MessageText();
|
|
870
|
+
const d = objectArg ? this.descriptor(unwrap(arg), out) : null;
|
|
871
|
+
if (d === null) this.builder.note(node.start, `${name}() with a computed descriptor was not rewritten`);
|
|
872
|
+
else this.msgSite(node, out, d);
|
|
873
|
+
} else if (name !== null && this.choiceMacros.has(name) && !this.absorbed.has(node)) {
|
|
874
|
+
const out = new MessageText();
|
|
875
|
+
this.choice(node, this.choiceMacros.get(name), out);
|
|
876
|
+
this.tSite(node, out.built(), NO_NOTES, this.calleeNodes(callee));
|
|
877
|
+
} else if (!this.runtimeCall(node)) this.setupCall(node, name);
|
|
878
|
+
}
|
|
879
|
+
/** `i18n.activate()`, `i18n.load()`, `setupI18n()`: reported with what to do instead. */
|
|
880
|
+
setupCall(node, name) {
|
|
881
|
+
const member = memberParts(node.callee);
|
|
882
|
+
const note = member === null ? null : instanceNote(member.property);
|
|
883
|
+
if (member !== null && note !== null) {
|
|
884
|
+
const object = identifierName(member.object);
|
|
885
|
+
if (object !== null && (this.runtimeI18n.has(object) || object === "i18n")) this.builder.note(node.start, note);
|
|
886
|
+
} else if (name !== null && this.imported("setupI18n").has(name)) this.builder.note(node.start, "setupI18n(): use createTranslator(locale) or withLocale() from @menapo/vi18n/server");
|
|
887
|
+
}
|
|
888
|
+
element(node) {
|
|
889
|
+
const name = jsxName(node);
|
|
890
|
+
if (this.transMacro.has(name)) this.transElement(node);
|
|
891
|
+
else if (this.choiceComponents.has(name) && !this.absorbed.has(node)) this.choiceElement(node, this.choiceComponents.get(name));
|
|
892
|
+
else if (this.providers.has(name)) this.unwrapProvider(node);
|
|
893
|
+
else if (this.imported("Trans", false).has(name)) this.builder.note(node.start, "<Trans id> from @lingui/react (the runtime component) was not rewritten: use t() or rich() with the message");
|
|
894
|
+
}
|
|
895
|
+
visit(node) {
|
|
896
|
+
if (node.type === "TaggedTemplateExpression") this.taggedTemplate(node);
|
|
897
|
+
else if (node.type === "CallExpression") this.call(node);
|
|
898
|
+
else if (node.type === "JSXElement") this.element(node);
|
|
899
|
+
for (const [, child] of childEntries(node)) this.visit(child);
|
|
900
|
+
}
|
|
901
|
+
};
|
|
902
|
+
export { createLinguiAdapter };
|