@menapo/vi18n 0.1.1 → 0.1.2
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/{apply-Bre-lY9J.js → apply-FAHl1R6T.js} +69 -89
- package/dist/{captured-Bj8bO6vd.js → captured-CTjjDF5o.js} +196 -217
- package/dist/{check-BIKEuu6B.js → check-DwFwOB1d.js} +66 -84
- package/dist/cli.js +2 -3
- package/dist/{collect-Cx4_SKOe.js → collect-CYATDjdo.js} +1 -2
- package/dist/collisions-uMTuq36M.js +215 -0
- package/dist/compile-CEvH-6sY.js +240 -0
- package/dist/{compile-Cc97JaW2.js → compile-DqkofbiO.js} +376 -331
- 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-1onDF6Ua.js} +26 -26
- package/dist/{dir-zw6bGMKQ.js → dir-TGLYuOZb.js} +34 -37
- package/dist/{duplicates-yuRljQ6h.js → duplicates-CsUXRX2J.js} +10 -27
- package/dist/engine-Bf7nVigL.js +556 -0
- package/dist/engine.d.ts +1 -5
- package/dist/engine.js +297 -168
- package/dist/exchange-DYG9RZoT.js +41 -0
- package/dist/{exclude-Du-Zd_qI.js → exclude-vMnm3lqU.js} +2 -3
- package/dist/{export-C9Sy6UWv.js → export-BYq0gfLd.js} +17 -38
- package/dist/{extract-C3S8btx0.js → extract-CYtuEo8u.js} +114 -82
- package/dist/{formats-9unqj_fZ.js → formats-ChV1LVH_.js} +352 -276
- package/dist/fs-ezCMZeDV.js +52 -0
- package/dist/i18next-1waHziMy.js +1 -0
- package/dist/i18next-D66Icz3L.js +1907 -0
- package/dist/{import-nANvQutw.js → import-BApbQlmm.js} +81 -75
- package/dist/index.d.ts +1 -13
- package/dist/index.js +14 -16
- package/dist/{init-C2ScuW6a.js → init-J7AwwA5e.js} +466 -501
- package/dist/{library-D5CWvTQs.js → library-BfSOBeBd.js} +975 -451
- package/dist/lingui-Ee2farVo.js +902 -0
- package/dist/lint.d.ts +0 -4
- package/dist/lint.js +68 -81
- package/dist/{load-gW-oJt1Q.js → load-BFLLqY8x.js} +272 -147
- package/dist/{load-CQRz4PpM.js → load-CMwhukZ4.js} +1211 -1115
- package/dist/{locales-DX4MOBFE.js → locales-DxLDrx-h.js} +271 -237
- package/dist/{lsp-RMMMc88P.js → lsp-BC7xDGON.js} +4 -5
- package/dist/{match-BGr8EXB3.js → match-BLDcj610.js} +11 -4
- package/dist/{mcp-CHVwBoRP.js → mcp-Cka8ywDF.js} +4 -5
- package/dist/{merge-driver-BTX87aVm.js → merge-driver-B04Apm29.js} +138 -122
- package/dist/migrate-D7TNl6SD.js +2169 -0
- package/dist/migrate-khCY8TE0.js +1 -0
- package/dist/{openai-CTaMDcl-.js → openai-RWw0YlAx.js} +128 -87
- package/dist/{output-DWE3SIUA.js → output-CF9uvk_-.js} +15 -9
- package/dist/parse.d.ts +1 -3
- package/dist/parse.js +7 -8
- package/dist/{pipeline-DbX6rlo5.js → pipeline-B30irGMD.js} +1 -1
- package/dist/{pipeline-C9Dbotkh.js → pipeline-sgl_q_op.js} +387 -359
- package/dist/project-DSzWKU3C.js +544 -0
- package/dist/{pseudo-DRv30iN5.js → pseudo-DNbNNVGP.js} +125 -3
- package/dist/{raw-C4Y6vyGY.js → raw-YsYZMS9n.js} +52 -29
- package/dist/react-intl-tzGIk6G3.js +634 -0
- package/dist/restore-BG_zMpSj.js +199 -0
- package/dist/rewrite-hAcRJwiZ.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-CFzR3V93.js +647 -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-deBOq2vq.js → server-WlxZH0oX.js} +275 -330
- package/dist/{server-Da1Ot9IN.js → server-ceuzzkal.js} +292 -386
- package/dist/server.d.ts +1 -17
- package/dist/server.js +415 -381
- package/dist/{sitemap-DJXzKoza.js → sitemap-796jJKUT.js} +21 -33
- package/dist/{sources-D19czDSA.js → sources-DuOdM02f.js} +712 -373
- package/dist/{status-zr4jY59Y.js → status-CTH3qI-0.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-CpL0Qsgw.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 +77 -48
- package/dist/translate-BrfVRkjl.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-BaG1LboH.d.ts} +2 -6
- package/dist/{types-CgUCkFU1.d.ts → types-Cy2bPdSg.d.ts} +14 -20
- package/dist/types-Ds_vebja.js +41 -0
- package/dist/{units-CQ5Q99RC.js → units-VVLgQ7x7.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-D6DJepZv.js → validate-PGgMsasz.js} +3 -5
- package/dist/{validate-BxpupdAf.js → validate-zv4-IZyZ.js} +690 -377
- package/dist/{version-Byqi2guX.js → version-BFs370Ce.js} +3 -5
- package/dist/{vite-scan-CT_vsp4g.js → vite-scan-DFKSGVE9.js} +87 -92
- package/dist/vite.d.ts +2 -6
- package/dist/vite.js +6482 -6018
- package/dist/vue-i18n-CBmZqHbG.js +806 -0
- package/dist/{which-BOemZ5Q8.js → which-BpyJyBkX.js} +44 -37
- package/dist/{worker-84U6uYB3.js → worker-BjzVqJVO.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,556 @@
|
|
|
1
|
+
import { n as isDiagnosticError } from "./diagnostics-1onDF6Ua.js";
|
|
2
|
+
import { F as isIncluded, I as listProjectFiles } from "./library-BfSOBeBd.js";
|
|
3
|
+
import { i as rootRelative, o as errorMessage, r as plural } from "./output-CF9uvk_-.js";
|
|
4
|
+
import { g as usageError, r as compareText, s as loadProject } from "./project-DSzWKU3C.js";
|
|
5
|
+
import "./types-Ds_vebja.js";
|
|
6
|
+
import { _ as toManualId, a as finishMigration, b as translationProblems, d as positionOf, i as encodeMarkup, j as jsString, t as MESSAGES_MODULE, y as rewriteSummary } from "./migrate-D7TNl6SD.js";
|
|
7
|
+
import { n as RUNTIME_MODULE, r as renderPlan } from "./rewrite-hAcRJwiZ.js";
|
|
8
|
+
import { basename, dirname, extname, join, relative, resolve } from "node:path";
|
|
9
|
+
import { readFile, readdir, stat } from "node:fs/promises";
|
|
10
|
+
// src/cli/commands/migrate/engine.ts
|
|
11
|
+
/**
|
|
12
|
+
* `vi18n migrate --from i18next | react-intl | lingui | vue-i18n` (INTERNALS
|
|
13
|
+
* §2.14, PLAN §5.19): the driver every library adapter shares.
|
|
14
|
+
*
|
|
15
|
+
* 1. `--catalog` files and directories become locale catalogs (the locale
|
|
16
|
+
* read from the path where no `<locale>=` names it); the adapter reads
|
|
17
|
+
* them and converts each text to ICU, keeping what it cannot convert;
|
|
18
|
+
* 2. the adapter plans each file under `project.include`: call sites become
|
|
19
|
+
* edits rendered later, and what it cannot rewrite is reported (keys a
|
|
20
|
+
* reported site names go to a `msg()` table, so no translation is lost);
|
|
21
|
+
* 3. every used message gets an id: a content id (the text is the message),
|
|
22
|
+
* unless two messages share a text but not their translations, or
|
|
23
|
+
* `--keep-ids` keeps a developer-chosen key: then a manual id;
|
|
24
|
+
* 4. edits are rendered, imports added, and the vi18n catalogs planned the
|
|
25
|
+
* way `--from saas-i18n` does (add-only; existing translations win).
|
|
26
|
+
*
|
|
27
|
+
* Without `--write` nothing is written; any error writes nothing.
|
|
28
|
+
*/
|
|
29
|
+
const LOCALE_LIKE = /^[a-z]{2,3}(?:[-_][A-Za-z0-9]{2,8})*$/i;
|
|
30
|
+
function normalizeLocale(name) {
|
|
31
|
+
return name.replace(/_/g, "-").toLowerCase();
|
|
32
|
+
}
|
|
33
|
+
/** The configured locale a path names (basename first, then directories, nearest first). */
|
|
34
|
+
function localeOfPath(path, locales) {
|
|
35
|
+
const byName = new Map(locales.map((l) => [normalizeLocale(l), l]));
|
|
36
|
+
const parts = path.split(/[\\/]/).filter((p) => p !== "");
|
|
37
|
+
const base = (parts.pop() ?? "").replace(/\.[^.]+$/, "");
|
|
38
|
+
const baseLocale = byName.get(normalizeLocale(base));
|
|
39
|
+
if (baseLocale !== void 0) return {
|
|
40
|
+
locale: baseLocale,
|
|
41
|
+
fromDirectory: false
|
|
42
|
+
};
|
|
43
|
+
let unknown = LOCALE_LIKE.test(base) && base.length <= 3 ? base : null;
|
|
44
|
+
for (let i = parts.length - 1; i >= 0; i--) {
|
|
45
|
+
const part = parts[i];
|
|
46
|
+
const locale = byName.get(normalizeLocale(part));
|
|
47
|
+
if (locale !== void 0) return {
|
|
48
|
+
locale,
|
|
49
|
+
fromDirectory: true
|
|
50
|
+
};
|
|
51
|
+
if (unknown === null && LOCALE_LIKE.test(part) && part.length <= 5 && /^[a-z]/.test(part)) unknown = part;
|
|
52
|
+
}
|
|
53
|
+
return unknown === null ? null : { unknown };
|
|
54
|
+
}
|
|
55
|
+
async function listFiles(dir, extensions) {
|
|
56
|
+
const out = [];
|
|
57
|
+
const walk = async (current, depth) => {
|
|
58
|
+
if (depth > 6) return;
|
|
59
|
+
const entries = await readdir(current, { withFileTypes: true });
|
|
60
|
+
for (const entry of entries.sort((a, b) => compareText(a.name, b.name))) {
|
|
61
|
+
if (entry.name.startsWith(".") || entry.name === "node_modules") continue;
|
|
62
|
+
const path = join(current, entry.name);
|
|
63
|
+
if (entry.isDirectory()) await walk(path, depth + 1);
|
|
64
|
+
else if (extensions.includes(extname(entry.name).toLowerCase())) out.push(path);
|
|
65
|
+
}
|
|
66
|
+
};
|
|
67
|
+
await walk(dir, 0);
|
|
68
|
+
return out;
|
|
69
|
+
}
|
|
70
|
+
/** `--catalog` entries as files with locales (and i18next namespaces). */
|
|
71
|
+
async function resolveCatalogFiles(cwd, config, entries, extensions, env) {
|
|
72
|
+
const out = /* @__PURE__ */ new Map();
|
|
73
|
+
const add = (file) => {
|
|
74
|
+
if (file !== null && !out.has(file.file)) out.set(file.file, file);
|
|
75
|
+
};
|
|
76
|
+
for (const [index, entry] of entries.entries()) {
|
|
77
|
+
const path = resolve(cwd, entry.file);
|
|
78
|
+
let info;
|
|
79
|
+
try {
|
|
80
|
+
info = await stat(path);
|
|
81
|
+
} catch (error) {
|
|
82
|
+
throw usageError(`--catalog ${entry.file}: ${errorMessage(error)}`);
|
|
83
|
+
}
|
|
84
|
+
if (!info.isDirectory()) {
|
|
85
|
+
add(catalogFile(cwd, config, entry, path, index === 0));
|
|
86
|
+
continue;
|
|
87
|
+
}
|
|
88
|
+
const files = await listFiles(path, extensions);
|
|
89
|
+
if (files.length === 0) throw usageError(`--catalog ${entry.file}: no ${extensions.join(", ")} files in it`);
|
|
90
|
+
for (const file of files) add(directoryFile(config, entry.locale, path, file, env));
|
|
91
|
+
}
|
|
92
|
+
return [...out.values()];
|
|
93
|
+
}
|
|
94
|
+
/** The configured spelling of a `--catalog <locale>=` locale. */
|
|
95
|
+
function configured(config, locale) {
|
|
96
|
+
const match = config.locales.find((l) => normalizeLocale(l) === normalizeLocale(locale));
|
|
97
|
+
if (match === void 0) throw usageError(`--catalog ${locale}=…: ${locale} is not a configured locale (${config.locales.join(", ")})`);
|
|
98
|
+
return match;
|
|
99
|
+
}
|
|
100
|
+
/** A `--catalog` file entry; the first one may name no locale, and is then the source locale's. */
|
|
101
|
+
function catalogFile(cwd, config, entry, path, first) {
|
|
102
|
+
if (entry.locale !== null) return {
|
|
103
|
+
locale: configured(config, entry.locale),
|
|
104
|
+
file: path,
|
|
105
|
+
namespace: null
|
|
106
|
+
};
|
|
107
|
+
const found = localeOfPath(relative(cwd, path), config.locales);
|
|
108
|
+
if (found !== null && "locale" in found) {
|
|
109
|
+
const namespace = found.fromDirectory ? basename(path, extname(path)) : null;
|
|
110
|
+
return {
|
|
111
|
+
locale: found.locale,
|
|
112
|
+
file: path,
|
|
113
|
+
namespace
|
|
114
|
+
};
|
|
115
|
+
}
|
|
116
|
+
if (first) return {
|
|
117
|
+
locale: config.sourceLocale,
|
|
118
|
+
file: path,
|
|
119
|
+
namespace: null
|
|
120
|
+
};
|
|
121
|
+
throw usageError(`--catalog ${entry.file}: the path names no configured locale; write <locale>=${entry.file}`);
|
|
122
|
+
}
|
|
123
|
+
/** A file of a `--catalog` directory; null (reported) when its path names no configured locale. */
|
|
124
|
+
function directoryFile(config, locale, dir, file, env) {
|
|
125
|
+
if (locale !== null) return {
|
|
126
|
+
locale: configured(config, locale),
|
|
127
|
+
file,
|
|
128
|
+
namespace: basename(file, extname(file))
|
|
129
|
+
};
|
|
130
|
+
const found = localeOfPath(relative(dir, file), config.locales);
|
|
131
|
+
if (found !== null && "locale" in found) {
|
|
132
|
+
const namespace = found.fromDirectory ? basename(file, extname(file)) : null;
|
|
133
|
+
return {
|
|
134
|
+
locale: found.locale,
|
|
135
|
+
file,
|
|
136
|
+
namespace
|
|
137
|
+
};
|
|
138
|
+
}
|
|
139
|
+
env.report("migrate-manual", found === null ? `${env.rel(file)} names no configured locale and was not read; pass it as --catalog <locale>=<file>` : `${env.rel(file)} is a ${found.unknown} catalog, which is not a configured locale: its translations were not migrated (add ${found.unknown} to locales and run again)`, { file });
|
|
140
|
+
return null;
|
|
141
|
+
}
|
|
142
|
+
/** Lowercase words of a text, for a readable manual id segment. */
|
|
143
|
+
function slug(text, max = 32) {
|
|
144
|
+
const words = text.normalize("NFKD").replace(/[̀-ͯ]/g, "").toLowerCase().match(/[a-z0-9]+/g);
|
|
145
|
+
if (words === null) return "";
|
|
146
|
+
let out = "";
|
|
147
|
+
for (const word of words) {
|
|
148
|
+
const next = out === "" ? word : `${out}_${word}`;
|
|
149
|
+
if (next.length > max) break;
|
|
150
|
+
out = next;
|
|
151
|
+
}
|
|
152
|
+
return out || words[0].slice(0, max);
|
|
153
|
+
}
|
|
154
|
+
/** Whether merging these messages into one content id would drop a translation. */
|
|
155
|
+
function translationsDiffer(group) {
|
|
156
|
+
const locales = new Set(group.flatMap((d) => [...d.translations.keys()]));
|
|
157
|
+
for (const locale of locales) {
|
|
158
|
+
const texts = /* @__PURE__ */ new Set();
|
|
159
|
+
for (const def of group) {
|
|
160
|
+
const translated = def.translations.get(locale);
|
|
161
|
+
if (translated === void 0) continue;
|
|
162
|
+
texts.add("icu" in translated ? translated.icu : `\0${translated.unconverted}`);
|
|
163
|
+
}
|
|
164
|
+
if (texts.size > 1) return true;
|
|
165
|
+
}
|
|
166
|
+
return false;
|
|
167
|
+
}
|
|
168
|
+
function assignIds(keys, defs, keepIds, codec) {
|
|
169
|
+
const groups = /* @__PURE__ */ new Map();
|
|
170
|
+
for (const key of keys) {
|
|
171
|
+
const def = defs.get(key);
|
|
172
|
+
if (def === void 0) continue;
|
|
173
|
+
let group = groups.get(def.canonical);
|
|
174
|
+
if (group === void 0) groups.set(def.canonical, group = []);
|
|
175
|
+
group.push(def);
|
|
176
|
+
}
|
|
177
|
+
const ids = /* @__PURE__ */ new Map();
|
|
178
|
+
const reasons = /* @__PURE__ */ new Map();
|
|
179
|
+
const taken = /* @__PURE__ */ new Set();
|
|
180
|
+
const pending = [];
|
|
181
|
+
for (const group of groups.values()) {
|
|
182
|
+
const conflict = group.length > 1 && translationsDiffer(group);
|
|
183
|
+
for (const def of group) if (conflict) {
|
|
184
|
+
reasons.set(def.key, "same text as another message, other translations");
|
|
185
|
+
pending.push(def);
|
|
186
|
+
} else if (keepIds && def.named) {
|
|
187
|
+
reasons.set(def.key, "--keep-ids");
|
|
188
|
+
pending.push(def);
|
|
189
|
+
} else ids.set(def.key, {
|
|
190
|
+
id: codec.label(def.canonical),
|
|
191
|
+
manual: false
|
|
192
|
+
});
|
|
193
|
+
}
|
|
194
|
+
const base = (def) => def.named ? toManualId(def.key.replace(/:/g, ".")) : toManualId(`${slug(def.hint ?? def.context ?? "") || "msg"}.${slug(def.source) || "message"}`);
|
|
195
|
+
for (const def of pending) {
|
|
196
|
+
const id = base(def);
|
|
197
|
+
if (def.named && id === def.key) taken.add(id);
|
|
198
|
+
}
|
|
199
|
+
for (const def of pending.sort((a, b) => compareText(a.key, b.key))) {
|
|
200
|
+
let id = base(def);
|
|
201
|
+
if (!(def.named && id === def.key)) {
|
|
202
|
+
if (taken.has(id)) {
|
|
203
|
+
let n = 2;
|
|
204
|
+
while (taken.has(`${id}_${n}`)) n++;
|
|
205
|
+
id = `${id}_${n}`;
|
|
206
|
+
}
|
|
207
|
+
taken.add(id);
|
|
208
|
+
}
|
|
209
|
+
ids.set(def.key, {
|
|
210
|
+
id,
|
|
211
|
+
manual: true
|
|
212
|
+
});
|
|
213
|
+
}
|
|
214
|
+
return {
|
|
215
|
+
ids,
|
|
216
|
+
reasons
|
|
217
|
+
};
|
|
218
|
+
}
|
|
219
|
+
async function createAdapter(library) {
|
|
220
|
+
switch (library) {
|
|
221
|
+
case "i18next": return (await import("./i18next-1waHziMy.js")).createI18nextAdapter();
|
|
222
|
+
case "react-intl": return (await import("./react-intl-tzGIk6G3.js")).createReactIntlAdapter();
|
|
223
|
+
case "lingui": return (await import("./lingui-Ee2farVo.js")).createLinguiAdapter();
|
|
224
|
+
case "vue-i18n": return (await import("./vue-i18n-CBmZqHbG.js")).createVueI18nAdapter();
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
/** Where the `msg()` table goes: beside the source catalog when that is extracted, else in the first include root. */
|
|
228
|
+
function messagesPath(config, files) {
|
|
229
|
+
const source = files.find((f) => f.locale === config.sourceLocale);
|
|
230
|
+
if (source !== void 0) {
|
|
231
|
+
const beside = join(dirname(source.file), MESSAGES_MODULE);
|
|
232
|
+
if (isIncluded(beside, config)) return beside;
|
|
233
|
+
}
|
|
234
|
+
return join(config.project.include[0] ?? config.root, MESSAGES_MODULE);
|
|
235
|
+
}
|
|
236
|
+
async function migrateLibrary(ctx, flags) {
|
|
237
|
+
return new LibraryRun(ctx, flags, await loadProject(ctx)).run(await createAdapter(flags.from));
|
|
238
|
+
}
|
|
239
|
+
/** One `vi18n migrate --from <library>` run: its messages, its diagnostics and what it reports. */
|
|
240
|
+
var LibraryRun = class {
|
|
241
|
+
config;
|
|
242
|
+
codec;
|
|
243
|
+
library;
|
|
244
|
+
diagnostics = [];
|
|
245
|
+
defs = /* @__PURE__ */ new Map();
|
|
246
|
+
failed = /* @__PURE__ */ new Set();
|
|
247
|
+
/** Each used message key, and the files that use it. */
|
|
248
|
+
used = /* @__PURE__ */ new Map();
|
|
249
|
+
/** Keys the code computes, which go to the `msg()` table. */
|
|
250
|
+
dynamicKeys = /* @__PURE__ */ new Set();
|
|
251
|
+
data;
|
|
252
|
+
ctx;
|
|
253
|
+
flags;
|
|
254
|
+
project;
|
|
255
|
+
constructor(ctx, flags, project) {
|
|
256
|
+
this.ctx = ctx;
|
|
257
|
+
this.flags = flags;
|
|
258
|
+
this.project = project;
|
|
259
|
+
this.config = project.config;
|
|
260
|
+
this.codec = project.codec;
|
|
261
|
+
this.library = flags.from;
|
|
262
|
+
this.data = {
|
|
263
|
+
from: flags.from,
|
|
264
|
+
write: flags.write,
|
|
265
|
+
files: [],
|
|
266
|
+
rewritten: 0,
|
|
267
|
+
renamed: [],
|
|
268
|
+
manualIds: [],
|
|
269
|
+
messagesModule: null,
|
|
270
|
+
entries: {},
|
|
271
|
+
edits: [],
|
|
272
|
+
unused: []
|
|
273
|
+
};
|
|
274
|
+
}
|
|
275
|
+
rel(file) {
|
|
276
|
+
return rootRelative(this.config.root, file);
|
|
277
|
+
}
|
|
278
|
+
define(def) {
|
|
279
|
+
if (this.defs.has(def.key)) return true;
|
|
280
|
+
if (this.failed.has(def.key)) return false;
|
|
281
|
+
try {
|
|
282
|
+
this.defs.set(def.key, {
|
|
283
|
+
...def,
|
|
284
|
+
canonical: this.codec.canonicalize(def.source)
|
|
285
|
+
});
|
|
286
|
+
return true;
|
|
287
|
+
} catch (error) {
|
|
288
|
+
if (!isDiagnosticError(error)) throw error;
|
|
289
|
+
this.failed.add(def.key);
|
|
290
|
+
this.diagnostics.push({
|
|
291
|
+
code: "migrate-unconverted",
|
|
292
|
+
severity: "warning",
|
|
293
|
+
message: `${def.key}: the source text is not valid ICU after conversion (${error.diagnostic.message}); its call sites were not rewritten: ${JSON.stringify(def.source)}`,
|
|
294
|
+
file: this.rel(def.file),
|
|
295
|
+
...def.line > 0 ? { line: def.line } : {},
|
|
296
|
+
id: def.key
|
|
297
|
+
});
|
|
298
|
+
return false;
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
message(key) {
|
|
302
|
+
return this.defs.get(key);
|
|
303
|
+
}
|
|
304
|
+
report(code, message, at, severity = "warning") {
|
|
305
|
+
const diagnostic = {
|
|
306
|
+
code,
|
|
307
|
+
severity,
|
|
308
|
+
message,
|
|
309
|
+
file: this.rel(at.file)
|
|
310
|
+
};
|
|
311
|
+
if (at.text !== void 0 && at.offset !== void 0) Object.assign(diagnostic, positionOf(at.text, at.offset));
|
|
312
|
+
else if (at.line !== void 0 && at.line > 0) diagnostic.line = at.line;
|
|
313
|
+
this.diagnostics.push(diagnostic);
|
|
314
|
+
}
|
|
315
|
+
async run(adapter) {
|
|
316
|
+
const { config, data } = this;
|
|
317
|
+
const catalogFiles = await this.catalogs(adapter);
|
|
318
|
+
const table = messagesPath(config, catalogFiles);
|
|
319
|
+
const plans = await this.planFiles(adapter, /* @__PURE__ */ new Set([table, ...catalogFiles.map((f) => f.file)]));
|
|
320
|
+
for (const plan of plans) this.collectUses(plan, table);
|
|
321
|
+
const usedKeys = [...this.used.keys()].sort(compareText);
|
|
322
|
+
const { ids, reasons } = assignIds(usedKeys, this.defs, this.flags.keepIds, this.codec);
|
|
323
|
+
data.unused.push(...adapter.unusedKeys());
|
|
324
|
+
data.unused.sort(compareText);
|
|
325
|
+
for (const key of usedKeys) {
|
|
326
|
+
const assigned = ids.get(key);
|
|
327
|
+
if (assigned?.manual === true) data.manualIds.push({
|
|
328
|
+
key,
|
|
329
|
+
id: assigned.id,
|
|
330
|
+
reason: reasons.get(key) ?? ""
|
|
331
|
+
});
|
|
332
|
+
}
|
|
333
|
+
const describe = describer(this.defs, ids);
|
|
334
|
+
const outputs = this.render(plans, describe, table);
|
|
335
|
+
const entries = this.entries(usedKeys, ids);
|
|
336
|
+
return this.finish(outputs, entries);
|
|
337
|
+
}
|
|
338
|
+
/** The `--catalog` files, loaded into the adapter. */
|
|
339
|
+
async catalogs(adapter) {
|
|
340
|
+
const { config } = this;
|
|
341
|
+
const files = await resolveCatalogFiles(this.ctx.global.cwd, config, this.flags.catalogs, adapter.catalogExtensions, this);
|
|
342
|
+
if (adapter.catalogsRequired && !files.some((f) => f.locale === config.sourceLocale)) throw usageError(`--catalog: no ${config.sourceLocale} catalog found; ${this.library} keeps the source text in the catalog of the source locale (${config.sourceLocale})`);
|
|
343
|
+
adapter.load(files, this);
|
|
344
|
+
return files;
|
|
345
|
+
}
|
|
346
|
+
/** Each project file using the library, planned by the adapter, but the `skip`ped ones. */
|
|
347
|
+
async planFiles(adapter, skip) {
|
|
348
|
+
const listed = await listProjectFiles(this.config);
|
|
349
|
+
const plans = [];
|
|
350
|
+
for (const file of listed.files.slice().sort(compareText)) {
|
|
351
|
+
if (skip.has(file)) continue;
|
|
352
|
+
const text = await readFile(file, "utf8");
|
|
353
|
+
if (!adapter.mentions(text)) continue;
|
|
354
|
+
if (!adapter.extensions.has(extname(file))) {
|
|
355
|
+
this.report("migrate-manual", `uses ${this.library}, but ${extname(file)} files are not rewritten by vi18n migrate --from ${this.library}: convert it by hand`, { file });
|
|
356
|
+
continue;
|
|
357
|
+
}
|
|
358
|
+
const plan = adapter.planFile(file, text, this);
|
|
359
|
+
if (plan !== null && (plan.edits.length > 0 || plan.manual.length > 0)) plans.push(plan);
|
|
360
|
+
}
|
|
361
|
+
return plans;
|
|
362
|
+
}
|
|
363
|
+
use(key, file) {
|
|
364
|
+
let files = this.used.get(key);
|
|
365
|
+
if (files === void 0) this.used.set(key, files = /* @__PURE__ */ new Set());
|
|
366
|
+
files.add(this.rel(file));
|
|
367
|
+
}
|
|
368
|
+
/** The messages `plan` uses, and its sites left for a person, reported. */
|
|
369
|
+
collectUses(plan, table) {
|
|
370
|
+
for (const key of renderPlan(plan, () => "").used) this.use(key, plan.file);
|
|
371
|
+
for (const site of plan.manual) {
|
|
372
|
+
const where = positionOf(plan.text, site.offset);
|
|
373
|
+
const keys = site.keys.filter((key) => this.defs.has(key));
|
|
374
|
+
for (const key of keys) {
|
|
375
|
+
this.dynamicKeys.add(key);
|
|
376
|
+
this.use(key, table);
|
|
377
|
+
}
|
|
378
|
+
this.diagnostics.push({
|
|
379
|
+
code: keys.length > 0 ? "migrate-dynamic-key" : "migrate-manual",
|
|
380
|
+
severity: "warning",
|
|
381
|
+
message: keys.length > 0 ? this.dynamicMessage(site.message, keys, table) : site.message,
|
|
382
|
+
file: this.rel(plan.file),
|
|
383
|
+
line: where.line,
|
|
384
|
+
column: where.column
|
|
385
|
+
});
|
|
386
|
+
}
|
|
387
|
+
}
|
|
388
|
+
dynamicMessage(message, keys, table) {
|
|
389
|
+
const sorted = [...new Set(keys)].sort(compareText);
|
|
390
|
+
const example = sorted.slice(0, 3).join(", ") + (sorted.length > 3 ? ", …" : "");
|
|
391
|
+
const are = sorted.length === 1 ? "is a msg() descriptor" : "are msg() descriptors";
|
|
392
|
+
return `${message}; ${plural(sorted.length, "message")} (${example}) ${are} in ${this.rel(table)}: import { messages } from it and call t(messages[key])`;
|
|
393
|
+
}
|
|
394
|
+
/** The rewritten files, and the `msg()` table when the code computes keys. */
|
|
395
|
+
render(plans, describe, table) {
|
|
396
|
+
const { data, config } = this;
|
|
397
|
+
const outputs = [];
|
|
398
|
+
for (const plan of plans) {
|
|
399
|
+
if (plan.edits.length === 0) continue;
|
|
400
|
+
const rendered = renderPlan(plan, describe);
|
|
401
|
+
for (const edit of rendered.listed) {
|
|
402
|
+
const { line, column } = positionOf(plan.text, edit.start);
|
|
403
|
+
data.edits.push({
|
|
404
|
+
file: this.rel(plan.file),
|
|
405
|
+
line,
|
|
406
|
+
column,
|
|
407
|
+
from: edit.from,
|
|
408
|
+
to: edit.to
|
|
409
|
+
});
|
|
410
|
+
}
|
|
411
|
+
data.rewritten += rendered.rewritten;
|
|
412
|
+
if (rendered.text !== plan.text) outputs.push({
|
|
413
|
+
file: plan.file,
|
|
414
|
+
text: rendered.text
|
|
415
|
+
});
|
|
416
|
+
}
|
|
417
|
+
if (this.dynamicKeys.size === 0) return outputs;
|
|
418
|
+
outputs.push({
|
|
419
|
+
file: table,
|
|
420
|
+
text: messagesModule(this.library, [...this.dynamicKeys], this.defs, describe)
|
|
421
|
+
});
|
|
422
|
+
data.messagesModule = this.rel(table);
|
|
423
|
+
if (!isIncluded(table, config)) this.diagnostics.push({
|
|
424
|
+
code: "extract-outside-include",
|
|
425
|
+
severity: "warning",
|
|
426
|
+
message: `${this.rel(table)} is outside project.include, so vi18n extract will not see its msg() descriptors; add its directory to project.include`,
|
|
427
|
+
file: this.rel(table)
|
|
428
|
+
});
|
|
429
|
+
return outputs;
|
|
430
|
+
}
|
|
431
|
+
/** The catalog entries of the used messages, under their assigned ids. */
|
|
432
|
+
entries(usedKeys, ids) {
|
|
433
|
+
const entries = {
|
|
434
|
+
messages: /* @__PURE__ */ new Map(),
|
|
435
|
+
translations: /* @__PURE__ */ new Map(),
|
|
436
|
+
notes: /* @__PURE__ */ new Map()
|
|
437
|
+
};
|
|
438
|
+
for (const key of usedKeys) {
|
|
439
|
+
const def = this.defs.get(key);
|
|
440
|
+
const assigned = ids.get(key);
|
|
441
|
+
this.addMessage(entries.messages, def, assigned, [...this.used.get(key) ?? []]);
|
|
442
|
+
for (const [locale, translated] of def.translations) {
|
|
443
|
+
if (locale === this.config.sourceLocale || !this.config.locales.includes(locale)) continue;
|
|
444
|
+
if ("icu" in translated) this.addTranslation(entries, def, assigned.id, locale, translated.icu);
|
|
445
|
+
else this.addNote(entries, def, assigned.id, locale, translated);
|
|
446
|
+
}
|
|
447
|
+
}
|
|
448
|
+
return entries;
|
|
449
|
+
}
|
|
450
|
+
addMessage(messages, def, assigned, refs) {
|
|
451
|
+
const existing = messages.get(assigned.id);
|
|
452
|
+
if (existing === void 0) {
|
|
453
|
+
messages.set(assigned.id, {
|
|
454
|
+
id: assigned.id,
|
|
455
|
+
manual: assigned.manual,
|
|
456
|
+
canonical: def.canonical,
|
|
457
|
+
sourceHash: this.codec.sourceHash(def.canonical),
|
|
458
|
+
label: this.codec.label(def.canonical),
|
|
459
|
+
contexts: def.context === null ? [] : [def.context],
|
|
460
|
+
refs: refs.sort(compareText),
|
|
461
|
+
sources: [def.canonical],
|
|
462
|
+
locations: []
|
|
463
|
+
});
|
|
464
|
+
return;
|
|
465
|
+
}
|
|
466
|
+
existing.refs = [.../* @__PURE__ */ new Set([...existing.refs, ...refs])].sort(compareText);
|
|
467
|
+
if (def.context !== null && !existing.contexts.includes(def.context)) existing.contexts = [...existing.contexts, def.context].sort(compareText);
|
|
468
|
+
}
|
|
469
|
+
addTranslation(entries, def, id, locale, icu) {
|
|
470
|
+
let map = entries.translations.get(locale);
|
|
471
|
+
if (map === void 0) entries.translations.set(locale, map = /* @__PURE__ */ new Map());
|
|
472
|
+
if (map.has(id)) return;
|
|
473
|
+
map.set(id, icu);
|
|
474
|
+
this.diagnostics.push(...translationProblems(this.codec, {
|
|
475
|
+
key: def.key,
|
|
476
|
+
canonical: def.canonical,
|
|
477
|
+
translation: icu
|
|
478
|
+
}, {
|
|
479
|
+
file: this.rel(def.file),
|
|
480
|
+
locale,
|
|
481
|
+
id
|
|
482
|
+
}));
|
|
483
|
+
}
|
|
484
|
+
addNote(entries, def, id, locale, translated) {
|
|
485
|
+
let map = entries.notes.get(locale);
|
|
486
|
+
if (map === void 0) entries.notes.set(locale, map = /* @__PURE__ */ new Map());
|
|
487
|
+
const note = `vi18n migrate: ${this.library} text not converted (${translated.reason}): ${translated.unconverted.replace(/\r?\n/g, "\\n")}`;
|
|
488
|
+
map.set(id, [...map.get(id) ?? [], note]);
|
|
489
|
+
const read = translated.file !== void 0 && (translated.line ?? 0) > 0;
|
|
490
|
+
this.diagnostics.push({
|
|
491
|
+
code: "migrate-unconverted",
|
|
492
|
+
severity: "warning",
|
|
493
|
+
message: `${def.key} (${locale}): ${translated.reason}; the entry stays untranslated with the original as a translator comment: ${JSON.stringify(translated.unconverted)}`,
|
|
494
|
+
file: this.rel(translated.file ?? def.file),
|
|
495
|
+
...read ? { line: translated.line } : {},
|
|
496
|
+
locale,
|
|
497
|
+
id
|
|
498
|
+
});
|
|
499
|
+
}
|
|
500
|
+
/** The shared last step: catalogs planned, reported, written when asked, the plan printed. */
|
|
501
|
+
finish(outputs, entries) {
|
|
502
|
+
const { diagnostics, data } = this;
|
|
503
|
+
return finishMigration(this.ctx, this.project, this.flags.write, {
|
|
504
|
+
diagnostics,
|
|
505
|
+
data,
|
|
506
|
+
outputs,
|
|
507
|
+
...entries
|
|
508
|
+
}, printPlan);
|
|
509
|
+
}
|
|
510
|
+
};
|
|
511
|
+
/** A message as code: a `msg()`-style descriptor, or the bare text when it has neither id nor context. */
|
|
512
|
+
function describer(defs, ids) {
|
|
513
|
+
return (key, quote, markup) => {
|
|
514
|
+
const def = defs.get(key);
|
|
515
|
+
const assigned = ids.get(key);
|
|
516
|
+
const parts = [];
|
|
517
|
+
if (assigned.manual) parts.push(`id: ${jsString(assigned.id, quote)}`);
|
|
518
|
+
parts.push(`message: ${jsString(def.canonical, quote)}`);
|
|
519
|
+
if (def.context !== null) parts.push(`context: ${jsString(def.context, quote)}`);
|
|
520
|
+
const code = parts.length === 1 ? jsString(def.canonical, quote) : `{ ${parts.join(", ")} }`;
|
|
521
|
+
return encodeMarkup(code, markup);
|
|
522
|
+
};
|
|
523
|
+
}
|
|
524
|
+
/** `export const messages = { "<key>": msg(…) }`, keyed by the old library's key the code computes. */
|
|
525
|
+
function messagesModule(library, keys, defs, describe) {
|
|
526
|
+
const lines = [
|
|
527
|
+
`// Generated by vi18n migrate --from ${library}: messages the code looks up by a`,
|
|
528
|
+
"// computed key, or at call sites vi18n migrate left for a person, as msg()",
|
|
529
|
+
"// descriptors so vi18n extract keeps them. Look one up with t(messages[key]).",
|
|
530
|
+
`import { msg } from "${RUNTIME_MODULE}";`,
|
|
531
|
+
"",
|
|
532
|
+
"export const messages = {"
|
|
533
|
+
];
|
|
534
|
+
for (const key of [...new Set(keys)].sort(compareText)) {
|
|
535
|
+
if (!defs.has(key)) continue;
|
|
536
|
+
lines.push(` ${JSON.stringify(key)}: msg(${describe(key, "\"", null)}),`);
|
|
537
|
+
}
|
|
538
|
+
lines.push("} as const;", "");
|
|
539
|
+
return lines.join("\n");
|
|
540
|
+
}
|
|
541
|
+
function printPlan(ctx, data, failed) {
|
|
542
|
+
const indent = (text) => text.replace(/\n/g, "\n ");
|
|
543
|
+
for (const edit of data.edits) {
|
|
544
|
+
const lines = [`${edit.file}:${edit.line}:${edit.column}`];
|
|
545
|
+
const show = (text) => indent(text.replace(/^[ \t]+/, "").replace(/\n$/, ""));
|
|
546
|
+
if (edit.from !== "") lines.push(` - ${show(edit.from)}`);
|
|
547
|
+
if (edit.to !== "") lines.push(` + ${show(edit.to)}`);
|
|
548
|
+
ctx.out.result(lines.join("\n"));
|
|
549
|
+
}
|
|
550
|
+
if (data.manualIds.length > 0) ctx.out.result(`manual ids: ${data.manualIds.map((m) => `${m.key} → ${m.id} (${m.reason})`).join(", ")}`);
|
|
551
|
+
if (data.messagesModule !== null) ctx.out.result(`msg() table: ${data.messagesModule}`);
|
|
552
|
+
for (const [locale, count] of Object.entries(data.entries)) ctx.out.result(`${locale}: ${plural(count, "translation")} for the vi18n catalog`);
|
|
553
|
+
if (data.unused.length > 0) ctx.out.result(`${plural(data.unused.length, "catalog key")} no code uses: ${data.unused.join(", ")}`);
|
|
554
|
+
ctx.out.result(rewriteSummary(data, failed));
|
|
555
|
+
}
|
|
556
|
+
export { migrateLibrary };
|
package/dist/engine.d.ts
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
|
-
import "./types-
|
|
2
|
-
//#region src/runtime/devtools.d.ts
|
|
1
|
+
import "./types-BaG1LboH.js";
|
|
3
2
|
/** The part of `import.meta.hot` the editor uses. */
|
|
4
3
|
interface DevHot {
|
|
5
4
|
on(event: string, listener: (data: never) => void): void;
|
|
@@ -12,8 +11,6 @@ interface DevHot {
|
|
|
12
11
|
* `import.meta.hot`; a no-op outside a document or without HMR.
|
|
13
12
|
*/
|
|
14
13
|
export declare function inContext(hot: DevHot | undefined, endpoint: string): void;
|
|
15
|
-
//#endregion
|
|
16
|
-
//#region src/runtime/loader.d.ts
|
|
17
14
|
/**
|
|
18
15
|
* The client engine behind the global state (INTERNALS §2.11): registration,
|
|
19
16
|
* bundle loading, skew handling, locale switching, the navigation watcher,
|
|
@@ -39,5 +36,4 @@ export declare function inContext(hot: DevHot | undefined, endpoint: string): vo
|
|
|
39
36
|
* fragment does not reach) never hear from it.
|
|
40
37
|
*/
|
|
41
38
|
export declare function bridge(): void;
|
|
42
|
-
//#endregion
|
|
43
39
|
export {};
|