@menapo/vi18n 0.1.0 → 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/README.md +14 -14
- package/dist/{apply-Bre-lY9J.js → apply-FAHl1R6T.js} +69 -89
- package/dist/{captured-Bj8bO6vd.js → captured-CTjjDF5o.js} +196 -217
- package/dist/{check-DyrAbv73.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-DGsMbcTZ.js → export-BYq0gfLd.js} +17 -38
- package/dist/{extract-Dujlsgnk.js → extract-CYtuEo8u.js} +114 -82
- package/dist/{formats-KPIg-08V.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-Cwdhcywt.js → import-BApbQlmm.js} +81 -75
- package/dist/index.d.ts +1 -13
- package/dist/index.js +14 -16
- package/dist/{init-BecCTvKi.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 +69 -82
- 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-Cq1Nuu-Q.js → lsp-BC7xDGON.js} +4 -5
- package/dist/{match-BGr8EXB3.js → match-BLDcj610.js} +11 -4
- package/dist/{mcp-Cf0T7qPW.js → mcp-Cka8ywDF.js} +4 -5
- package/dist/{merge-driver-CpiiSVIc.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-DsaHKHEt.js → output-CF9uvk_-.js} +15 -9
- package/dist/parse.d.ts +1 -3
- package/dist/parse.js +7 -8
- package/dist/{pipeline-wxS0SApG.js → pipeline-B30irGMD.js} +1 -1
- package/dist/{pipeline-C7g11THL.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-BDkykMjb.js → server-WlxZH0oX.js} +275 -330
- package/dist/{server-qv3mz70d.js → server-ceuzzkal.js} +292 -386
- package/dist/server.d.ts +1 -17
- package/dist/server.js +415 -381
- package/dist/{sitemap-DV3o8QD7.js → sitemap-796jJKUT.js} +21 -33
- package/dist/{sources-D19czDSA.js → sources-DuOdM02f.js} +712 -373
- package/dist/{status-DZArW5aE.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-BlyLGlOa.js → version-BFs370Ce.js} +3 -5
- package/dist/{vite-scan-B_YW4X_a.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-C6PFI1Dm.js → which-BpyJyBkX.js} +44 -37
- package/dist/{worker-84U6uYB3.js → worker-BjzVqJVO.js} +454 -608
- package/package.json +8 -8
- package/dist/check-D34P-S--.js +0 -1
- package/dist/codec-DuvU2VQy.js +0 -207
- package/dist/collisions-CfpZU8e4.js +0 -217
- package/dist/compile-B5Mz6WJm.js +0 -245
- package/dist/concurrency-C9UUWuDC.js +0 -96
- package/dist/concurrency-DONL7t4V.js +0 -1
- package/dist/engine-B7p40M__.js +0 -531
- package/dist/i18next-CrUnKFfh.js +0 -1596
- package/dist/i18next-Rgo-v9l0.js +0 -1
- package/dist/lingui--PcOOYry.js +0 -833
- package/dist/messages-Cxa-5tR0.js +0 -166
- package/dist/migrate-CntqvrW4.js +0 -1
- package/dist/migrate-dPgq9uzy.js +0 -1484
- package/dist/project-Dwx7iPm6.js +0 -381
- package/dist/project-pXipT8iG.js +0 -1046
- package/dist/react-intl-CaBVwJ9u.js +0 -519
- package/dist/restore-Btq66Tid.js +0 -175
- package/dist/rewrite-CiTRAICl.js +0 -803
- package/dist/run-DYD_VXKy.js +0 -694
- package/dist/runtime-DTMmliT1.js +0 -64
- package/dist/sync-CcVzger_.js +0 -658
- package/dist/translate-DZR3MkSA.js +0 -299
- package/dist/types-wSh9fCBY.js +0 -11
- package/dist/vue-i18n-CEMnpv7k.js +0 -651
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { r as validateTranslation } from "./validate-
|
|
1
|
+
import { n as isDiagnosticError, t as diagnosticError } from "./diagnostics-1onDF6Ua.js";
|
|
2
|
+
import { c as mapLimit, h as isManualId, i as MISSING, l as modulePath, o as hasExtension, p as tryCanonicalize, r as validateTranslation, s as isMissing$1, u as toPosix } from "./validate-zv4-IZyZ.js";
|
|
3
3
|
import { basename, dirname, isAbsolute, join, relative, resolve, sep } from "node:path";
|
|
4
4
|
import { hash, randomBytes } from "node:crypto";
|
|
5
|
-
import { existsSync, readFileSync, realpathSync } from "node:fs";
|
|
6
|
-
import { mkdir, readFile, readdir, rename, rm, writeFile } from "node:fs/promises";
|
|
7
|
-
|
|
5
|
+
import { existsSync, readFileSync, readdirSync, realpathSync, statSync } from "node:fs";
|
|
6
|
+
import { mkdir, readFile, readdir, realpath, rename, rm, stat, writeFile } from "node:fs/promises";
|
|
7
|
+
// src/core/extract/dependencies.ts
|
|
8
8
|
/**
|
|
9
9
|
* `extraction.dependencies` (PLAN §4.5, §5.24; INTERNALS §2.5.7): published
|
|
10
10
|
* packages the app opts in. Each name resolves from the config root the way
|
|
@@ -15,8 +15,6 @@ import { mkdir, readFile, readdir, rename, rm, writeFile } from "node:fs/promise
|
|
|
15
15
|
* catalog references read `<name>/<path in the package>`, the same on every
|
|
16
16
|
* machine and package manager and across the package's versions.
|
|
17
17
|
*/
|
|
18
|
-
/** As `files.ts` has it (which imports this module). */
|
|
19
|
-
const toPosix = (path) => sep === "/" ? path : path.replaceAll("\\", "/");
|
|
20
18
|
const resolved = /* @__PURE__ */ new WeakMap();
|
|
21
19
|
/** Directories and names never extracted inside a package. */
|
|
22
20
|
const SKIPPED_DIRS = /* @__PURE__ */ new Set([
|
|
@@ -25,7 +23,6 @@ const SKIPPED_DIRS = /* @__PURE__ */ new Set([
|
|
|
25
23
|
".git"
|
|
26
24
|
]);
|
|
27
25
|
const TEST_FILE = /\.(?:test|spec)\.[^/]*$/;
|
|
28
|
-
const DECLARATION = /\.d\.[cm]?ts$/;
|
|
29
26
|
/**
|
|
30
27
|
* Every `extraction.dependencies` package, resolved once per config. A
|
|
31
28
|
* name that does not resolve is a `config-invalid` error: its strings would
|
|
@@ -79,10 +76,7 @@ function messagesFile(dir) {
|
|
|
79
76
|
*/
|
|
80
77
|
function dependencyOf(file, config) {
|
|
81
78
|
if (config.extraction.dependencies.length === 0) return null;
|
|
82
|
-
|
|
83
|
-
const query = path.indexOf("?");
|
|
84
|
-
if (query !== -1) path = path.slice(0, query);
|
|
85
|
-
path = toPosix(path);
|
|
79
|
+
const path = modulePath(file);
|
|
86
80
|
for (const dependency of dependenciesOf(config)) {
|
|
87
81
|
if (!path.startsWith(dependency.dir + "/")) continue;
|
|
88
82
|
return extracted(path.slice(dependency.dir.length + 1), config) ? dependency : null;
|
|
@@ -92,8 +86,7 @@ function dependencyOf(file, config) {
|
|
|
92
86
|
function extracted(rel, config) {
|
|
93
87
|
const segments = rel.split("/");
|
|
94
88
|
const name = segments[segments.length - 1];
|
|
95
|
-
if (
|
|
96
|
-
if (!config.project.extensions.some((ext) => name.endsWith(ext) && name.length > ext.length)) return false;
|
|
89
|
+
if (TEST_FILE.test(name) || !hasExtension(name, config.project.extensions)) return false;
|
|
97
90
|
for (let i = 0; i < segments.length - 1; i++) if (SKIPPED_DIRS.has(segments[i])) return false;
|
|
98
91
|
return true;
|
|
99
92
|
}
|
|
@@ -120,8 +113,397 @@ async function listDependencyFiles(config) {
|
|
|
120
113
|
}
|
|
121
114
|
return [...new Set(out)].sort();
|
|
122
115
|
}
|
|
123
|
-
|
|
124
|
-
|
|
116
|
+
// src/core/text.ts
|
|
117
|
+
/** Text helpers shared across core. */
|
|
118
|
+
/**
|
|
119
|
+
* Orders strings by UTF-16 code unit, as `<` does: the same order on every
|
|
120
|
+
* machine, unlike `localeCompare`, so generated files and reports are stable.
|
|
121
|
+
*/
|
|
122
|
+
function compareText$1(a, b) {
|
|
123
|
+
if (a < b) return -1;
|
|
124
|
+
return a > b ? 1 : 0;
|
|
125
|
+
}
|
|
126
|
+
// src/core/extract/files.ts
|
|
127
|
+
/** Directories read at once while walking: enough to keep the disk busy, few enough for any fd limit. */
|
|
128
|
+
const READDIR_CONCURRENCY = 32;
|
|
129
|
+
/** `file` relative to `root` in posix form for messages, or the whole path when it lies outside `root`. */
|
|
130
|
+
function displayPath(root, file) {
|
|
131
|
+
const rel = relative(root, file);
|
|
132
|
+
return toPosix(rel === "" || rel.startsWith("..") || isAbsolute(rel) ? file : rel);
|
|
133
|
+
}
|
|
134
|
+
function escapeRegExp(ch) {
|
|
135
|
+
return /[\\^$.*+?()[\]{}|]/.test(ch) ? "\\" + ch : ch;
|
|
136
|
+
}
|
|
137
|
+
/**
|
|
138
|
+
* A gitignore glob (`*` one segment, `**` any depth, `?` one character) as a
|
|
139
|
+
* regular expression body over a posix path.
|
|
140
|
+
*/
|
|
141
|
+
function globBody(pattern) {
|
|
142
|
+
let out = "";
|
|
143
|
+
for (let i = 0; i < pattern.length; i++) {
|
|
144
|
+
const ch = pattern[i];
|
|
145
|
+
if (ch === "*") {
|
|
146
|
+
const atSegmentStart = i === 0 || pattern[i - 1] === "/";
|
|
147
|
+
if (pattern[i + 1] === "*" && atSegmentStart) {
|
|
148
|
+
if (pattern[i + 2] === "/") {
|
|
149
|
+
out += "(?:.*/)?";
|
|
150
|
+
i += 2;
|
|
151
|
+
continue;
|
|
152
|
+
}
|
|
153
|
+
if (i + 2 === pattern.length) {
|
|
154
|
+
out += ".*";
|
|
155
|
+
i += 1;
|
|
156
|
+
continue;
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
while (pattern[i + 1] === "*") i++;
|
|
160
|
+
out += "[^/]*";
|
|
161
|
+
} else if (ch === "?") out += "[^/]";
|
|
162
|
+
else out += escapeRegExp(ch);
|
|
163
|
+
}
|
|
164
|
+
return out;
|
|
165
|
+
}
|
|
166
|
+
const matchers = /* @__PURE__ */ new WeakMap();
|
|
167
|
+
/**
|
|
168
|
+
* One alternation per kind of path, so deciding a path is one regex test
|
|
169
|
+
* whatever the number of patterns. A pattern also matches everything below
|
|
170
|
+
* what it matches (gitignore excludes a directory's contents with it).
|
|
171
|
+
*/
|
|
172
|
+
function compileExcludes(patterns) {
|
|
173
|
+
const file = [];
|
|
174
|
+
const dir = [];
|
|
175
|
+
for (const raw of patterns) {
|
|
176
|
+
let pattern = raw.trim();
|
|
177
|
+
if (pattern === "" || pattern.startsWith("#")) continue;
|
|
178
|
+
const dirOnly = pattern.endsWith("/");
|
|
179
|
+
if (dirOnly) pattern = pattern.replace(/\/+$/, "");
|
|
180
|
+
if (pattern === "") continue;
|
|
181
|
+
if (!pattern.includes("/")) {
|
|
182
|
+
const segment = globBody(pattern);
|
|
183
|
+
file.push(`(?:^|/)${segment}${dirOnly ? "/" : "(?:/|$)"}`);
|
|
184
|
+
dir.push(`(?:^|/)${segment}(?:/|$)`);
|
|
185
|
+
continue;
|
|
186
|
+
}
|
|
187
|
+
const anchored = globBody(pattern.replace(/^\/+/, ""));
|
|
188
|
+
file.push(`^${anchored}${dirOnly ? "/" : "(?:/|$)"}`);
|
|
189
|
+
const pruned = !dirOnly && anchored.endsWith("/.*") ? anchored.slice(0, -3) : anchored;
|
|
190
|
+
dir.push(`^${pruned}(?:/|$)`);
|
|
191
|
+
}
|
|
192
|
+
return {
|
|
193
|
+
file: file.length === 0 ? null : new RegExp(file.join("|")),
|
|
194
|
+
dir: dir.length === 0 ? null : new RegExp(dir.join("|"))
|
|
195
|
+
};
|
|
196
|
+
}
|
|
197
|
+
function matcherFor(config) {
|
|
198
|
+
const project = config.project;
|
|
199
|
+
const known = matchers.get(project);
|
|
200
|
+
if (known !== void 0 && known.include === project.include && known.exclude === project.exclude && known.extensions === project.extensions) return known;
|
|
201
|
+
const roots = project.include.map((entry) => toPosix(entry).replace(/\/+$/, ""));
|
|
202
|
+
const realRoots = project.include.map((entry, i) => {
|
|
203
|
+
try {
|
|
204
|
+
const real = toPosix(realpathSync(entry));
|
|
205
|
+
return real === roots[i] ? null : real;
|
|
206
|
+
} catch {
|
|
207
|
+
return null;
|
|
208
|
+
}
|
|
209
|
+
});
|
|
210
|
+
const matcher = {
|
|
211
|
+
include: project.include,
|
|
212
|
+
exclude: project.exclude,
|
|
213
|
+
extensions: project.extensions,
|
|
214
|
+
roots,
|
|
215
|
+
realRoots,
|
|
216
|
+
links: project.include.map(() => null),
|
|
217
|
+
...compileExcludes(project.exclude)
|
|
218
|
+
};
|
|
219
|
+
matchers.set(project, matcher);
|
|
220
|
+
return matcher;
|
|
221
|
+
}
|
|
222
|
+
/** Config-relative posix path, `../` allowed. */
|
|
223
|
+
function relativeTo(root, path) {
|
|
224
|
+
const rootPosix = toPosix(root);
|
|
225
|
+
if (path.startsWith(rootPosix) && path[rootPosix.length] === "/") return path.slice(rootPosix.length + 1);
|
|
226
|
+
return toPosix(relative(root, path));
|
|
227
|
+
}
|
|
228
|
+
function isExcluded(m, rel, directory) {
|
|
229
|
+
const re = directory ? m.dir : m.file;
|
|
230
|
+
return re !== null && re.test(rel);
|
|
231
|
+
}
|
|
232
|
+
function under(path, dir) {
|
|
233
|
+
return path === dir || path.startsWith(dir) && path[dir.length] === "/";
|
|
234
|
+
}
|
|
235
|
+
/**
|
|
236
|
+
* Whether the plugin and the CLI extract `file` (absolute; a `?query` or `\0`
|
|
237
|
+
* prefix is ignored): under an include entry, by its path, by the entry's
|
|
238
|
+
* realpath, or by a realpath the walk reached through a link inside the
|
|
239
|
+
* entry, with a scanned extension and no exclude match. Excludes see the
|
|
240
|
+
* path as the walk reached it, so a realpath module id (what Vite gives the
|
|
241
|
+
* plugin) gets the walk's answer.
|
|
242
|
+
*/
|
|
243
|
+
function isIncluded(file, config) {
|
|
244
|
+
if (dependencyOf(file, config) !== null) return true;
|
|
245
|
+
const m = matcherFor(config);
|
|
246
|
+
const path = modulePath(file);
|
|
247
|
+
if (!hasExtension(path.slice(path.lastIndexOf("/") + 1), m.extensions)) return false;
|
|
248
|
+
for (let i = 0; i < m.roots.length; i++) {
|
|
249
|
+
const root = m.roots[i];
|
|
250
|
+
const real = m.realRoots[i];
|
|
251
|
+
let logical = null;
|
|
252
|
+
if (under(path, root)) logical = path;
|
|
253
|
+
else if (real != null && under(path, real)) logical = root + path.slice(real.length);
|
|
254
|
+
else logical = throughLink(path, linksOf(m, config, i));
|
|
255
|
+
if (logical !== null && !isExcluded(m, relativeTo(config.root, logical), false)) return true;
|
|
256
|
+
}
|
|
257
|
+
return false;
|
|
258
|
+
}
|
|
259
|
+
/** The path the walk reached a realpath by through a link: the file itself if linked, else its directory. */
|
|
260
|
+
function throughLink(path, links) {
|
|
261
|
+
const linked = links.get(path);
|
|
262
|
+
if (linked !== void 0) return linked;
|
|
263
|
+
const slash = path.lastIndexOf("/");
|
|
264
|
+
const dir = links.get(path.slice(0, slash));
|
|
265
|
+
return dir === void 0 ? null : dir + path.slice(slash);
|
|
266
|
+
}
|
|
267
|
+
/**
|
|
268
|
+
* The links of include entry `i`. A module outside every entry's tree may
|
|
269
|
+
* still be reached through a link, which only a walk can tell; the first
|
|
270
|
+
* such question walks the entry's directories once (the plugin asks only for
|
|
271
|
+
* modules with messages), and `listProjectFiles` refreshes the answer.
|
|
272
|
+
*/
|
|
273
|
+
function linksOf(m, config, i) {
|
|
274
|
+
let links = m.links[i];
|
|
275
|
+
if (links == null) {
|
|
276
|
+
links = /* @__PURE__ */ new Map();
|
|
277
|
+
const entry = config.project.include[i];
|
|
278
|
+
try {
|
|
279
|
+
if (statSync(entry).isDirectory() && enterable(m, config, entry)) {
|
|
280
|
+
const walk = new Walk(m, config, {
|
|
281
|
+
logical: entry,
|
|
282
|
+
real: realpathSync(entry)
|
|
283
|
+
}, null);
|
|
284
|
+
while (walk.level.length > 0) walk.step(walk.level.map(listSync));
|
|
285
|
+
links = walk.links;
|
|
286
|
+
}
|
|
287
|
+
} catch (error) {
|
|
288
|
+
if (!isMissing$1(error, UNREACHABLE)) throw error;
|
|
289
|
+
}
|
|
290
|
+
m.links[i] = links;
|
|
291
|
+
}
|
|
292
|
+
return links;
|
|
293
|
+
}
|
|
294
|
+
/** Missing, or a link cycle: either way there is nothing to walk. */
|
|
295
|
+
const UNREACHABLE = /* @__PURE__ */ new Set([...MISSING, "ELOOP"]);
|
|
296
|
+
function listed(entry) {
|
|
297
|
+
return {
|
|
298
|
+
name: entry.name,
|
|
299
|
+
isDir: entry.isDirectory(),
|
|
300
|
+
isFile: entry.isFile(),
|
|
301
|
+
real: null
|
|
302
|
+
};
|
|
303
|
+
}
|
|
304
|
+
async function listAsync(dir) {
|
|
305
|
+
let entries;
|
|
306
|
+
try {
|
|
307
|
+
entries = await readdir(dir.logical, { withFileTypes: true });
|
|
308
|
+
} catch (error) {
|
|
309
|
+
if (isMissing$1(error, UNREACHABLE)) return null;
|
|
310
|
+
throw error;
|
|
311
|
+
}
|
|
312
|
+
const out = [];
|
|
313
|
+
for (const entry of entries) {
|
|
314
|
+
if (!entry.isSymbolicLink()) {
|
|
315
|
+
out.push(listed(entry));
|
|
316
|
+
continue;
|
|
317
|
+
}
|
|
318
|
+
const logical = dir.logical + sep + entry.name;
|
|
319
|
+
try {
|
|
320
|
+
const target = await stat(logical);
|
|
321
|
+
out.push({
|
|
322
|
+
name: entry.name,
|
|
323
|
+
isDir: target.isDirectory(),
|
|
324
|
+
isFile: target.isFile(),
|
|
325
|
+
real: await realpath(logical)
|
|
326
|
+
});
|
|
327
|
+
} catch (error) {
|
|
328
|
+
if (!isMissing$1(error, UNREACHABLE)) throw error;
|
|
329
|
+
}
|
|
330
|
+
}
|
|
331
|
+
return out;
|
|
332
|
+
}
|
|
333
|
+
function listSync(dir) {
|
|
334
|
+
let entries;
|
|
335
|
+
try {
|
|
336
|
+
entries = readdirSync(dir.logical, { withFileTypes: true });
|
|
337
|
+
} catch (error) {
|
|
338
|
+
if (isMissing$1(error, UNREACHABLE)) return null;
|
|
339
|
+
throw error;
|
|
340
|
+
}
|
|
341
|
+
const out = [];
|
|
342
|
+
for (const entry of entries) {
|
|
343
|
+
if (!entry.isSymbolicLink()) {
|
|
344
|
+
out.push(listed(entry));
|
|
345
|
+
continue;
|
|
346
|
+
}
|
|
347
|
+
const logical = dir.logical + sep + entry.name;
|
|
348
|
+
try {
|
|
349
|
+
const target = statSync(logical);
|
|
350
|
+
out.push({
|
|
351
|
+
name: entry.name,
|
|
352
|
+
isDir: target.isDirectory(),
|
|
353
|
+
isFile: target.isFile(),
|
|
354
|
+
real: realpathSync(logical)
|
|
355
|
+
});
|
|
356
|
+
} catch (error) {
|
|
357
|
+
if (!isMissing$1(error, UNREACHABLE)) throw error;
|
|
358
|
+
}
|
|
359
|
+
}
|
|
360
|
+
return out;
|
|
361
|
+
}
|
|
362
|
+
/** Whether a native path is `dir` or below it. */
|
|
363
|
+
function within(path, dir) {
|
|
364
|
+
return path === dir || path.startsWith(dir) && path[dir.length] === sep;
|
|
365
|
+
}
|
|
366
|
+
/**
|
|
367
|
+
* One include directory, walked breadth-first by `step` (the listing is the
|
|
368
|
+
* caller's, async for the walk and sync for `isIncluded`, so both decide
|
|
369
|
+
* alike). Symlinks are followed by their realpath once, so a link cycle ends
|
|
370
|
+
* and a directory linked twice is read once. A link into the entry's own
|
|
371
|
+
* tree is not followed: its target is walked where it is, under its own
|
|
372
|
+
* path, which is the path a realpath module id names.
|
|
373
|
+
*/
|
|
374
|
+
var Walk = class {
|
|
375
|
+
links = /* @__PURE__ */ new Map();
|
|
376
|
+
/** Whether any file matched (also when an earlier entry already listed it: such an entry is not empty). */
|
|
377
|
+
found = false;
|
|
378
|
+
level;
|
|
379
|
+
#m;
|
|
380
|
+
#config;
|
|
381
|
+
#start;
|
|
382
|
+
#files;
|
|
383
|
+
#visited;
|
|
384
|
+
constructor(m, config, start, files) {
|
|
385
|
+
this.#m = m;
|
|
386
|
+
this.#config = config;
|
|
387
|
+
this.#start = start;
|
|
388
|
+
this.#files = files;
|
|
389
|
+
this.#visited = /* @__PURE__ */ new Set([start.real]);
|
|
390
|
+
this.level = [start];
|
|
391
|
+
}
|
|
392
|
+
/** Applies the listings of the current level (index-aligned with `level`). */
|
|
393
|
+
step(listings) {
|
|
394
|
+
const m = this.#m;
|
|
395
|
+
const candidates = [];
|
|
396
|
+
for (let i = 0; i < this.level.length; i++) {
|
|
397
|
+
const dir = this.level[i];
|
|
398
|
+
for (const entry of listings[i] ?? []) {
|
|
399
|
+
if (entry.real !== null && within(entry.real, this.#start.real)) continue;
|
|
400
|
+
const logical = dir.logical + sep + entry.name;
|
|
401
|
+
const real = entry.real ?? dir.real + sep + entry.name;
|
|
402
|
+
const rel = relativeTo(this.#config.root, toPosix(logical));
|
|
403
|
+
if (entry.isDir) {
|
|
404
|
+
if (!isExcluded(m, rel, true)) candidates.push({
|
|
405
|
+
logical,
|
|
406
|
+
real
|
|
407
|
+
});
|
|
408
|
+
} else if (entry.isFile && hasExtension(entry.name, m.extensions)) {
|
|
409
|
+
if (isExcluded(m, rel, false)) continue;
|
|
410
|
+
this.found = true;
|
|
411
|
+
this.#files?.add(real);
|
|
412
|
+
if (entry.real !== null) this.links.set(toPosix(real), toPosix(logical));
|
|
413
|
+
}
|
|
414
|
+
}
|
|
415
|
+
}
|
|
416
|
+
candidates.sort((a, b) => compareText$1(a.logical, b.logical));
|
|
417
|
+
const next = [];
|
|
418
|
+
for (const dir of candidates) {
|
|
419
|
+
if (this.#visited.has(dir.real)) continue;
|
|
420
|
+
this.#visited.add(dir.real);
|
|
421
|
+
next.push(dir);
|
|
422
|
+
if (!within(dir.real, this.#start.real)) this.links.set(toPosix(dir.real), toPosix(dir.logical));
|
|
423
|
+
}
|
|
424
|
+
this.level = next;
|
|
425
|
+
}
|
|
426
|
+
};
|
|
427
|
+
async function walk(walker) {
|
|
428
|
+
while (walker.level.length > 0) {
|
|
429
|
+
const level = walker.level;
|
|
430
|
+
const listings = Array.from({ length: level.length }, () => null);
|
|
431
|
+
await mapLimit(level.map((_, i) => i), READDIR_CONCURRENCY, async (i) => {
|
|
432
|
+
listings[i] = await listAsync(level[i]);
|
|
433
|
+
});
|
|
434
|
+
walker.step(listings);
|
|
435
|
+
}
|
|
436
|
+
}
|
|
437
|
+
/**
|
|
438
|
+
* Whether an include directory is walked at all: its own path must pass the
|
|
439
|
+
* directory excludes too (excluding `legacy` excludes an entry `src/legacy`).
|
|
440
|
+
*/
|
|
441
|
+
function enterable(m, config, entry) {
|
|
442
|
+
const rel = relativeTo(config.root, toPosix(entry));
|
|
443
|
+
return rel === "" || !isExcluded(m, rel, true);
|
|
444
|
+
}
|
|
445
|
+
/**
|
|
446
|
+
* Lists the project's files by realpath, sorted by code unit so scans are
|
|
447
|
+
* deterministic, and the include entries that matched no file (missing,
|
|
448
|
+
* empty, or wholly excluded), which `vi18n extract` reports as
|
|
449
|
+
* `extract-empty-root`. Overlapping entries list a file once.
|
|
450
|
+
*/
|
|
451
|
+
async function listProjectFiles(config) {
|
|
452
|
+
const m = matcherFor(config);
|
|
453
|
+
const files = /* @__PURE__ */ new Set();
|
|
454
|
+
const emptyRoots = [];
|
|
455
|
+
for (let i = 0; i < config.project.include.length; i++) {
|
|
456
|
+
const entry = config.project.include[i];
|
|
457
|
+
const rel = relativeTo(config.root, toPosix(entry));
|
|
458
|
+
let found = false;
|
|
459
|
+
let links = /* @__PURE__ */ new Map();
|
|
460
|
+
try {
|
|
461
|
+
const info = await stat(entry);
|
|
462
|
+
if (info.isDirectory()) {
|
|
463
|
+
if (enterable(m, config, entry)) {
|
|
464
|
+
const walker = new Walk(m, config, {
|
|
465
|
+
logical: entry,
|
|
466
|
+
real: await realpath(entry)
|
|
467
|
+
}, files);
|
|
468
|
+
await walk(walker);
|
|
469
|
+
found = walker.found;
|
|
470
|
+
links = walker.links;
|
|
471
|
+
}
|
|
472
|
+
} else if (info.isFile()) {
|
|
473
|
+
const name = entry.slice(entry.lastIndexOf(sep) + 1);
|
|
474
|
+
if (hasExtension(name, m.extensions) && !isExcluded(m, rel, false)) {
|
|
475
|
+
files.add(await realpath(entry));
|
|
476
|
+
found = true;
|
|
477
|
+
}
|
|
478
|
+
}
|
|
479
|
+
} catch (error) {
|
|
480
|
+
if (!isMissing$1(error, UNREACHABLE)) throw error;
|
|
481
|
+
}
|
|
482
|
+
m.links[i] = links;
|
|
483
|
+
if (!found) emptyRoots.push(entry);
|
|
484
|
+
}
|
|
485
|
+
for (const file of await listDependencyFiles(config)) files.add(file);
|
|
486
|
+
return {
|
|
487
|
+
files: [...files].sort(),
|
|
488
|
+
emptyRoots
|
|
489
|
+
};
|
|
490
|
+
}
|
|
491
|
+
// src/core/queue.ts
|
|
492
|
+
/**
|
|
493
|
+
* Runs `task` once every task queued before it under `key` has settled, so
|
|
494
|
+
* one process never interleaves two writers of one file; a failed task does
|
|
495
|
+
* not stop the next one. The key is dropped when its last task settles.
|
|
496
|
+
*/
|
|
497
|
+
function serialized(queues, key, task) {
|
|
498
|
+
const run = (queues.get(key) ?? Promise.resolve()).then(task, task);
|
|
499
|
+
const tail = run.then(() => void 0, () => void 0);
|
|
500
|
+
queues.set(key, tail);
|
|
501
|
+
tail.then(() => {
|
|
502
|
+
if (queues.get(key) === tail) queues.delete(key);
|
|
503
|
+
});
|
|
504
|
+
return run;
|
|
505
|
+
}
|
|
506
|
+
// src/core/catalog/validity.ts
|
|
125
507
|
function sourceMatches(entry, layout, message, codec) {
|
|
126
508
|
if (layout === "id") return entry.label !== null && entry.label === message.label;
|
|
127
509
|
const source = entry.source;
|
|
@@ -129,8 +511,7 @@ function sourceMatches(entry, layout, message, codec) {
|
|
|
129
511
|
if (source === message.canonical) return true;
|
|
130
512
|
return entryCanonical(entry, codec) === message.canonical;
|
|
131
513
|
}
|
|
132
|
-
|
|
133
|
-
//#region src/core/catalog/model.ts
|
|
514
|
+
// src/core/catalog/model.ts
|
|
134
515
|
/**
|
|
135
516
|
* The catalog model shared by every reader, writer and consumer (INTERNALS
|
|
136
517
|
* §2.6.2, §2.6.5, §2.6.7): how an entry is keyed, how entries sort, and when
|
|
@@ -141,18 +522,23 @@ function emptyCatalog(locale, layout, header) {
|
|
|
141
522
|
return {
|
|
142
523
|
locale,
|
|
143
524
|
layout,
|
|
144
|
-
header:
|
|
145
|
-
language: locale,
|
|
146
|
-
idScheme: header.idScheme,
|
|
147
|
-
sourceLocale: header.sourceLocale,
|
|
148
|
-
aiModel: null,
|
|
149
|
-
extra: []
|
|
150
|
-
},
|
|
525
|
+
header: emptyHeader(locale, header),
|
|
151
526
|
entries: [],
|
|
152
527
|
obsolete: [],
|
|
153
528
|
droppedObsolete: 0
|
|
154
529
|
};
|
|
155
530
|
}
|
|
531
|
+
/** A header with every field present, as `createEntry` is for entries. */
|
|
532
|
+
function emptyHeader(locale, fields = {}) {
|
|
533
|
+
return {
|
|
534
|
+
language: locale,
|
|
535
|
+
idScheme: null,
|
|
536
|
+
sourceLocale: null,
|
|
537
|
+
aiModel: null,
|
|
538
|
+
extra: [],
|
|
539
|
+
...fields
|
|
540
|
+
};
|
|
541
|
+
}
|
|
156
542
|
/**
|
|
157
543
|
* A blank entry with every field present, so sync and tests spell out only
|
|
158
544
|
* what differs and new fields of `CatalogEntry` get a default in one place.
|
|
@@ -188,13 +574,7 @@ function entryCanonical(entry, codec) {
|
|
|
188
574
|
if (source === null) return null;
|
|
189
575
|
const memo = canonicals.get(entry);
|
|
190
576
|
if (memo !== void 0 && memo.source === source) return memo.canonical;
|
|
191
|
-
|
|
192
|
-
try {
|
|
193
|
-
canonical = codec.canonicalize(source);
|
|
194
|
-
} catch (error) {
|
|
195
|
-
if (!isDiagnosticError(error)) throw error;
|
|
196
|
-
canonical = null;
|
|
197
|
-
}
|
|
577
|
+
const canonical = tryCanonicalize(codec, source);
|
|
198
578
|
canonicals.set(entry, {
|
|
199
579
|
source,
|
|
200
580
|
canonical
|
|
@@ -202,6 +582,60 @@ function entryCanonical(entry, codec) {
|
|
|
202
582
|
return canonical;
|
|
203
583
|
}
|
|
204
584
|
/**
|
|
585
|
+
* An empty translation is untranslated whatever the file says; a filled one
|
|
586
|
+
* is `ai` only when marked so, which also covers one typed by someone who
|
|
587
|
+
* left an `untranslated` marker alone.
|
|
588
|
+
*/
|
|
589
|
+
function entryStateOf(translation, ai) {
|
|
590
|
+
if (translation === "") return "untranslated";
|
|
591
|
+
return ai ? "ai" : "translated";
|
|
592
|
+
}
|
|
593
|
+
/** Which copy of an entry wins when two disagree: `translated` over `ai` over `untranslated`. */
|
|
594
|
+
const STATE_RANK = {
|
|
595
|
+
translated: 2,
|
|
596
|
+
ai: 1,
|
|
597
|
+
untranslated: 0
|
|
598
|
+
};
|
|
599
|
+
/** The stricter of two `maxLength` limits, null meaning none: safe whichever one a translator honoured. */
|
|
600
|
+
function stricterLimit(a, b) {
|
|
601
|
+
if (a === null) return b;
|
|
602
|
+
if (b === null) return a;
|
|
603
|
+
return Math.min(a, b);
|
|
604
|
+
}
|
|
605
|
+
/** A flag's identity: both line kinds have two characters, so the key names one flag. */
|
|
606
|
+
function flagKey(flag) {
|
|
607
|
+
return flag.line + flag.name;
|
|
608
|
+
}
|
|
609
|
+
/**
|
|
610
|
+
* `values` sorted (UTF-16 order, as `compareText`) without repeats; the input
|
|
611
|
+
* itself when it has fewer than two values, so the common case allocates nothing.
|
|
612
|
+
*/
|
|
613
|
+
function sortedUnique(values) {
|
|
614
|
+
if (values.length < 2) return values;
|
|
615
|
+
const sorted = values.slice().sort();
|
|
616
|
+
let kept = 1;
|
|
617
|
+
for (let read = 1; read < sorted.length; read++) if (sorted[read] !== sorted[kept - 1]) sorted[kept++] = sorted[read];
|
|
618
|
+
sorted.length = kept;
|
|
619
|
+
return sorted;
|
|
620
|
+
}
|
|
621
|
+
/**
|
|
622
|
+
* Copies `from`'s own fields onto `into` in place (no spread per copy, which
|
|
623
|
+
* would copy everything kept so far each time); defineProperty keeps a
|
|
624
|
+
* `__proto__` field data instead of replacing the prototype.
|
|
625
|
+
*/
|
|
626
|
+
function defineFields(into, from) {
|
|
627
|
+
for (const field of Object.keys(from)) defineField(into, field, from[field]);
|
|
628
|
+
return into;
|
|
629
|
+
}
|
|
630
|
+
function defineField(into, field, value) {
|
|
631
|
+
Object.defineProperty(into, field, {
|
|
632
|
+
value,
|
|
633
|
+
enumerable: true,
|
|
634
|
+
writable: true,
|
|
635
|
+
configurable: true
|
|
636
|
+
});
|
|
637
|
+
}
|
|
638
|
+
/**
|
|
205
639
|
* The key that joins an entry to code. Null for foreign contexts (never
|
|
206
640
|
* matched) and for `"source"`-layout entries whose source does not parse,
|
|
207
641
|
* which consumers report on the source instead.
|
|
@@ -228,9 +662,22 @@ function msgidOf(entry, layout) {
|
|
|
228
662
|
case "json": return entry.manualId ?? entry.label ?? "";
|
|
229
663
|
}
|
|
230
664
|
}
|
|
231
|
-
/**
|
|
665
|
+
/**
|
|
666
|
+
* The identity git-level tools see, which needs no codec: `msgctxt` + `msgid`,
|
|
667
|
+
* or the JSON key.
|
|
668
|
+
*/
|
|
669
|
+
function catalogIdentity(entry, layout) {
|
|
670
|
+
if (layout === "json") return msgidOf(entry, layout);
|
|
671
|
+
return `${contextOf(entry, layout)}\u0004${msgidOf(entry, layout)}`;
|
|
672
|
+
}
|
|
673
|
+
/**
|
|
674
|
+
* UTF-16 code-unit order: `localeCompare` would differ between machines and
|
|
675
|
+
* ICU versions. A plain string array sorts the same with `.sort()` and no
|
|
676
|
+
* comparator.
|
|
677
|
+
*/
|
|
232
678
|
function compareText(a, b) {
|
|
233
|
-
|
|
679
|
+
if (a === b) return 0;
|
|
680
|
+
return a < b ? -1 : 1;
|
|
234
681
|
}
|
|
235
682
|
/**
|
|
236
683
|
* Catalog order (INTERNALS §2.6.5): first reference (entries without one
|
|
@@ -279,31 +726,30 @@ function usableTranslation(entry, layout, message, codec, locale) {
|
|
|
279
726
|
reason: "fuzzy",
|
|
280
727
|
diagnostics: []
|
|
281
728
|
};
|
|
729
|
+
const errors = translationErrors(entry.translation, message.canonical, codec, locale);
|
|
730
|
+
if (errors.length > 0) return {
|
|
731
|
+
ok: false,
|
|
732
|
+
reason: "invalid",
|
|
733
|
+
diagnostics: errors.map((error) => withSubject(error, locale, message.id, entry.line))
|
|
734
|
+
};
|
|
735
|
+
return {
|
|
736
|
+
ok: true,
|
|
737
|
+
parsed: codec.parse(entry.translation)
|
|
738
|
+
};
|
|
739
|
+
}
|
|
740
|
+
/** The ICU errors that keep `translation` of `canonical` from shipping: a parse error, or what validation finds. */
|
|
741
|
+
function translationErrors(translation, canonical, codec, locale) {
|
|
282
742
|
let parsed;
|
|
283
743
|
try {
|
|
284
|
-
parsed = codec.parse(
|
|
744
|
+
parsed = codec.parse(translation);
|
|
285
745
|
} catch (error) {
|
|
286
746
|
if (!isDiagnosticError(error)) throw error;
|
|
287
|
-
return
|
|
288
|
-
ok: false,
|
|
289
|
-
reason: "invalid",
|
|
290
|
-
diagnostics: [withSubject(error.diagnostic, locale, message.id, entry.line)]
|
|
291
|
-
};
|
|
747
|
+
return [error.diagnostic];
|
|
292
748
|
}
|
|
293
|
-
|
|
749
|
+
return validateTranslation(parsed, codec.parse(canonical), {
|
|
294
750
|
locale,
|
|
295
|
-
text:
|
|
296
|
-
});
|
|
297
|
-
let errors = null;
|
|
298
|
-
for (const diagnostic of found) if (diagnostic.severity === "error") (errors ??= []).push(withSubject(diagnostic, locale, message.id, entry.line));
|
|
299
|
-
return errors === null ? {
|
|
300
|
-
ok: true,
|
|
301
|
-
parsed
|
|
302
|
-
} : {
|
|
303
|
-
ok: false,
|
|
304
|
-
reason: "invalid",
|
|
305
|
-
diagnostics: errors
|
|
306
|
-
};
|
|
751
|
+
text: translation
|
|
752
|
+
}).filter((diagnostic) => diagnostic.severity === "error");
|
|
307
753
|
}
|
|
308
754
|
/**
|
|
309
755
|
* ICU diagnostics locate problems inside the message text; for a catalog the
|
|
@@ -320,8 +766,15 @@ function withSubject(diagnostic, locale, id, line) {
|
|
|
320
766
|
if (line > 0) located.line = line;
|
|
321
767
|
return located;
|
|
322
768
|
}
|
|
323
|
-
|
|
324
|
-
|
|
769
|
+
// src/core/catalog/json.ts
|
|
770
|
+
/**
|
|
771
|
+
* JSON catalogs (INTERNALS §2.6.4, PLAN §5.6). The writer controls every byte
|
|
772
|
+
* (key order, two-space indent, inline string arrays) so output is
|
|
773
|
+
* deterministic and `source` always sits on the line before `translation`.
|
|
774
|
+
* The reader finds duplicate message keys by scanning the text, because
|
|
775
|
+
* `JSON.parse` silently keeps the last one and would hide a git-merge
|
|
776
|
+
* duplicate.
|
|
777
|
+
*/
|
|
325
778
|
const CATALOG_SCHEMA_URL = "https://unpkg.com/@menapo/vi18n/schemas/catalog.json";
|
|
326
779
|
/** Top-level keys the reader maps onto the header; anything else is preserved in `extra`. */
|
|
327
780
|
const OWNED_KEYS = /* @__PURE__ */ new Set([
|
|
@@ -442,15 +895,12 @@ function scanMessages(text) {
|
|
|
442
895
|
}
|
|
443
896
|
return null;
|
|
444
897
|
}
|
|
445
|
-
function isRecord(value) {
|
|
898
|
+
function isRecord$1(value) {
|
|
446
899
|
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
447
900
|
}
|
|
448
901
|
function isStringArray(value) {
|
|
449
902
|
return Array.isArray(value) && value.every((item) => typeof item === "string");
|
|
450
903
|
}
|
|
451
|
-
function sortedUnique$1(values) {
|
|
452
|
-
return [...new Set(values)].sort((a, b) => a < b ? -1 : a > b ? 1 : 0);
|
|
453
|
-
}
|
|
454
904
|
/** Line and column of a `JSON.parse` error, when V8 names a position. */
|
|
455
905
|
function errorPosition(text, error) {
|
|
456
906
|
const match = /position (\d+)/.exec(error instanceof Error ? error.message : "");
|
|
@@ -467,39 +917,78 @@ function errorPosition(text, error) {
|
|
|
467
917
|
column: offset - lineStart + 1
|
|
468
918
|
};
|
|
469
919
|
}
|
|
920
|
+
/** Each model field's check and the error naming its shape, in the order they are reported. */
|
|
921
|
+
const FIELD_CHECKS = [
|
|
922
|
+
[
|
|
923
|
+
"context",
|
|
924
|
+
(value) => typeof value === "string",
|
|
925
|
+
"context must be a string"
|
|
926
|
+
],
|
|
927
|
+
[
|
|
928
|
+
"fuzzy",
|
|
929
|
+
(value) => typeof value === "boolean",
|
|
930
|
+
"fuzzy must be true or absent"
|
|
931
|
+
],
|
|
932
|
+
[
|
|
933
|
+
"maxLength",
|
|
934
|
+
(value) => typeof value === "number" && Number.isInteger(value) && value > 0,
|
|
935
|
+
"maxLength must be a positive integer"
|
|
936
|
+
],
|
|
937
|
+
[
|
|
938
|
+
"flags",
|
|
939
|
+
isStringArray,
|
|
940
|
+
"flags must be an array of strings"
|
|
941
|
+
],
|
|
942
|
+
[
|
|
943
|
+
"refs",
|
|
944
|
+
isStringArray,
|
|
945
|
+
"refs must be an array of strings"
|
|
946
|
+
],
|
|
947
|
+
[
|
|
948
|
+
"state",
|
|
949
|
+
(value) => typeof value === "string" && STATES.has(value),
|
|
950
|
+
"state must be \"untranslated\", \"ai\" or \"translated\""
|
|
951
|
+
],
|
|
952
|
+
[
|
|
953
|
+
"source",
|
|
954
|
+
(value) => value === null || typeof value === "string",
|
|
955
|
+
"source must be a string"
|
|
956
|
+
],
|
|
957
|
+
[
|
|
958
|
+
"translation",
|
|
959
|
+
(value) => typeof value === "string",
|
|
960
|
+
"translation must be a string"
|
|
961
|
+
]
|
|
962
|
+
];
|
|
963
|
+
function readFields(key, value) {
|
|
964
|
+
for (const [field, valid, error] of FIELD_CHECKS) if (value[field] !== void 0 && !valid(value[field])) return `${key}: ${error}`;
|
|
965
|
+
return value;
|
|
966
|
+
}
|
|
967
|
+
/** The fields of other tools, kept for the writer; undefined when there are none. */
|
|
968
|
+
function extraFields(value) {
|
|
969
|
+
let extra;
|
|
970
|
+
for (const field of Object.keys(value)) if (!MESSAGE_FIELDS.has(field)) defineField(extra ??= {}, field, value[field]);
|
|
971
|
+
return extra;
|
|
972
|
+
}
|
|
470
973
|
function readEntry(key, value, line, isSourceLocale) {
|
|
471
|
-
if (!isRecord(value)) return { error: `message ${JSON.stringify(key)} must be an object` };
|
|
472
|
-
const
|
|
473
|
-
if (
|
|
474
|
-
|
|
475
|
-
if (maxLength !== void 0 && !(Number.isInteger(maxLength) && maxLength > 0)) return { error: `${key}: maxLength must be a positive integer` };
|
|
476
|
-
if (flags !== void 0 && !isStringArray(flags)) return { error: `${key}: flags must be an array of strings` };
|
|
477
|
-
if (refs !== void 0 && !isStringArray(refs)) return { error: `${key}: refs must be an array of strings` };
|
|
478
|
-
if (state !== void 0 && (typeof state !== "string" || !STATES.has(state))) return { error: `${key}: state must be "untranslated", "ai" or "translated"` };
|
|
479
|
-
if (source !== void 0 && source !== null && typeof source !== "string") return { error: `${key}: source must be a string` };
|
|
480
|
-
if (translation !== void 0 && typeof translation !== "string") return { error: `${key}: translation must be a string` };
|
|
974
|
+
if (!isRecord$1(value)) return { error: `message ${JSON.stringify(key)} must be an object` };
|
|
975
|
+
const fields = readFields(key, value);
|
|
976
|
+
if (typeof fields === "string") return { error: fields };
|
|
977
|
+
const { context, fuzzy, maxLength, flags, refs, state, source, translation } = fields;
|
|
481
978
|
const text = isSourceLocale ? "" : translation ?? "";
|
|
482
|
-
const
|
|
483
|
-
let extra;
|
|
484
|
-
for (const field of Object.keys(value)) if (!MESSAGE_FIELDS.has(field)) Object.defineProperty(extra ??= {}, field, {
|
|
485
|
-
value: value[field],
|
|
486
|
-
enumerable: true,
|
|
487
|
-
writable: true,
|
|
488
|
-
configurable: true
|
|
489
|
-
});
|
|
490
|
-
const manual = key.includes(".");
|
|
979
|
+
const manual = isManualId(key);
|
|
491
980
|
const entry = {
|
|
492
981
|
manualId: manual ? key : null,
|
|
493
982
|
foreignContext: null,
|
|
494
983
|
label: manual ? null : key,
|
|
495
984
|
source: typeof source === "string" ? source : null,
|
|
496
985
|
translation: text,
|
|
497
|
-
state:
|
|
986
|
+
state: entryStateOf(text, state === "ai"),
|
|
498
987
|
fuzzy: fuzzy === true,
|
|
499
|
-
contexts:
|
|
988
|
+
contexts: context === void 0 ? [] : splitContexts(context),
|
|
500
989
|
comments: [],
|
|
501
|
-
refs: refs === void 0 ? [] : sortedUnique
|
|
502
|
-
maxLength: maxLength
|
|
990
|
+
refs: refs === void 0 ? [] : sortedUnique(refs),
|
|
991
|
+
maxLength: maxLength ?? null,
|
|
503
992
|
flags: flags === void 0 ? [] : [...new Set(flags)].map((name) => ({
|
|
504
993
|
name,
|
|
505
994
|
line: "#,"
|
|
@@ -507,19 +996,46 @@ function readEntry(key, value, line, isSourceLocale) {
|
|
|
507
996
|
line,
|
|
508
997
|
wrapped: false
|
|
509
998
|
};
|
|
999
|
+
const extra = extraFields(value);
|
|
510
1000
|
if (extra !== void 0) entry.extra = extra;
|
|
511
1001
|
return { entry };
|
|
512
1002
|
}
|
|
1003
|
+
/** The top-level keys onto the header; keys of other tools go to `extra`. */
|
|
1004
|
+
function readTopLevel(root, header, fail) {
|
|
1005
|
+
for (const [key, value] of Object.entries(root)) switch (key) {
|
|
1006
|
+
case "$schema":
|
|
1007
|
+
case "messages": break;
|
|
1008
|
+
case "locale":
|
|
1009
|
+
if (typeof value === "string" && value !== "") header.language = value;
|
|
1010
|
+
else fail("locale must be a string");
|
|
1011
|
+
break;
|
|
1012
|
+
case "sourceLocale":
|
|
1013
|
+
case "idScheme":
|
|
1014
|
+
case "aiModel":
|
|
1015
|
+
if (typeof value === "string") header[key] = value;
|
|
1016
|
+
else if (value !== null) fail(`${key} must be a string`);
|
|
1017
|
+
break;
|
|
1018
|
+
default: header.extra.push([key, value]);
|
|
1019
|
+
}
|
|
1020
|
+
}
|
|
1021
|
+
/** The entries of `messages` in text order, duplicate keys included (they are read from the text). */
|
|
1022
|
+
function readMessages(text, messages, isSourceLocale, catalog, fail) {
|
|
1023
|
+
const members = scanMessages(text) ?? [];
|
|
1024
|
+
const seen = /* @__PURE__ */ new Set();
|
|
1025
|
+
let duplicates = null;
|
|
1026
|
+
for (const member of members) if (seen.has(member.key)) (duplicates ??= /* @__PURE__ */ new Set()).add(member.key);
|
|
1027
|
+
else seen.add(member.key);
|
|
1028
|
+
for (const member of members) {
|
|
1029
|
+
const value = duplicates !== null && duplicates.has(member.key) ? JSON.parse(text.slice(member.start, member.end)) : messages[member.key];
|
|
1030
|
+
const read = readEntry(member.key, value, member.line, isSourceLocale);
|
|
1031
|
+
if ("error" in read) fail(read.error, member.line);
|
|
1032
|
+
else catalog.entries.push(read.entry);
|
|
1033
|
+
}
|
|
1034
|
+
}
|
|
513
1035
|
function parseJsonCatalog(text, options) {
|
|
514
1036
|
const { locale, isSourceLocale } = options;
|
|
515
1037
|
const diagnostics = [];
|
|
516
|
-
const header =
|
|
517
|
-
language: locale,
|
|
518
|
-
idScheme: null,
|
|
519
|
-
sourceLocale: null,
|
|
520
|
-
aiModel: null,
|
|
521
|
-
extra: []
|
|
522
|
-
};
|
|
1038
|
+
const header = emptyHeader(locale);
|
|
523
1039
|
const catalog = {
|
|
524
1040
|
locale,
|
|
525
1041
|
layout: "json",
|
|
@@ -553,51 +1069,21 @@ function parseJsonCatalog(text, options) {
|
|
|
553
1069
|
diagnostics
|
|
554
1070
|
};
|
|
555
1071
|
}
|
|
556
|
-
if (!isRecord(root)) {
|
|
1072
|
+
if (!isRecord$1(root)) {
|
|
557
1073
|
fail("a JSON catalog must be an object", 1);
|
|
558
1074
|
return {
|
|
559
1075
|
catalog,
|
|
560
1076
|
diagnostics
|
|
561
1077
|
};
|
|
562
1078
|
}
|
|
563
|
-
|
|
564
|
-
case "$schema":
|
|
565
|
-
case "messages": break;
|
|
566
|
-
case "locale":
|
|
567
|
-
if (typeof value === "string" && value !== "") header.language = value;
|
|
568
|
-
else fail("locale must be a string");
|
|
569
|
-
break;
|
|
570
|
-
case "sourceLocale":
|
|
571
|
-
case "idScheme":
|
|
572
|
-
case "aiModel":
|
|
573
|
-
if (typeof value === "string") header[key] = value;
|
|
574
|
-
else if (value !== null) fail(`${key} must be a string`);
|
|
575
|
-
break;
|
|
576
|
-
default: if (!OWNED_KEYS.has(key)) header.extra.push([key, value]);
|
|
577
|
-
}
|
|
1079
|
+
readTopLevel(root, header, fail);
|
|
578
1080
|
const messages = root.messages;
|
|
579
1081
|
if (messages === void 0) return {
|
|
580
1082
|
catalog,
|
|
581
1083
|
diagnostics
|
|
582
1084
|
};
|
|
583
|
-
if (!isRecord(messages))
|
|
584
|
-
|
|
585
|
-
return {
|
|
586
|
-
catalog,
|
|
587
|
-
diagnostics
|
|
588
|
-
};
|
|
589
|
-
}
|
|
590
|
-
const members = scanMessages(text) ?? [];
|
|
591
|
-
const seen = /* @__PURE__ */ new Set();
|
|
592
|
-
let duplicates = null;
|
|
593
|
-
for (const member of members) if (seen.has(member.key)) (duplicates ??= /* @__PURE__ */ new Set()).add(member.key);
|
|
594
|
-
else seen.add(member.key);
|
|
595
|
-
for (const member of members) {
|
|
596
|
-
const value = duplicates !== null && duplicates.has(member.key) ? JSON.parse(text.slice(member.start, member.end)) : messages[member.key];
|
|
597
|
-
const read = readEntry(member.key, value, member.line, isSourceLocale);
|
|
598
|
-
if ("error" in read) fail(read.error, member.line);
|
|
599
|
-
else catalog.entries.push(read.entry);
|
|
600
|
-
}
|
|
1085
|
+
if (!isRecord$1(messages)) fail("messages must be an object");
|
|
1086
|
+
else readMessages(text, messages, isSourceLocale, catalog, fail);
|
|
601
1087
|
return {
|
|
602
1088
|
catalog,
|
|
603
1089
|
diagnostics
|
|
@@ -617,7 +1103,7 @@ function stringify(value, indent) {
|
|
|
617
1103
|
const inner = `${indent} `;
|
|
618
1104
|
return `[\n${value.map((item) => inner + stringify(item, inner)).join(",\n")}\n${indent}]`;
|
|
619
1105
|
}
|
|
620
|
-
if (isRecord(value)) {
|
|
1106
|
+
if (isRecord$1(value)) {
|
|
621
1107
|
const keys = Object.keys(value).filter((key) => value[key] !== void 0);
|
|
622
1108
|
if (keys.length === 0) return "{}";
|
|
623
1109
|
const inner = `${indent} `;
|
|
@@ -659,15 +1145,14 @@ function serializeJsonCatalog(catalog, options) {
|
|
|
659
1145
|
if (entries.length === 0) top.push(" \"messages\": {}");
|
|
660
1146
|
else {
|
|
661
1147
|
const members = entries.map((entry) => {
|
|
662
|
-
const key = entry
|
|
1148
|
+
const key = catalogIdentity(entry, "json");
|
|
663
1149
|
return ` ${JSON.stringify(key)}: {\n${entryLines(entry, options.isSourceLocale).join(",\n")}\n }`;
|
|
664
1150
|
});
|
|
665
1151
|
top.push(` "messages": {\n${members.join(",\n")}\n }`);
|
|
666
1152
|
}
|
|
667
1153
|
return `{\n${top.join(",\n")}\n}\n`;
|
|
668
1154
|
}
|
|
669
|
-
|
|
670
|
-
//#region src/core/catalog/plural-forms.ts
|
|
1155
|
+
// src/core/catalog/plural-forms.ts
|
|
671
1156
|
/**
|
|
672
1157
|
* The PO `Plural-Forms` header (PLAN §4.4 P1, INTERNALS §2.6.3). vi18n never
|
|
673
1158
|
* evaluates it: an ICU message carries its own plural cases, and vi18n never
|
|
@@ -739,8 +1224,7 @@ function pluralFormsFor(language) {
|
|
|
739
1224
|
function isPluralFormsPlaceholder(value) {
|
|
740
1225
|
return /\bINTEGER\b|\bEXPRESSION\b/.test(value) || !/\bnplurals\s*=\s*\d/.test(value);
|
|
741
1226
|
}
|
|
742
|
-
|
|
743
|
-
//#region src/core/catalog/po.ts
|
|
1227
|
+
// src/core/catalog/po.ts
|
|
744
1228
|
/**
|
|
745
1229
|
* Header fields the writer always emits itself; keeping them in `extra` would
|
|
746
1230
|
* write them twice.
|
|
@@ -953,38 +1437,23 @@ function readFlags(text, line, draft) {
|
|
|
953
1437
|
if (name === "") continue;
|
|
954
1438
|
if (name === "fuzzy") draft.fuzzy = true;
|
|
955
1439
|
else if (name === "ai") draft.ai = true;
|
|
956
|
-
else if (name.startsWith("max-length:") && /^max-length:[1-9]\d*$/.test(name))
|
|
957
|
-
|
|
958
|
-
|
|
959
|
-
|
|
960
|
-
|
|
1440
|
+
else if (name.startsWith("max-length:") && /^max-length:[1-9]\d*$/.test(name)) draft.maxLength = stricterLimit(draft.maxLength, Number(name.slice(11)));
|
|
1441
|
+
else {
|
|
1442
|
+
const flag = {
|
|
1443
|
+
name,
|
|
1444
|
+
line
|
|
1445
|
+
};
|
|
1446
|
+
const key = flagKey(flag);
|
|
961
1447
|
const keys = draft.flagKeys ??= /* @__PURE__ */ new Set();
|
|
962
1448
|
if (!keys.has(key)) {
|
|
963
1449
|
keys.add(key);
|
|
964
|
-
draft.flags.push(
|
|
965
|
-
name,
|
|
966
|
-
line
|
|
967
|
-
});
|
|
1450
|
+
draft.flags.push(flag);
|
|
968
1451
|
}
|
|
969
1452
|
}
|
|
970
1453
|
}
|
|
971
1454
|
}
|
|
972
|
-
function sortedUnique(values) {
|
|
973
|
-
if (values.length < 2) return values;
|
|
974
|
-
const sorted = values.slice().sort((a, b) => a < b ? -1 : a > b ? 1 : 0);
|
|
975
|
-
let w = 1;
|
|
976
|
-
for (let r = 1; r < sorted.length; r++) if (sorted[r] !== sorted[w - 1]) sorted[w++] = sorted[r];
|
|
977
|
-
sorted.length = w;
|
|
978
|
-
return sorted;
|
|
979
|
-
}
|
|
980
1455
|
function readHeader(msgstr, locale) {
|
|
981
|
-
const header =
|
|
982
|
-
language: locale,
|
|
983
|
-
idScheme: null,
|
|
984
|
-
sourceLocale: null,
|
|
985
|
-
aiModel: null,
|
|
986
|
-
extra: []
|
|
987
|
-
};
|
|
1456
|
+
const header = emptyHeader(locale);
|
|
988
1457
|
for (const field of msgstr.split("\n")) {
|
|
989
1458
|
const colon = field.indexOf(":");
|
|
990
1459
|
if (colon <= 0) continue;
|
|
@@ -1002,183 +1471,187 @@ function readHeader(msgstr, locale) {
|
|
|
1002
1471
|
return header;
|
|
1003
1472
|
}
|
|
1004
1473
|
function parsePo(text, options) {
|
|
1005
|
-
const
|
|
1006
|
-
const diagnostics = [];
|
|
1007
|
-
const entries = [];
|
|
1008
|
-
const obsolete = [];
|
|
1009
|
-
let header = null;
|
|
1010
|
-
let draft = newDraft();
|
|
1011
|
-
let block = null;
|
|
1012
|
-
let blockHasMsgstr = false;
|
|
1013
|
-
const closeBlock = () => {
|
|
1014
|
-
if (block !== null) {
|
|
1015
|
-
obsolete.push(block.join("\n"));
|
|
1016
|
-
block = null;
|
|
1017
|
-
blockHasMsgstr = false;
|
|
1018
|
-
}
|
|
1019
|
-
};
|
|
1020
|
-
const finish = () => {
|
|
1021
|
-
const done = draft;
|
|
1022
|
-
if (!hasKeywords(done)) return;
|
|
1023
|
-
draft = newDraft();
|
|
1024
|
-
if (done.error !== null) {
|
|
1025
|
-
diagnostics.push(done.error);
|
|
1026
|
-
return;
|
|
1027
|
-
}
|
|
1028
|
-
if (done.msgid === null) {
|
|
1029
|
-
diagnostics.push(parseError(done.firstLine, "entry without msgid"));
|
|
1030
|
-
return;
|
|
1031
|
-
}
|
|
1032
|
-
if (done.plural) {
|
|
1033
|
-
diagnostics.push(parseError(done.line, "gettext plural entries (msgid_plural) are not supported: write the plural as ICU in the source text"));
|
|
1034
|
-
return;
|
|
1035
|
-
}
|
|
1036
|
-
if (done.msgstr === null) {
|
|
1037
|
-
diagnostics.push(parseError(done.line, "msgid without msgstr"));
|
|
1038
|
-
return;
|
|
1039
|
-
}
|
|
1040
|
-
if (done.msgid === "" && done.msgctxt === null && header === null && entries.length === 0) {
|
|
1041
|
-
header = readHeader(done.msgstr, locale);
|
|
1042
|
-
return;
|
|
1043
|
-
}
|
|
1044
|
-
const manual = done.msgctxt !== null && done.msgctxt.includes(".");
|
|
1045
|
-
const translation = isSourceLocale ? "" : done.msgstr;
|
|
1046
|
-
let source;
|
|
1047
|
-
let label = null;
|
|
1048
|
-
if (layout === "source") source = done.msgid;
|
|
1049
|
-
else {
|
|
1050
|
-
label = done.msgid;
|
|
1051
|
-
source = isSourceLocale ? done.msgstr : done.sourceNote;
|
|
1052
|
-
}
|
|
1053
|
-
const state = translation === "" ? "untranslated" : done.ai ? "ai" : "translated";
|
|
1054
|
-
entries.push({
|
|
1055
|
-
manualId: manual ? done.msgctxt : null,
|
|
1056
|
-
foreignContext: manual ? null : done.msgctxt,
|
|
1057
|
-
label,
|
|
1058
|
-
source,
|
|
1059
|
-
translation,
|
|
1060
|
-
state,
|
|
1061
|
-
fuzzy: done.fuzzy,
|
|
1062
|
-
contexts: done.contexts,
|
|
1063
|
-
comments: done.comments,
|
|
1064
|
-
refs: sortedUnique(done.refs),
|
|
1065
|
-
maxLength: done.maxLength,
|
|
1066
|
-
flags: done.flags,
|
|
1067
|
-
line: done.line,
|
|
1068
|
-
wrapped: done.wrapped
|
|
1069
|
-
});
|
|
1070
|
-
};
|
|
1071
|
-
const fail = (line, message) => {
|
|
1072
|
-
draft.error ??= parseError(line, message);
|
|
1073
|
-
};
|
|
1474
|
+
const reader = new PoReader(options);
|
|
1074
1475
|
const start = text.charCodeAt(0) === 65279 ? 1 : 0;
|
|
1075
1476
|
const lines = (start === 0 ? text : text.slice(start)).split("\n");
|
|
1076
1477
|
for (let index = 0; index < lines.length; index++) {
|
|
1077
|
-
const
|
|
1078
|
-
|
|
1079
|
-
|
|
1478
|
+
const raw = lines[index];
|
|
1479
|
+
reader.readLine(raw.endsWith("\r") ? raw.slice(0, -1) : raw, index + 1);
|
|
1480
|
+
}
|
|
1481
|
+
return reader.end();
|
|
1482
|
+
}
|
|
1483
|
+
/** The reader's state between lines: the entry being read, and the `#~` block being collected. */
|
|
1484
|
+
var PoReader = class {
|
|
1485
|
+
#options;
|
|
1486
|
+
#diagnostics = [];
|
|
1487
|
+
#entries = [];
|
|
1488
|
+
#obsolete = [];
|
|
1489
|
+
#header = null;
|
|
1490
|
+
#draft = newDraft();
|
|
1491
|
+
#block = null;
|
|
1492
|
+
#blockHasMsgstr = false;
|
|
1493
|
+
constructor(options) {
|
|
1494
|
+
this.#options = options;
|
|
1495
|
+
}
|
|
1496
|
+
readLine(raw, lineNo) {
|
|
1080
1497
|
let s = 0;
|
|
1081
1498
|
while (s < raw.length && (raw[s] === " " || raw[s] === " ")) s++;
|
|
1082
1499
|
if (s === raw.length) {
|
|
1083
|
-
finish();
|
|
1084
|
-
closeBlock();
|
|
1085
|
-
|
|
1500
|
+
this.#finish();
|
|
1501
|
+
this.#closeBlock();
|
|
1502
|
+
return;
|
|
1086
1503
|
}
|
|
1087
1504
|
const line = s === 0 ? raw : raw.slice(s);
|
|
1088
1505
|
if (line.startsWith("#~")) {
|
|
1089
|
-
|
|
1090
|
-
|
|
1091
|
-
|
|
1092
|
-
|
|
1093
|
-
|
|
1094
|
-
|
|
1095
|
-
|
|
1096
|
-
|
|
1097
|
-
|
|
1098
|
-
|
|
1099
|
-
|
|
1506
|
+
this.#readObsolete(line, raw);
|
|
1507
|
+
return;
|
|
1508
|
+
}
|
|
1509
|
+
this.#closeBlock();
|
|
1510
|
+
const first = line.charCodeAt(0);
|
|
1511
|
+
if (first === 35) this.#readComment(line, raw, lineNo);
|
|
1512
|
+
else if (first === 34) this.#readContinuation(line, lineNo);
|
|
1513
|
+
else this.#readKeyword(line, lineNo);
|
|
1514
|
+
}
|
|
1515
|
+
end() {
|
|
1516
|
+
this.#finish();
|
|
1517
|
+
this.#closeBlock();
|
|
1518
|
+
const { locale, layout } = this.#options;
|
|
1519
|
+
return {
|
|
1520
|
+
catalog: {
|
|
1521
|
+
locale,
|
|
1522
|
+
layout,
|
|
1523
|
+
header: this.#header ?? emptyHeader(locale),
|
|
1524
|
+
entries: this.#entries,
|
|
1525
|
+
obsolete: this.#obsolete,
|
|
1526
|
+
droppedObsolete: this.#obsolete.length
|
|
1527
|
+
},
|
|
1528
|
+
diagnostics: this.#diagnostics
|
|
1529
|
+
};
|
|
1530
|
+
}
|
|
1531
|
+
#closeBlock() {
|
|
1532
|
+
if (this.#block === null) return;
|
|
1533
|
+
this.#obsolete.push(this.#block.join("\n"));
|
|
1534
|
+
this.#block = null;
|
|
1535
|
+
this.#blockHasMsgstr = false;
|
|
1536
|
+
}
|
|
1537
|
+
#fail(line, message) {
|
|
1538
|
+
this.#draft.error ??= parseError(line, message);
|
|
1539
|
+
}
|
|
1540
|
+
#readObsolete(line, raw) {
|
|
1541
|
+
this.#finish();
|
|
1542
|
+
const body = line.slice(2).trimStart();
|
|
1543
|
+
const startsEntry = body.startsWith("msgid") || body.startsWith("msgctxt");
|
|
1544
|
+
if (this.#block !== null && this.#blockHasMsgstr && startsEntry) this.#closeBlock();
|
|
1545
|
+
if (this.#block === null) {
|
|
1546
|
+
this.#block = this.#draft.raw;
|
|
1547
|
+
this.#draft = newDraft();
|
|
1100
1548
|
}
|
|
1101
|
-
|
|
1102
|
-
if (
|
|
1103
|
-
|
|
1104
|
-
|
|
1105
|
-
|
|
1106
|
-
|
|
1107
|
-
|
|
1108
|
-
|
|
1109
|
-
|
|
1110
|
-
|
|
1111
|
-
|
|
1112
|
-
|
|
1113
|
-
|
|
1114
|
-
|
|
1115
|
-
|
|
1116
|
-
|
|
1117
|
-
|
|
1118
|
-
|
|
1119
|
-
|
|
1120
|
-
|
|
1121
|
-
|
|
1122
|
-
|
|
1123
|
-
|
|
1124
|
-
|
|
1125
|
-
|
|
1126
|
-
|
|
1549
|
+
this.#block.push(raw);
|
|
1550
|
+
if (body.startsWith("msgstr")) this.#blockHasMsgstr = true;
|
|
1551
|
+
}
|
|
1552
|
+
#readComment(line, raw, lineNo) {
|
|
1553
|
+
if (hasKeywords(this.#draft)) this.#finish();
|
|
1554
|
+
const into = this.#draft;
|
|
1555
|
+
if (into.firstLine === 0) into.firstLine = lineNo;
|
|
1556
|
+
into.raw.push(raw);
|
|
1557
|
+
const kind = line[1];
|
|
1558
|
+
const rest = line.slice(2);
|
|
1559
|
+
const content = rest.startsWith(" ") ? rest.slice(1) : rest;
|
|
1560
|
+
switch (kind) {
|
|
1561
|
+
case ".":
|
|
1562
|
+
this.#readNote(content, lineNo);
|
|
1563
|
+
break;
|
|
1564
|
+
case ":":
|
|
1565
|
+
readRefs(content, into.refs);
|
|
1566
|
+
break;
|
|
1567
|
+
case ",":
|
|
1568
|
+
readFlags(content, "#,", into);
|
|
1569
|
+
break;
|
|
1570
|
+
case "=":
|
|
1571
|
+
readFlags(content, "#=", into);
|
|
1572
|
+
break;
|
|
1573
|
+
case "|": break;
|
|
1574
|
+
default: {
|
|
1127
1575
|
const body = line.slice(1);
|
|
1128
|
-
|
|
1576
|
+
into.comments.push(body.startsWith(" ") ? body.slice(1) : body);
|
|
1129
1577
|
}
|
|
1130
|
-
continue;
|
|
1131
1578
|
}
|
|
1132
|
-
|
|
1133
|
-
|
|
1134
|
-
|
|
1135
|
-
|
|
1136
|
-
|
|
1137
|
-
|
|
1138
|
-
|
|
1139
|
-
}
|
|
1140
|
-
if (value === null) {
|
|
1141
|
-
fail(lineNo, "malformed string");
|
|
1142
|
-
continue;
|
|
1143
|
-
}
|
|
1144
|
-
switch (draft.field) {
|
|
1145
|
-
case "msgctxt":
|
|
1146
|
-
draft.msgctxt = (draft.msgctxt ?? "") + value;
|
|
1147
|
-
break;
|
|
1148
|
-
case "msgid":
|
|
1149
|
-
draft.msgid = (draft.msgid ?? "") + value;
|
|
1150
|
-
draft.wrapped = true;
|
|
1151
|
-
break;
|
|
1152
|
-
case "msgstr":
|
|
1153
|
-
draft.msgstr = (draft.msgstr ?? "") + value;
|
|
1154
|
-
draft.wrapped = true;
|
|
1155
|
-
}
|
|
1156
|
-
continue;
|
|
1579
|
+
}
|
|
1580
|
+
/** A `#.` line: a developer note, or in the `"id"` layout possibly the `source:` note. */
|
|
1581
|
+
#readNote(content, lineNo) {
|
|
1582
|
+
const { layout, isSourceLocale } = this.#options;
|
|
1583
|
+
if (layout !== "id" || !content.startsWith("source:")) {
|
|
1584
|
+
this.#draft.contexts.push(layout === "id" && ESCAPED_SOURCE_LIKE.test(content) ? content.slice(1) : content);
|
|
1585
|
+
return;
|
|
1157
1586
|
}
|
|
1587
|
+
if (isSourceLocale) return;
|
|
1588
|
+
const body = content.slice(content.startsWith("source: ") ? 8 : 7);
|
|
1589
|
+
const note = unescapePo(body);
|
|
1590
|
+
if (note === null) this.#diagnostics.push({
|
|
1591
|
+
code: "catalog-parse",
|
|
1592
|
+
severity: "warning",
|
|
1593
|
+
message: "invalid escape in #. source:; the note is kept as written",
|
|
1594
|
+
line: lineNo
|
|
1595
|
+
});
|
|
1596
|
+
this.#draft.sourceNote = note ?? body;
|
|
1597
|
+
}
|
|
1598
|
+
#readContinuation(line, lineNo) {
|
|
1599
|
+
const draft = this.#draft;
|
|
1600
|
+
const value = readString(line, 0);
|
|
1601
|
+
if (draft.field === null) {
|
|
1602
|
+
this.#fail(lineNo, "string continuation without a keyword");
|
|
1603
|
+
draft.firstLine ||= lineNo;
|
|
1604
|
+
draft.field = "msgstr";
|
|
1605
|
+
return;
|
|
1606
|
+
}
|
|
1607
|
+
if (value === null) {
|
|
1608
|
+
this.#fail(lineNo, "malformed string");
|
|
1609
|
+
return;
|
|
1610
|
+
}
|
|
1611
|
+
switch (draft.field) {
|
|
1612
|
+
case "msgctxt":
|
|
1613
|
+
draft.msgctxt = (draft.msgctxt ?? "") + value;
|
|
1614
|
+
break;
|
|
1615
|
+
case "msgid":
|
|
1616
|
+
draft.msgid = (draft.msgid ?? "") + value;
|
|
1617
|
+
draft.wrapped = true;
|
|
1618
|
+
break;
|
|
1619
|
+
case "msgstr":
|
|
1620
|
+
draft.msgstr = (draft.msgstr ?? "") + value;
|
|
1621
|
+
draft.wrapped = true;
|
|
1622
|
+
}
|
|
1623
|
+
}
|
|
1624
|
+
#readKeyword(line, lineNo) {
|
|
1158
1625
|
const keyword = /^(msgctxt|msgid_plural|msgid|msgstr\[\d+\]|msgstr)(?=[\s"])/.exec(line);
|
|
1159
1626
|
if (keyword === null) {
|
|
1627
|
+
const draft = this.#draft;
|
|
1160
1628
|
if (!hasKeywords(draft)) draft.firstLine ||= lineNo;
|
|
1161
1629
|
draft.field ??= "msgstr";
|
|
1162
|
-
fail(lineNo, `unexpected line: ${line.length > 40 ? `${line.slice(0, 40)}…` : line}`);
|
|
1163
|
-
|
|
1630
|
+
this.#fail(lineNo, `unexpected line: ${line.length > 40 ? `${line.slice(0, 40)}…` : line}`);
|
|
1631
|
+
return;
|
|
1164
1632
|
}
|
|
1165
1633
|
const word = keyword[1];
|
|
1166
1634
|
const field = word.startsWith("msgstr[") ? "msgstr[]" : word;
|
|
1167
|
-
if ((field === "msgctxt" || field === "msgid") && (draft.msgstr !== null || draft.plural)) finish();
|
|
1635
|
+
if ((field === "msgctxt" || field === "msgid") && (this.#draft.msgstr !== null || this.#draft.plural)) this.#finish();
|
|
1636
|
+
const draft = this.#draft;
|
|
1168
1637
|
if (draft.firstLine === 0) draft.firstLine = lineNo;
|
|
1169
1638
|
const value = readString(line, word.length);
|
|
1170
1639
|
draft.field = field;
|
|
1171
1640
|
if (value === null) {
|
|
1172
|
-
fail(lineNo, `malformed ${word} string`);
|
|
1173
|
-
|
|
1641
|
+
this.#fail(lineNo, `malformed ${word} string`);
|
|
1642
|
+
return;
|
|
1174
1643
|
}
|
|
1644
|
+
this.#setField(field, word, value, lineNo);
|
|
1645
|
+
}
|
|
1646
|
+
#setField(field, word, value, lineNo) {
|
|
1647
|
+
const draft = this.#draft;
|
|
1175
1648
|
switch (field) {
|
|
1176
1649
|
case "msgctxt":
|
|
1177
|
-
if (draft.msgctxt !== null || draft.msgid !== null) fail(lineNo, "msgctxt must come first, once");
|
|
1650
|
+
if (draft.msgctxt !== null || draft.msgid !== null) this.#fail(lineNo, "msgctxt must come first, once");
|
|
1178
1651
|
draft.msgctxt = value;
|
|
1179
1652
|
break;
|
|
1180
1653
|
case "msgid":
|
|
1181
|
-
if (draft.msgid !== null) fail(lineNo, "duplicate msgid");
|
|
1654
|
+
if (draft.msgid !== null) this.#fail(lineNo, "duplicate msgid");
|
|
1182
1655
|
draft.msgid = value;
|
|
1183
1656
|
draft.line = lineNo;
|
|
1184
1657
|
break;
|
|
@@ -1186,31 +1659,66 @@ function parsePo(text, options) {
|
|
|
1186
1659
|
draft.plural = true;
|
|
1187
1660
|
break;
|
|
1188
1661
|
case "msgstr":
|
|
1189
|
-
if (draft.msgid === null) fail(lineNo, "msgstr without msgid");
|
|
1190
|
-
if (draft.msgstr !== null) fail(lineNo, "duplicate msgstr");
|
|
1662
|
+
if (draft.msgid === null) this.#fail(lineNo, "msgstr without msgid");
|
|
1663
|
+
if (draft.msgstr !== null) this.#fail(lineNo, "duplicate msgstr");
|
|
1191
1664
|
draft.msgstr = value;
|
|
1192
1665
|
break;
|
|
1193
|
-
case "msgstr[]": if (!draft.plural) fail(lineNo, `${word} without msgid_plural`);
|
|
1666
|
+
case "msgstr[]": if (!draft.plural) this.#fail(lineNo, `${word} without msgid_plural`);
|
|
1194
1667
|
}
|
|
1195
1668
|
}
|
|
1196
|
-
|
|
1197
|
-
|
|
1669
|
+
/** Ends the draft entry: an entry, the header, or a diagnostic for what it lacks. */
|
|
1670
|
+
#finish() {
|
|
1671
|
+
const done = this.#draft;
|
|
1672
|
+
if (!hasKeywords(done)) return;
|
|
1673
|
+
this.#draft = newDraft();
|
|
1674
|
+
const problem = draftProblem(done);
|
|
1675
|
+
if (problem !== null) {
|
|
1676
|
+
this.#diagnostics.push(problem);
|
|
1677
|
+
return;
|
|
1678
|
+
}
|
|
1679
|
+
const msgid = done.msgid;
|
|
1680
|
+
const msgstr = done.msgstr;
|
|
1681
|
+
if (msgid === "" && done.msgctxt === null && this.#header === null && this.#entries.length === 0) {
|
|
1682
|
+
this.#header = readHeader(msgstr, this.#options.locale);
|
|
1683
|
+
return;
|
|
1684
|
+
}
|
|
1685
|
+
this.#entries.push(entryOf(done, msgid, msgstr, this.#options));
|
|
1686
|
+
}
|
|
1687
|
+
};
|
|
1688
|
+
/** Why a finished draft is not an entry, or null when it is one. */
|
|
1689
|
+
function draftProblem(done) {
|
|
1690
|
+
if (done.error !== null) return done.error;
|
|
1691
|
+
if (done.msgid === null) return parseError(done.firstLine, "entry without msgid");
|
|
1692
|
+
if (done.plural) return parseError(done.line, "gettext plural entries (msgid_plural) are not supported: write the plural as ICU in the source text");
|
|
1693
|
+
if (done.msgstr === null) return parseError(done.line, "msgid without msgstr");
|
|
1694
|
+
return null;
|
|
1695
|
+
}
|
|
1696
|
+
function entryOf(done, msgid, msgstr, options) {
|
|
1697
|
+
const { layout, isSourceLocale } = options;
|
|
1698
|
+
const manual = done.msgctxt !== null && isManualId(done.msgctxt);
|
|
1699
|
+
const translation = isSourceLocale ? "" : msgstr;
|
|
1700
|
+
let source;
|
|
1701
|
+
let label = null;
|
|
1702
|
+
if (layout === "source") source = msgid;
|
|
1703
|
+
else {
|
|
1704
|
+
label = msgid;
|
|
1705
|
+
source = isSourceLocale ? msgstr : done.sourceNote;
|
|
1706
|
+
}
|
|
1198
1707
|
return {
|
|
1199
|
-
|
|
1200
|
-
|
|
1201
|
-
|
|
1202
|
-
|
|
1203
|
-
|
|
1204
|
-
|
|
1205
|
-
|
|
1206
|
-
|
|
1207
|
-
|
|
1208
|
-
|
|
1209
|
-
|
|
1210
|
-
|
|
1211
|
-
|
|
1212
|
-
|
|
1213
|
-
diagnostics
|
|
1708
|
+
manualId: manual ? done.msgctxt : null,
|
|
1709
|
+
foreignContext: manual ? null : done.msgctxt,
|
|
1710
|
+
label,
|
|
1711
|
+
source,
|
|
1712
|
+
translation,
|
|
1713
|
+
state: entryStateOf(translation, done.ai),
|
|
1714
|
+
fuzzy: done.fuzzy,
|
|
1715
|
+
contexts: done.contexts,
|
|
1716
|
+
comments: done.comments,
|
|
1717
|
+
refs: sortedUnique(done.refs),
|
|
1718
|
+
maxLength: done.maxLength,
|
|
1719
|
+
flags: done.flags,
|
|
1720
|
+
line: done.line,
|
|
1721
|
+
wrapped: done.wrapped
|
|
1214
1722
|
};
|
|
1215
1723
|
}
|
|
1216
1724
|
function quoted(value) {
|
|
@@ -1229,11 +1737,8 @@ function pushNoteLines(out, prefix, value, idContext = false) {
|
|
|
1229
1737
|
function refLine(ref) {
|
|
1230
1738
|
return /\s/.test(ref) ? `#: ${FSI}${ref}${PDI}` : `#: ${ref}`;
|
|
1231
1739
|
}
|
|
1232
|
-
|
|
1233
|
-
|
|
1234
|
-
for (const context of entry.contexts) pushNoteLines(out, "#.", context, layout === "id");
|
|
1235
|
-
if (layout === "id" && !isSourceLocale && entry.source !== null) out.push(`#. source: ${escapePo(entry.source)}`);
|
|
1236
|
-
for (const ref of entry.refs) if (!LINE_BREAK.test(ref)) out.push(refLine(ref));
|
|
1740
|
+
/** `#,` with vi18n's own flags first, then `#=` for flags read from such a line. */
|
|
1741
|
+
function pushFlagLines(out, entry, isSourceLocale) {
|
|
1237
1742
|
const comma = [];
|
|
1238
1743
|
if (entry.fuzzy) comma.push("fuzzy");
|
|
1239
1744
|
if (entry.state === "ai" && !isSourceLocale) comma.push("ai");
|
|
@@ -1243,11 +1748,22 @@ function pushEntry(out, entry, layout, isSourceLocale) {
|
|
|
1243
1748
|
else (equals ??= []).push(flag.name);
|
|
1244
1749
|
if (comma.length > 0) out.push(`#, ${comma.join(", ")}`);
|
|
1245
1750
|
if (equals !== null) out.push(`#= ${equals.join(", ")}`);
|
|
1751
|
+
}
|
|
1752
|
+
function pushEntry(out, entry, layout, isSourceLocale) {
|
|
1753
|
+
for (const comment of entry.comments) pushNoteLines(out, "#", comment);
|
|
1754
|
+
for (const context of entry.contexts) pushNoteLines(out, "#.", context, layout === "id");
|
|
1755
|
+
if (layout === "id" && !isSourceLocale && entry.source !== null) out.push(`#. source: ${escapePo(entry.source)}`);
|
|
1756
|
+
for (const ref of entry.refs) if (!LINE_BREAK.test(ref)) out.push(refLine(ref));
|
|
1757
|
+
pushFlagLines(out, entry, isSourceLocale);
|
|
1246
1758
|
const context = entry.manualId ?? entry.foreignContext;
|
|
1247
1759
|
if (context !== null) out.push(`msgctxt ${quoted(context)}`);
|
|
1248
1760
|
out.push(`msgid ${quoted((layout === "id" ? entry.label : entry.source) ?? "")}`);
|
|
1249
|
-
|
|
1250
|
-
|
|
1761
|
+
out.push(`msgstr ${quoted(msgstrOf(entry, layout, isSourceLocale))}`);
|
|
1762
|
+
}
|
|
1763
|
+
/** The source locale's `"id"` layout keeps the text in msgstr; its `"source"` layout has it in msgid. */
|
|
1764
|
+
function msgstrOf(entry, layout, isSourceLocale) {
|
|
1765
|
+
if (!isSourceLocale) return entry.translation;
|
|
1766
|
+
return layout === "id" ? entry.source ?? "" : "";
|
|
1251
1767
|
}
|
|
1252
1768
|
/**
|
|
1253
1769
|
* The `Plural-Forms` value to write: one a tool wrote (kept, so vi18n and a
|
|
@@ -1285,8 +1801,7 @@ function serializePo(catalog, options) {
|
|
|
1285
1801
|
for (const block of catalog.obsolete) out.push("", block);
|
|
1286
1802
|
return `${out.join("\n")}\n`;
|
|
1287
1803
|
}
|
|
1288
|
-
|
|
1289
|
-
//#region src/core/catalog/io.ts
|
|
1804
|
+
// src/core/catalog/io.ts
|
|
1290
1805
|
/**
|
|
1291
1806
|
* Catalog files (INTERNALS §2.6.1, §2.6.6, PLAN §5.6): one per locale,
|
|
1292
1807
|
* source locale included. Every writer (dev, extract, translate, import,
|
|
@@ -1337,11 +1852,6 @@ function withoutGenerator(text) {
|
|
|
1337
1852
|
function sameCatalogText(a, b) {
|
|
1338
1853
|
return a === b || withoutGenerator(a) === withoutGenerator(b);
|
|
1339
1854
|
}
|
|
1340
|
-
function displayPath$1(root, file) {
|
|
1341
|
-
const rel = relative(root, file);
|
|
1342
|
-
if (rel === "" || rel.startsWith("..") || isAbsolute(rel)) return file.split(sep).join("/");
|
|
1343
|
-
return rel.split(sep).join("/");
|
|
1344
|
-
}
|
|
1345
1855
|
function isMissing(error) {
|
|
1346
1856
|
return error?.code === "ENOENT";
|
|
1347
1857
|
}
|
|
@@ -1399,35 +1909,28 @@ function firstInvalidLine(bytes) {
|
|
|
1399
1909
|
*/
|
|
1400
1910
|
function decodeCatalog(bytes, layout) {
|
|
1401
1911
|
const charset = layout === "json" ? null : poCharset(bytes);
|
|
1402
|
-
const
|
|
1912
|
+
const undecodable = (message, line) => ({
|
|
1913
|
+
text: Buffer.from(bytes.buffer, bytes.byteOffset, bytes.length).toString("utf8"),
|
|
1914
|
+
diagnostic: {
|
|
1915
|
+
code: "catalog-parse",
|
|
1916
|
+
severity: "error",
|
|
1917
|
+
message,
|
|
1918
|
+
...line === void 0 ? {} : { line }
|
|
1919
|
+
}
|
|
1920
|
+
});
|
|
1403
1921
|
if (charset === null || UTF8_LABELS.has(charset.toLowerCase())) try {
|
|
1404
1922
|
return {
|
|
1405
1923
|
text: UTF8.decode(bytes),
|
|
1406
1924
|
diagnostic: null
|
|
1407
1925
|
};
|
|
1408
1926
|
} catch {
|
|
1409
|
-
return
|
|
1410
|
-
text: lossy(),
|
|
1411
|
-
diagnostic: {
|
|
1412
|
-
code: "catalog-parse",
|
|
1413
|
-
severity: "error",
|
|
1414
|
-
message: "the file is not valid UTF-8; re-save it as UTF-8",
|
|
1415
|
-
line: firstInvalidLine(bytes)
|
|
1416
|
-
}
|
|
1417
|
-
};
|
|
1927
|
+
return undecodable("the file is not valid UTF-8; re-save it as UTF-8", firstInvalidLine(bytes));
|
|
1418
1928
|
}
|
|
1419
1929
|
let decoder;
|
|
1420
1930
|
try {
|
|
1421
1931
|
decoder = new TextDecoder(charset, { fatal: true });
|
|
1422
1932
|
} catch {
|
|
1423
|
-
return {
|
|
1424
|
-
text: lossy(),
|
|
1425
|
-
diagnostic: {
|
|
1426
|
-
code: "catalog-parse",
|
|
1427
|
-
severity: "error",
|
|
1428
|
-
message: `unsupported charset ${charset} in Content-Type; convert the file to UTF-8`
|
|
1429
|
-
}
|
|
1430
|
-
};
|
|
1933
|
+
return undecodable(`unsupported charset ${charset} in Content-Type; convert the file to UTF-8`);
|
|
1431
1934
|
}
|
|
1432
1935
|
try {
|
|
1433
1936
|
return {
|
|
@@ -1435,18 +1938,17 @@ function decodeCatalog(bytes, layout) {
|
|
|
1435
1938
|
diagnostic: null
|
|
1436
1939
|
};
|
|
1437
1940
|
} catch {
|
|
1438
|
-
return {
|
|
1439
|
-
text: lossy(),
|
|
1440
|
-
diagnostic: {
|
|
1441
|
-
code: "catalog-parse",
|
|
1442
|
-
severity: "error",
|
|
1443
|
-
message: `the file is not valid ${charset} as its Content-Type declares; fix its encoding`
|
|
1444
|
-
}
|
|
1445
|
-
};
|
|
1941
|
+
return undecodable(`the file is not valid ${charset} as its Content-Type declares; fix its encoding`);
|
|
1446
1942
|
}
|
|
1447
1943
|
}
|
|
1944
|
+
const SEVERITY = {
|
|
1945
|
+
off: null,
|
|
1946
|
+
warn: "warning",
|
|
1947
|
+
error: "error"
|
|
1948
|
+
};
|
|
1949
|
+
/** The severity a `strict` level reports at; null for `"off"`, which reports nothing. */
|
|
1448
1950
|
function severityOf(level) {
|
|
1449
|
-
return level
|
|
1951
|
+
return SEVERITY[level];
|
|
1450
1952
|
}
|
|
1451
1953
|
/**
|
|
1452
1954
|
* Sources that do not parse leave their entry without a key (INTERNALS
|
|
@@ -1508,7 +2010,7 @@ async function loadCatalogSet(config, codec, locales) {
|
|
|
1508
2010
|
const diagnostics = read.diagnostics;
|
|
1509
2011
|
if (decoded.diagnostic !== null) diagnostics.unshift(decoded.diagnostic);
|
|
1510
2012
|
if (invalid !== null) diagnostics.push(...invalidSources(read.catalog, codec, isSourceLocale, invalid));
|
|
1511
|
-
const file = displayPath
|
|
2013
|
+
const file = displayPath(config.root, path);
|
|
1512
2014
|
for (const diagnostic of diagnostics) {
|
|
1513
2015
|
diagnostic.file ??= file;
|
|
1514
2016
|
diagnostic.locale ??= locale;
|
|
@@ -1571,14 +2073,7 @@ const queues = /* @__PURE__ */ new Map();
|
|
|
1571
2073
|
* already differed from `expectedHash` on entry.
|
|
1572
2074
|
*/
|
|
1573
2075
|
function updateCatalogFile(path, expectedHash, update, options) {
|
|
1574
|
-
|
|
1575
|
-
const run = (queues.get(key) ?? Promise.resolve()).then(() => writeCatalogFile(path, expectedHash, update, options), () => writeCatalogFile(path, expectedHash, update, options));
|
|
1576
|
-
const settled = run.then(() => void 0, () => void 0);
|
|
1577
|
-
queues.set(key, settled);
|
|
1578
|
-
settled.then(() => {
|
|
1579
|
-
if (queues.get(key) === settled) queues.delete(key);
|
|
1580
|
-
});
|
|
1581
|
-
return run;
|
|
2076
|
+
return serialized(queues, resolve(path), () => writeCatalogFile(path, expectedHash, update, options));
|
|
1582
2077
|
}
|
|
1583
2078
|
async function writeCatalogFile(path, expectedHash, update, options) {
|
|
1584
2079
|
const readOptions = {
|
|
@@ -1611,9 +2106,9 @@ async function writeCatalogFile(path, expectedHash, update, options) {
|
|
|
1611
2106
|
};
|
|
1612
2107
|
if (bytes === null) await mkdir(directory, { recursive: true });
|
|
1613
2108
|
const temp = join(directory, `.${basename(path)}.${process.pid}.${randomBytes(6).toString("hex")}.tmp`);
|
|
1614
|
-
await writeFile(temp, output);
|
|
1615
2109
|
let renamed = false;
|
|
1616
2110
|
try {
|
|
2111
|
+
await writeFile(temp, output);
|
|
1617
2112
|
const now = await readOptional(path);
|
|
1618
2113
|
const nowHash = now === null ? null : contentHash(now);
|
|
1619
2114
|
if (nowHash === currentHash) {
|
|
@@ -1639,8 +2134,7 @@ async function writeCatalogFile(path, expectedHash, update, options) {
|
|
|
1639
2134
|
});
|
|
1640
2135
|
}
|
|
1641
2136
|
}
|
|
1642
|
-
|
|
1643
|
-
//#region src/core/catalog/library.ts
|
|
2137
|
+
// src/core/catalog/library.ts
|
|
1644
2138
|
/**
|
|
1645
2139
|
* Library mode, the app's side (PLAN §4.7, §5.24; INTERNALS §2.6.11): what an
|
|
1646
2140
|
* opted-in package ships next to its code. Its `vi18n.messages.json` (written
|
|
@@ -1674,42 +2168,61 @@ function libraryManifests(config) {
|
|
|
1674
2168
|
}
|
|
1675
2169
|
function readManifest(file, root) {
|
|
1676
2170
|
const shown = displayPath(root, file);
|
|
1677
|
-
|
|
1678
|
-
try {
|
|
1679
|
-
data = JSON.parse(readFileSync(file, "utf8"));
|
|
1680
|
-
} catch (error) {
|
|
1681
|
-
return {
|
|
1682
|
-
manifest: null,
|
|
1683
|
-
diagnostic: {
|
|
1684
|
-
code: "catalog-parse",
|
|
1685
|
-
severity: "warning",
|
|
1686
|
-
message: `${shown} cannot be read (${error.message}); the package's translations are left out`,
|
|
1687
|
-
file: shown
|
|
1688
|
-
}
|
|
1689
|
-
};
|
|
1690
|
-
}
|
|
1691
|
-
const manifest = data;
|
|
1692
|
-
if (manifest === null || typeof manifest !== "object" || manifest.vi18n !== 1 || !Array.isArray(manifest.messages)) return {
|
|
2171
|
+
const unusable = (problem) => ({
|
|
1693
2172
|
manifest: null,
|
|
1694
2173
|
diagnostic: {
|
|
1695
2174
|
code: "catalog-parse",
|
|
1696
2175
|
severity: "warning",
|
|
1697
|
-
message: `${shown}
|
|
2176
|
+
message: `${shown} ${problem}; the package's translations are left out`,
|
|
1698
2177
|
file: shown
|
|
1699
2178
|
}
|
|
1700
|
-
};
|
|
2179
|
+
});
|
|
2180
|
+
let data;
|
|
2181
|
+
try {
|
|
2182
|
+
data = JSON.parse(readFileSync(file, "utf8"));
|
|
2183
|
+
} catch (error) {
|
|
2184
|
+
return unusable(`cannot be read (${error instanceof Error ? error.message : String(error)})`);
|
|
2185
|
+
}
|
|
2186
|
+
if (!isRecord(data) || data.vi18n !== 1 || !Array.isArray(data.messages)) return unusable("is not a vi18n.messages.json of version 1");
|
|
1701
2187
|
return {
|
|
1702
|
-
manifest,
|
|
2188
|
+
manifest: usableManifest(data, data.messages),
|
|
1703
2189
|
diagnostic: null
|
|
1704
2190
|
};
|
|
1705
2191
|
}
|
|
2192
|
+
function isRecord(value) {
|
|
2193
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
2194
|
+
}
|
|
2195
|
+
function isMessage(value) {
|
|
2196
|
+
return isRecord(value) && typeof value.message === "string" && (value.id === void 0 || typeof value.id === "string");
|
|
2197
|
+
}
|
|
2198
|
+
/**
|
|
2199
|
+
* The parts of a version 1 manifest the app can use, so later code can trust
|
|
2200
|
+
* the type: a malformed message is skipped, a malformed catalog list counts as
|
|
2201
|
+
* none, and a missing `sourceLocale` as the app's own. One bad field never
|
|
2202
|
+
* drops the rest of the package.
|
|
2203
|
+
*/
|
|
2204
|
+
function usableManifest(data, messages) {
|
|
2205
|
+
return {
|
|
2206
|
+
sourceLocale: typeof data.sourceLocale === "string" ? data.sourceLocale : null,
|
|
2207
|
+
catalogs: usableCatalogs(data.catalogs),
|
|
2208
|
+
messages: messages.filter(isMessage)
|
|
2209
|
+
};
|
|
2210
|
+
}
|
|
2211
|
+
function usableCatalogs(value) {
|
|
2212
|
+
if (!isRecord(value) || !isRecord(value.files)) return null;
|
|
2213
|
+
return {
|
|
2214
|
+
format: value.format === "json" ? "json" : "po",
|
|
2215
|
+
msgid: value.msgid === "id" ? "id" : "source",
|
|
2216
|
+
files: value.files
|
|
2217
|
+
};
|
|
2218
|
+
}
|
|
1706
2219
|
/**
|
|
1707
2220
|
* The manual-id messages of every opted-in package, first package first:
|
|
1708
2221
|
* what `msg({ id })` may name without a message (`vi18n.gen.ts` types them).
|
|
1709
2222
|
*/
|
|
1710
2223
|
function libraryIds(config) {
|
|
1711
2224
|
const out = /* @__PURE__ */ new Map();
|
|
1712
|
-
for (const { manifest } of libraryManifests(config)) for (const message of manifest?.messages ?? []) if (
|
|
2225
|
+
for (const { manifest } of libraryManifests(config)) for (const message of manifest?.messages ?? []) if (message.id !== void 0 && !out.has(message.id)) out.set(message.id, message.message);
|
|
1713
2226
|
return out;
|
|
1714
2227
|
}
|
|
1715
2228
|
/**
|
|
@@ -1722,69 +2235,86 @@ function libraryIds(config) {
|
|
|
1722
2235
|
async function attachLibraryCatalogs(set, config, codec, locales) {
|
|
1723
2236
|
const diagnostics = [];
|
|
1724
2237
|
if (config.extraction.dependencies.length === 0) return diagnostics;
|
|
1725
|
-
const layout = catalogLayout(config);
|
|
1726
2238
|
for (const loaded of libraryManifests(config)) {
|
|
1727
2239
|
if (loaded.diagnostic !== null) diagnostics.push(loaded.diagnostic);
|
|
1728
|
-
const
|
|
1729
|
-
if (
|
|
1730
|
-
const
|
|
1731
|
-
|
|
2240
|
+
const manifest = loaded.manifest;
|
|
2241
|
+
if (manifest === null || manifest.catalogs === null) continue;
|
|
2242
|
+
const library = {
|
|
2243
|
+
name: loaded.name,
|
|
2244
|
+
file: loaded.file,
|
|
2245
|
+
manifest,
|
|
2246
|
+
catalogs: manifest.catalogs
|
|
2247
|
+
};
|
|
1732
2248
|
for (const locale of locales) {
|
|
1733
|
-
if (locale === config.sourceLocale && !foreign) continue;
|
|
1734
2249
|
const own = set.get(locale);
|
|
1735
|
-
const
|
|
1736
|
-
|
|
1737
|
-
|
|
1738
|
-
|
|
1739
|
-
severity: "warning",
|
|
1740
|
-
message: `${loaded.name} is written in ${loaded.manifest?.sourceLocale} and ships no ${locale} catalog; its messages show in ${loaded.manifest?.sourceLocale} on ${locale} pages`,
|
|
1741
|
-
file: displayPath(config.root, loaded.file),
|
|
1742
|
-
locale
|
|
1743
|
-
});
|
|
1744
|
-
continue;
|
|
1745
|
-
}
|
|
1746
|
-
if (own === void 0 || typeof relativePath !== "string") continue;
|
|
1747
|
-
const path = resolve(dirname(loaded.file), relativePath);
|
|
1748
|
-
let text;
|
|
1749
|
-
try {
|
|
1750
|
-
text = await readFile(path, "utf8");
|
|
1751
|
-
} catch {
|
|
1752
|
-
diagnostics.push({
|
|
1753
|
-
code: "catalog-parse",
|
|
1754
|
-
severity: "warning",
|
|
1755
|
-
message: `${loaded.name} lists a ${locale} catalog it does not ship (${relativePath}); its ${locale} translations are left out`,
|
|
1756
|
-
file: displayPath(config.root, loaded.file),
|
|
1757
|
-
locale
|
|
1758
|
-
});
|
|
1759
|
-
continue;
|
|
1760
|
-
}
|
|
1761
|
-
const read = parseCatalog(text, {
|
|
1762
|
-
locale,
|
|
1763
|
-
layout: from,
|
|
1764
|
-
isSourceLocale: false
|
|
2250
|
+
const attached = await libraryCatalog(library, locale, own, {
|
|
2251
|
+
config,
|
|
2252
|
+
codec,
|
|
2253
|
+
diagnostics
|
|
1765
2254
|
});
|
|
1766
|
-
|
|
1767
|
-
...diagnostic,
|
|
1768
|
-
severity: diagnostic.severity === "error" ? "warning" : diagnostic.severity,
|
|
1769
|
-
file: diagnostic.file ?? displayPath(config.root, path),
|
|
1770
|
-
locale
|
|
1771
|
-
});
|
|
1772
|
-
const library = {
|
|
1773
|
-
locale,
|
|
1774
|
-
path,
|
|
1775
|
-
catalog: joined(read.catalog, layout, codec),
|
|
1776
|
-
hash: contentHash(text),
|
|
1777
|
-
library: true,
|
|
1778
|
-
...foreign ? { librarySource: loaded.manifest?.sourceLocale } : {}
|
|
1779
|
-
};
|
|
1780
|
-
set.set(locale, {
|
|
2255
|
+
if (own !== void 0 && attached !== null) set.set(locale, {
|
|
1781
2256
|
...own,
|
|
1782
|
-
libraries: [...own.libraries ?? [],
|
|
2257
|
+
libraries: [...own.libraries ?? [], attached]
|
|
1783
2258
|
});
|
|
1784
2259
|
}
|
|
1785
2260
|
}
|
|
1786
2261
|
return diagnostics;
|
|
1787
2262
|
}
|
|
2263
|
+
/** One package's catalog for `locale`, read and joined; null (with a warning when it should exist) otherwise. */
|
|
2264
|
+
async function libraryCatalog(library, locale, own, app) {
|
|
2265
|
+
const { config, codec, diagnostics } = app;
|
|
2266
|
+
const { name, manifest, catalogs } = library;
|
|
2267
|
+
const librarySource = manifest.sourceLocale ?? config.sourceLocale;
|
|
2268
|
+
const foreign = librarySource !== config.sourceLocale;
|
|
2269
|
+
if (locale === config.sourceLocale && !foreign) return null;
|
|
2270
|
+
const relativePath = Object.hasOwn(catalogs.files, locale) ? catalogs.files[locale] : void 0;
|
|
2271
|
+
const shown = displayPath(config.root, library.file);
|
|
2272
|
+
if (locale === config.sourceLocale && typeof relativePath !== "string") {
|
|
2273
|
+
diagnostics.push({
|
|
2274
|
+
code: "catalog-parse",
|
|
2275
|
+
severity: "warning",
|
|
2276
|
+
message: `${name} is written in ${librarySource} and ships no ${locale} catalog; its messages show in ${librarySource} on ${locale} pages`,
|
|
2277
|
+
file: shown,
|
|
2278
|
+
locale
|
|
2279
|
+
});
|
|
2280
|
+
return null;
|
|
2281
|
+
}
|
|
2282
|
+
if (own === void 0 || typeof relativePath !== "string") return null;
|
|
2283
|
+
const path = resolve(dirname(library.file), relativePath);
|
|
2284
|
+
let text;
|
|
2285
|
+
try {
|
|
2286
|
+
text = await readFile(path, "utf8");
|
|
2287
|
+
} catch {
|
|
2288
|
+
diagnostics.push({
|
|
2289
|
+
code: "catalog-parse",
|
|
2290
|
+
severity: "warning",
|
|
2291
|
+
message: `${name} lists a ${locale} catalog it does not ship (${relativePath}); its ${locale} translations are left out`,
|
|
2292
|
+
file: shown,
|
|
2293
|
+
locale
|
|
2294
|
+
});
|
|
2295
|
+
return null;
|
|
2296
|
+
}
|
|
2297
|
+
const from = catalogLayout({ catalogs });
|
|
2298
|
+
const read = parseCatalog(text, {
|
|
2299
|
+
locale,
|
|
2300
|
+
layout: from,
|
|
2301
|
+
isSourceLocale: false
|
|
2302
|
+
});
|
|
2303
|
+
for (const diagnostic of read.diagnostics) diagnostics.push({
|
|
2304
|
+
...diagnostic,
|
|
2305
|
+
severity: diagnostic.severity === "error" ? "warning" : diagnostic.severity,
|
|
2306
|
+
file: diagnostic.file ?? displayPath(config.root, path),
|
|
2307
|
+
locale
|
|
2308
|
+
});
|
|
2309
|
+
return {
|
|
2310
|
+
locale,
|
|
2311
|
+
path,
|
|
2312
|
+
catalog: joined(read.catalog, catalogLayout(config), codec),
|
|
2313
|
+
hash: contentHash(text),
|
|
2314
|
+
library: true,
|
|
2315
|
+
...foreign ? { librarySource } : {}
|
|
2316
|
+
};
|
|
2317
|
+
}
|
|
1788
2318
|
/**
|
|
1789
2319
|
* A library catalog in the app's terms: the app's layout, no recorded
|
|
1790
2320
|
* scheme, and every entry's `label` computed from its source by the app's
|
|
@@ -1817,11 +2347,6 @@ function joined(catalog, layout, codec) {
|
|
|
1817
2347
|
droppedObsolete: 0
|
|
1818
2348
|
};
|
|
1819
2349
|
}
|
|
1820
|
-
function displayPath(root, file) {
|
|
1821
|
-
const rel = relative(root, file);
|
|
1822
|
-
if (rel === "" || rel.startsWith("..") || isAbsolute(rel)) return file.split(sep).join("/");
|
|
1823
|
-
return rel.split(sep).join("/");
|
|
1824
|
-
}
|
|
1825
2350
|
function libraryCoverage(set, messages, config, codec) {
|
|
1826
2351
|
const layout = catalogLayout(config);
|
|
1827
2352
|
const locales = /* @__PURE__ */ new Map();
|
|
@@ -1878,5 +2403,4 @@ function libraryTranslation(loaded, message, layout, codec) {
|
|
|
1878
2403
|
}
|
|
1879
2404
|
return null;
|
|
1880
2405
|
}
|
|
1881
|
-
|
|
1882
|
-
export { usableTranslation as C, dependencyRef as D, dependencyOf as E, listDependencyFiles as O, entryKey as S, dependenciesOf as T, parseJsonCatalog as _, ownedMessages as a, emptyCatalog as b, contentHash as c, parseCatalogBytes as d, sameCatalogText as f, serializePo as g, parsePo as h, libraryTranslation as i, loadCatalogSet as l, updateCatalogFile as m, libraryCoverage as n, catalogLayout as o, serializeCatalog as p, libraryIds as r, catalogPath as s, MESSAGES_FILE as t, parseCatalog as u, serializeJsonCatalog as v, sourceMatches as w, entryCanonical as x, createEntry as y };
|
|
2406
|
+
export { usableTranslation as A, emptyCatalog as C, flagKey as D, entryKey as E, isIncluded as F, listProjectFiles as I, compareText$1 as L, sourceMatches as M, serialized as N, stricterLimit as O, displayPath as P, dependenciesOf as R, defineFields as S, entryCanonical as T, parseJsonCatalog as _, ownedMessages as a, compareText as b, contentHash as c, parseCatalogBytes as d, sameCatalogText as f, parsePo as g, updateCatalogFile as h, libraryTranslation as i, withSubject as j, translationErrors as k, loadCatalogSet as l, severityOf as m, libraryCoverage as n, catalogLayout as o, serializeCatalog as p, libraryIds as r, catalogPath as s, MESSAGES_FILE as t, parseCatalog as u, STATE_RANK as v, emptyHeader as w, createEntry as x, catalogIdentity as y, dependencyRef as z };
|