@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,7 +1,7 @@
|
|
|
1
|
-
import { _ as parseJsonCatalog, g as
|
|
2
|
-
import { t as VERSION } from "./version-
|
|
3
|
-
import {
|
|
4
|
-
|
|
1
|
+
import { _ as parseJsonCatalog, g as parsePo, p as serializeCatalog, w as emptyHeader, x as createEntry } from "./library-BfSOBeBd.js";
|
|
2
|
+
import { t as VERSION } from "./version-BFs370Ce.js";
|
|
3
|
+
import { i as exchangeKey, o as parseExchangeKey } from "./units-VVLgQ7x7.js";
|
|
4
|
+
// src/core/exchange/csv.ts
|
|
5
5
|
/**
|
|
6
6
|
* RFC 4180 CSV for `vi18n export --format csv` (PLAN §4.4 P2). The writer
|
|
7
7
|
* starts with a UTF-8 byte order mark and ends rows with CRLF, which is what
|
|
@@ -28,6 +28,36 @@ var CsvError = class extends Error {
|
|
|
28
28
|
this.line = line;
|
|
29
29
|
}
|
|
30
30
|
};
|
|
31
|
+
/**
|
|
32
|
+
* A quoted field whose opening quote is at `start`: its value, the index
|
|
33
|
+
* just past the closing quote, and the line it ends on.
|
|
34
|
+
*/
|
|
35
|
+
function readQuoted(text, start, line) {
|
|
36
|
+
let value = "";
|
|
37
|
+
let i = start + 1;
|
|
38
|
+
while (i < text.length) {
|
|
39
|
+
const ch = text[i];
|
|
40
|
+
if (ch !== "\"") {
|
|
41
|
+
if (ch === "\n") line++;
|
|
42
|
+
value += ch;
|
|
43
|
+
i++;
|
|
44
|
+
continue;
|
|
45
|
+
}
|
|
46
|
+
if (text[i + 1] === "\"") {
|
|
47
|
+
value += "\"";
|
|
48
|
+
i += 2;
|
|
49
|
+
continue;
|
|
50
|
+
}
|
|
51
|
+
const next = text[i + 1];
|
|
52
|
+
if (next !== void 0 && next !== "," && next !== "\n" && next !== "\r") throw new CsvError("text after a closing quote; double a quote inside a quoted field", line);
|
|
53
|
+
return {
|
|
54
|
+
value,
|
|
55
|
+
end: i + 1,
|
|
56
|
+
line
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
throw new CsvError("a quoted field is never closed", line);
|
|
60
|
+
}
|
|
31
61
|
/** Rows with the 1-based line each starts on; a quoted field may span lines. */
|
|
32
62
|
function parseCsv(input) {
|
|
33
63
|
const text = input.charCodeAt(0) === 65279 ? input.slice(1) : input;
|
|
@@ -37,7 +67,6 @@ function parseCsv(input) {
|
|
|
37
67
|
let line = 1;
|
|
38
68
|
let rowLine = 1;
|
|
39
69
|
let i = 0;
|
|
40
|
-
let quoted = false;
|
|
41
70
|
let started = false;
|
|
42
71
|
const endRow = () => {
|
|
43
72
|
cells.push(unguard(cell));
|
|
@@ -51,31 +80,15 @@ function parseCsv(input) {
|
|
|
51
80
|
};
|
|
52
81
|
while (i < text.length) {
|
|
53
82
|
const ch = text[i];
|
|
54
|
-
if (quoted) {
|
|
55
|
-
if (ch === "\"") {
|
|
56
|
-
if (text[i + 1] === "\"") {
|
|
57
|
-
cell += "\"";
|
|
58
|
-
i += 2;
|
|
59
|
-
continue;
|
|
60
|
-
}
|
|
61
|
-
quoted = false;
|
|
62
|
-
i++;
|
|
63
|
-
const next = text[i];
|
|
64
|
-
if (next !== void 0 && next !== "," && next !== "\n" && next !== "\r") throw new CsvError("text after a closing quote; double a quote inside a quoted field", line);
|
|
65
|
-
continue;
|
|
66
|
-
}
|
|
67
|
-
if (ch === "\n") line++;
|
|
68
|
-
cell += ch;
|
|
69
|
-
i++;
|
|
70
|
-
continue;
|
|
71
|
-
}
|
|
72
83
|
if (!started) {
|
|
73
84
|
rowLine = line;
|
|
74
85
|
started = true;
|
|
75
86
|
}
|
|
76
87
|
if (ch === "\"" && cell === "") {
|
|
77
|
-
quoted =
|
|
78
|
-
|
|
88
|
+
const quoted = readQuoted(text, i, line);
|
|
89
|
+
cell = quoted.value;
|
|
90
|
+
i = quoted.end;
|
|
91
|
+
line = quoted.line;
|
|
79
92
|
} else if (ch === ",") {
|
|
80
93
|
cells.push(unguard(cell));
|
|
81
94
|
cell = "";
|
|
@@ -89,12 +102,22 @@ function parseCsv(input) {
|
|
|
89
102
|
i++;
|
|
90
103
|
}
|
|
91
104
|
}
|
|
92
|
-
if (quoted) throw new CsvError("a quoted field is never closed", line);
|
|
93
105
|
if (started || cell !== "" || cells.length > 0) endRow();
|
|
94
106
|
return rows;
|
|
95
107
|
}
|
|
96
|
-
|
|
97
|
-
|
|
108
|
+
// src/core/exchange/records.ts
|
|
109
|
+
/** The result of a file nothing can be imported from. */
|
|
110
|
+
function failed(message, line = 0) {
|
|
111
|
+
return {
|
|
112
|
+
locale: null,
|
|
113
|
+
records: [],
|
|
114
|
+
error: {
|
|
115
|
+
message,
|
|
116
|
+
line
|
|
117
|
+
}
|
|
118
|
+
};
|
|
119
|
+
}
|
|
120
|
+
// src/core/exchange/xml.ts
|
|
98
121
|
var XmlError = class extends Error {
|
|
99
122
|
line;
|
|
100
123
|
constructor(message, line) {
|
|
@@ -116,9 +139,14 @@ const PREDEFINED = {
|
|
|
116
139
|
function isXmlChar(code) {
|
|
117
140
|
return code === 9 || code === 10 || code === 13 || code >= 32 && code <= 55295 || code >= 57344 && code <= 65533 || code >= 65536 && code <= 1114111;
|
|
118
141
|
}
|
|
142
|
+
const TEXT_ESCAPES = {
|
|
143
|
+
"&": "&",
|
|
144
|
+
"<": "<",
|
|
145
|
+
">": ">"
|
|
146
|
+
};
|
|
119
147
|
/** Text content: `&`, `<` and `>` (the last so `]]>` can never appear). */
|
|
120
148
|
function escapeText(value) {
|
|
121
|
-
return value.replace(/[&<>]/g, (ch) => ch
|
|
149
|
+
return value.replace(/[&<>]/g, (ch) => TEXT_ESCAPES[ch]);
|
|
122
150
|
}
|
|
123
151
|
/** Attribute values in double quotes; whitespace as references, since readers normalise it to spaces. */
|
|
124
152
|
function escapeAttribute(value) {
|
|
@@ -134,66 +162,149 @@ function escapeAttribute(value) {
|
|
|
134
162
|
}
|
|
135
163
|
function parseXml(input) {
|
|
136
164
|
const text = (input.charCodeAt(0) === 65279 ? input.slice(1) : input).replace(/\r\n?/g, "\n");
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
165
|
+
const reader = new XmlReader(text);
|
|
166
|
+
reader.skipMisc();
|
|
167
|
+
if (text[reader.i] !== "<") reader.fail("expected the root element");
|
|
168
|
+
const root = reader.element([/* @__PURE__ */ new Map([["", ""]])], 0);
|
|
169
|
+
reader.skipMisc();
|
|
170
|
+
if (reader.i < text.length) reader.fail("content after the root element");
|
|
171
|
+
return root;
|
|
172
|
+
}
|
|
173
|
+
/** The value of a character reference (`#x41`, `#65`), NaN when it is spelled wrong. */
|
|
174
|
+
function characterReference(name) {
|
|
175
|
+
if (/^#x[0-9a-fA-F]+$/.test(name)) return Number.parseInt(name.slice(2), 16);
|
|
176
|
+
if (/^#\d+$/.test(name)) return Number(name.slice(1));
|
|
177
|
+
return NaN;
|
|
178
|
+
}
|
|
179
|
+
/** A recursive-descent reader over the text, with the position and its line. */
|
|
180
|
+
var XmlReader = class {
|
|
181
|
+
#text;
|
|
182
|
+
i = 0;
|
|
183
|
+
#line = 1;
|
|
184
|
+
constructor(text) {
|
|
185
|
+
this.#text = text;
|
|
186
|
+
}
|
|
187
|
+
fail(message) {
|
|
188
|
+
throw new XmlError(message, this.#line);
|
|
189
|
+
}
|
|
190
|
+
/** Comments, processing instructions and white space between markup. */
|
|
191
|
+
skipMisc() {
|
|
192
|
+
for (;;) {
|
|
193
|
+
this.#skipSpace();
|
|
194
|
+
if (this.#at("<!--")) this.#skipComment();
|
|
195
|
+
else if (this.#at("<?")) this.#skipProcessingInstruction();
|
|
196
|
+
else if (this.#at("<!DOCTYPE")) this.fail("a DOCTYPE; XLIFF needs none, and vi18n does not read entity declarations");
|
|
197
|
+
else return;
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
element(scopes, depth) {
|
|
201
|
+
if (depth > MAX_DEPTH) this.fail(`elements nested more than ${MAX_DEPTH} deep`);
|
|
202
|
+
const start = this.#line;
|
|
203
|
+
this.#expect("<");
|
|
204
|
+
const name = this.#readName();
|
|
205
|
+
const attrs = this.#readAttributes();
|
|
206
|
+
const declared = /* @__PURE__ */ new Map();
|
|
207
|
+
for (const [attr, value] of attrs) if (attr === "xmlns") declared.set("", value);
|
|
208
|
+
else if (attr.startsWith("xmlns:")) declared.set(attr.slice(6), value);
|
|
209
|
+
const inner = declared.size > 0 ? [...scopes, declared] : scopes;
|
|
210
|
+
const { ns, local } = this.#resolve(name, inner, false);
|
|
211
|
+
for (const attr of attrs.keys()) if (attr !== "xmlns" && !attr.startsWith("xmlns:")) this.#resolve(attr, inner, true);
|
|
212
|
+
const node = {
|
|
213
|
+
kind: "element",
|
|
214
|
+
name,
|
|
215
|
+
ns,
|
|
216
|
+
local,
|
|
217
|
+
attrs,
|
|
218
|
+
children: [],
|
|
219
|
+
line: start
|
|
220
|
+
};
|
|
221
|
+
if (this.#at("/>")) {
|
|
222
|
+
this.#advance(this.i + 2);
|
|
223
|
+
return node;
|
|
224
|
+
}
|
|
225
|
+
this.#advance(this.i + 1);
|
|
226
|
+
this.#readContent(node, inner, depth);
|
|
227
|
+
return node;
|
|
228
|
+
}
|
|
229
|
+
#at(literal) {
|
|
230
|
+
return this.#text.startsWith(literal, this.i);
|
|
231
|
+
}
|
|
232
|
+
#advance(to) {
|
|
233
|
+
const text = this.#text;
|
|
234
|
+
for (let k = this.i; k < to; k++) if (text.charCodeAt(k) === 10) this.#line++;
|
|
235
|
+
this.i = to;
|
|
236
|
+
}
|
|
237
|
+
#expect(literal) {
|
|
238
|
+
if (!this.#at(literal)) this.fail(`expected ${JSON.stringify(literal)}`);
|
|
239
|
+
this.#advance(this.i + literal.length);
|
|
240
|
+
}
|
|
241
|
+
#skipSpace() {
|
|
242
|
+
const text = this.#text;
|
|
243
|
+
let k = this.i;
|
|
152
244
|
while (k < text.length && /[ \t\n]/.test(text[k])) k++;
|
|
153
|
-
advance(k);
|
|
154
|
-
}
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
const
|
|
158
|
-
if (
|
|
159
|
-
advance(
|
|
245
|
+
this.#advance(k);
|
|
246
|
+
}
|
|
247
|
+
/** Index just past `close`, searched from `from`; fails with `unterminated` when it never comes. */
|
|
248
|
+
#skipPast(from, close, unterminated) {
|
|
249
|
+
const end = this.#text.indexOf(close, from);
|
|
250
|
+
if (end === -1) this.fail(unterminated);
|
|
251
|
+
this.#advance(end + close.length);
|
|
252
|
+
}
|
|
253
|
+
#skipComment() {
|
|
254
|
+
this.#skipPast(this.i + 4, "-->", "an unterminated comment");
|
|
255
|
+
}
|
|
256
|
+
#skipProcessingInstruction() {
|
|
257
|
+
this.#skipPast(this.i + 2, "?>", "an unterminated processing instruction");
|
|
258
|
+
}
|
|
259
|
+
#readName() {
|
|
260
|
+
NAME.lastIndex = this.i;
|
|
261
|
+
const match = NAME.exec(this.#text);
|
|
262
|
+
if (match === null) return this.fail("expected a name");
|
|
263
|
+
this.#advance(this.i + match[0].length);
|
|
160
264
|
return match[0];
|
|
161
|
-
}
|
|
162
|
-
|
|
163
|
-
for (const ch of value) if (!isXmlChar(ch.codePointAt(0))) fail("a character XML does not allow");
|
|
164
|
-
}
|
|
165
|
-
|
|
166
|
-
checkChars(raw);
|
|
265
|
+
}
|
|
266
|
+
#checkChars(value) {
|
|
267
|
+
for (const ch of value) if (!isXmlChar(ch.codePointAt(0))) this.fail("a character XML does not allow");
|
|
268
|
+
}
|
|
269
|
+
#decode(raw) {
|
|
270
|
+
this.#checkChars(raw);
|
|
167
271
|
if (!raw.includes("&")) return raw;
|
|
168
272
|
return raw.replace(/&([^;&\s]*);?/g, (match, name) => {
|
|
169
|
-
if (!match.endsWith(";")) return fail("an unterminated entity reference");
|
|
273
|
+
if (!match.endsWith(";")) return this.fail("an unterminated entity reference");
|
|
170
274
|
if (name.startsWith("#")) {
|
|
171
|
-
const code =
|
|
172
|
-
if (!isXmlChar(code)) return fail(`an invalid character reference &${name};`);
|
|
275
|
+
const code = characterReference(name);
|
|
276
|
+
if (!isXmlChar(code)) return this.fail(`an invalid character reference &${name};`);
|
|
173
277
|
return String.fromCodePoint(code);
|
|
174
278
|
}
|
|
175
279
|
const value = Object.hasOwn(PREDEFINED, name) ? PREDEFINED[name] : void 0;
|
|
176
|
-
if (value === void 0) return fail(`an undeclared entity &${name};`);
|
|
280
|
+
if (value === void 0) return this.fail(`an undeclared entity &${name};`);
|
|
177
281
|
return value;
|
|
178
282
|
});
|
|
179
|
-
}
|
|
180
|
-
|
|
181
|
-
|
|
283
|
+
}
|
|
284
|
+
#readAttributes() {
|
|
285
|
+
const text = this.#text;
|
|
286
|
+
const attrs = /* @__PURE__ */ new Map();
|
|
182
287
|
for (;;) {
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
288
|
+
const before = this.i;
|
|
289
|
+
this.#skipSpace();
|
|
290
|
+
if (this.#at("/>") || this.#at(">")) return attrs;
|
|
291
|
+
if (this.i === before) this.fail("expected white space between attributes");
|
|
292
|
+
const attr = this.#readName();
|
|
293
|
+
this.#skipSpace();
|
|
294
|
+
this.#expect("=");
|
|
295
|
+
this.#skipSpace();
|
|
296
|
+
const quote = text[this.i];
|
|
297
|
+
if (quote !== "\"" && quote !== "'") this.fail("expected a quoted attribute value");
|
|
298
|
+
const end = text.indexOf(quote, this.i + 1);
|
|
299
|
+
if (end === -1) this.fail("an unterminated attribute value");
|
|
300
|
+
const raw = text.slice(this.i + 1, end);
|
|
301
|
+
if (raw.includes("<")) this.fail("< in an attribute value");
|
|
302
|
+
this.#advance(end + 1);
|
|
303
|
+
if (attrs.has(attr)) this.fail(`attribute ${attr} given twice`);
|
|
304
|
+
attrs.set(attr, this.#decode(raw.replace(/[\t\n]/g, " ")));
|
|
194
305
|
}
|
|
195
|
-
}
|
|
196
|
-
|
|
306
|
+
}
|
|
307
|
+
#resolve(name, scopes, attribute) {
|
|
197
308
|
const colon = name.indexOf(":");
|
|
198
309
|
const prefix = colon === -1 ? "" : name.slice(0, colon);
|
|
199
310
|
const local = colon === -1 ? name : name.slice(colon + 1);
|
|
@@ -212,57 +323,15 @@ function parseXml(input) {
|
|
|
212
323
|
local
|
|
213
324
|
};
|
|
214
325
|
}
|
|
215
|
-
if (prefix !== "") fail(`an undeclared namespace prefix ${prefix}:`);
|
|
326
|
+
if (prefix !== "") this.fail(`an undeclared namespace prefix ${prefix}:`);
|
|
216
327
|
return {
|
|
217
328
|
ns: null,
|
|
218
329
|
local
|
|
219
330
|
};
|
|
220
|
-
}
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
const
|
|
224
|
-
expect("<");
|
|
225
|
-
const name = readName();
|
|
226
|
-
const attrs = /* @__PURE__ */ new Map();
|
|
227
|
-
for (;;) {
|
|
228
|
-
const before = i;
|
|
229
|
-
skipSpace();
|
|
230
|
-
if (text.startsWith("/>", i) || text.startsWith(">", i)) break;
|
|
231
|
-
if (i === before) fail("expected white space between attributes");
|
|
232
|
-
const attr = readName();
|
|
233
|
-
skipSpace();
|
|
234
|
-
expect("=");
|
|
235
|
-
skipSpace();
|
|
236
|
-
const quote = text[i];
|
|
237
|
-
if (quote !== "\"" && quote !== "'") fail("expected a quoted attribute value");
|
|
238
|
-
const end = text.indexOf(quote, i + 1);
|
|
239
|
-
if (end === -1) fail("an unterminated attribute value");
|
|
240
|
-
const raw = text.slice(i + 1, end);
|
|
241
|
-
if (raw.includes("<")) fail("< in an attribute value");
|
|
242
|
-
advance(end + 1);
|
|
243
|
-
if (attrs.has(attr)) fail(`attribute ${attr} given twice`);
|
|
244
|
-
attrs.set(attr, decode(raw.replace(/[\t\n]/g, " ")));
|
|
245
|
-
}
|
|
246
|
-
const declared = /* @__PURE__ */ new Map();
|
|
247
|
-
for (const [attr, value] of attrs) if (attr === "xmlns") declared.set("", value);
|
|
248
|
-
else if (attr.startsWith("xmlns:")) declared.set(attr.slice(6), value);
|
|
249
|
-
const inner = declared.size > 0 ? [...scopes, declared] : scopes;
|
|
250
|
-
const { ns, local } = resolve(name, inner, false);
|
|
251
|
-
for (const attr of attrs.keys()) if (attr !== "xmlns" && !attr.startsWith("xmlns:")) resolve(attr, inner, true);
|
|
252
|
-
const node = {
|
|
253
|
-
kind: "element",
|
|
254
|
-
name,
|
|
255
|
-
ns,
|
|
256
|
-
local,
|
|
257
|
-
attrs,
|
|
258
|
-
children: [],
|
|
259
|
-
line: start
|
|
260
|
-
};
|
|
261
|
-
if (text.startsWith("/>", i)) {
|
|
262
|
-
advance(i + 2);
|
|
263
|
-
return node;
|
|
264
|
-
}
|
|
265
|
-
advance(i + 1);
|
|
331
|
+
}
|
|
332
|
+
/** Children up to and including the end tag; adjacent text and CDATA join into one text node. */
|
|
333
|
+
#readContent(node, scopes, depth) {
|
|
334
|
+
const text = this.#text;
|
|
266
335
|
let buffer = "";
|
|
267
336
|
const flush = () => {
|
|
268
337
|
if (buffer !== "") node.children.push({
|
|
@@ -272,59 +341,54 @@ function parseXml(input) {
|
|
|
272
341
|
buffer = "";
|
|
273
342
|
};
|
|
274
343
|
for (;;) {
|
|
275
|
-
if (i >= text.length) fail(`<${name}> is never closed`);
|
|
276
|
-
if (
|
|
344
|
+
if (this.i >= text.length) this.fail(`<${node.name}> is never closed`);
|
|
345
|
+
if (this.#at("</")) {
|
|
277
346
|
flush();
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
if (end !== name) fail(`</${end}> closes <${name}>`);
|
|
281
|
-
skipSpace();
|
|
282
|
-
expect(">");
|
|
283
|
-
return node;
|
|
347
|
+
this.#readEndTag(node.name);
|
|
348
|
+
return;
|
|
284
349
|
}
|
|
285
|
-
if (
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
} else if (text.startsWith("<![CDATA[", i)) {
|
|
290
|
-
const end = text.indexOf("]]>", i + 9);
|
|
291
|
-
if (end === -1) fail("an unterminated CDATA section");
|
|
292
|
-
const raw = text.slice(i + 9, end);
|
|
293
|
-
checkChars(raw);
|
|
294
|
-
buffer += raw;
|
|
295
|
-
advance(end + 3);
|
|
296
|
-
} else if (text.startsWith("<?", i)) {
|
|
297
|
-
const end = text.indexOf("?>", i + 2);
|
|
298
|
-
if (end === -1) fail("an unterminated processing instruction");
|
|
299
|
-
advance(end + 2);
|
|
300
|
-
} else if (text[i] === "<") {
|
|
350
|
+
if (this.#at("<!--")) this.#skipComment();
|
|
351
|
+
else if (this.#at("<![CDATA[")) buffer += this.#readCdata();
|
|
352
|
+
else if (this.#at("<?")) this.#skipProcessingInstruction();
|
|
353
|
+
else if (text[this.i] === "<") {
|
|
301
354
|
flush();
|
|
302
|
-
node.children.push(element(
|
|
303
|
-
} else
|
|
304
|
-
let end = text.indexOf("<", i);
|
|
305
|
-
if (end === -1) end = text.length;
|
|
306
|
-
const raw = text.slice(i, end);
|
|
307
|
-
if (raw.includes("]]>")) fail("]]> in text");
|
|
308
|
-
buffer += decode(raw);
|
|
309
|
-
advance(end);
|
|
310
|
-
}
|
|
355
|
+
node.children.push(this.element(scopes, depth + 1));
|
|
356
|
+
} else buffer += this.#readText();
|
|
311
357
|
}
|
|
312
|
-
}
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
}
|
|
358
|
+
}
|
|
359
|
+
#readEndTag(name) {
|
|
360
|
+
this.#advance(this.i + 2);
|
|
361
|
+
const end = this.#readName();
|
|
362
|
+
if (end !== name) this.fail(`</${end}> closes <${name}>`);
|
|
363
|
+
this.#skipSpace();
|
|
364
|
+
this.#expect(">");
|
|
365
|
+
}
|
|
366
|
+
#readCdata() {
|
|
367
|
+
const end = this.#text.indexOf("]]>", this.i + 9);
|
|
368
|
+
if (end === -1) this.fail("an unterminated CDATA section");
|
|
369
|
+
const raw = this.#text.slice(this.i + 9, end);
|
|
370
|
+
this.#checkChars(raw);
|
|
371
|
+
this.#advance(end + 3);
|
|
372
|
+
return raw;
|
|
373
|
+
}
|
|
374
|
+
#readText() {
|
|
375
|
+
const text = this.#text;
|
|
376
|
+
let end = text.indexOf("<", this.i);
|
|
377
|
+
if (end === -1) end = text.length;
|
|
378
|
+
const raw = text.slice(this.i, end);
|
|
379
|
+
if (raw.includes("]]>")) this.fail("]]> in text");
|
|
380
|
+
const decoded = this.#decode(raw);
|
|
381
|
+
this.#advance(end);
|
|
382
|
+
return decoded;
|
|
383
|
+
}
|
|
384
|
+
};
|
|
320
385
|
/** Child elements of one namespace, in order. */
|
|
321
386
|
function childElements(node, ns, local) {
|
|
322
387
|
const out = [];
|
|
323
|
-
for (const child of node.children) if (child.kind === "element" && child.ns === ns &&
|
|
388
|
+
for (const child of node.children) if (child.kind === "element" && child.ns === ns && child.local === local) out.push(child);
|
|
324
389
|
return out;
|
|
325
390
|
}
|
|
326
|
-
|
|
327
|
-
//#region src/core/exchange/xliff.ts
|
|
391
|
+
// src/core/exchange/xliff.ts
|
|
328
392
|
const XLIFF_NS = "urn:oasis:names:tc:xliff:document:2.0";
|
|
329
393
|
const AI_SUBSTATE = "vi18n:ai";
|
|
330
394
|
const STATES = [
|
|
@@ -333,29 +397,37 @@ const STATES = [
|
|
|
333
397
|
"reviewed",
|
|
334
398
|
"final"
|
|
335
399
|
];
|
|
400
|
+
function isSegmentState(value) {
|
|
401
|
+
return STATES.includes(value);
|
|
402
|
+
}
|
|
336
403
|
/**
|
|
337
|
-
*
|
|
338
|
-
*
|
|
339
|
-
*
|
|
404
|
+
* Escaped text in which a carriage return is a character reference, since
|
|
405
|
+
* readers turn a literal one into a line feed, and `invalid` replaces each
|
|
406
|
+
* character XML cannot hold.
|
|
340
407
|
*/
|
|
341
|
-
function
|
|
408
|
+
function xmlChars(value, invalid) {
|
|
342
409
|
let out = "";
|
|
343
410
|
for (const ch of escapeText(value)) {
|
|
344
411
|
const code = ch.codePointAt(0);
|
|
345
412
|
if (code === 13) out += " ";
|
|
346
413
|
else if (isXmlChar(code)) out += ch;
|
|
347
|
-
else out +=
|
|
414
|
+
else out += invalid(code);
|
|
348
415
|
}
|
|
349
416
|
return out;
|
|
350
417
|
}
|
|
418
|
+
/** Segment content: a character XML cannot hold becomes `<cp hex>` (XLIFF 2 §4.3.1.8). */
|
|
419
|
+
function content(value) {
|
|
420
|
+
return xmlChars(value, (code) => `<cp hex="${code.toString(16).toUpperCase().padStart(4, "0")}"/>`);
|
|
421
|
+
}
|
|
351
422
|
/** Notes hold text only; a character XML cannot hold is replaced. */
|
|
352
423
|
function noteText(value) {
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
return
|
|
424
|
+
return xmlChars(value, () => "�");
|
|
425
|
+
}
|
|
426
|
+
/** The `<segment>` state attributes of a unit with a target (module docs: States). */
|
|
427
|
+
function stateAttributes(unit) {
|
|
428
|
+
if (unit.fuzzy) return " state=\"initial\"";
|
|
429
|
+
if (unit.state === "ai") return ` state="translated" subState="${AI_SUBSTATE}"`;
|
|
430
|
+
return " state=\"translated\"";
|
|
359
431
|
}
|
|
360
432
|
/** White space a reader may normalise unless told to preserve it. */
|
|
361
433
|
function space(value) {
|
|
@@ -378,9 +450,7 @@ function serializeXliff(units, meta) {
|
|
|
378
450
|
lines.push(" </notes>");
|
|
379
451
|
}
|
|
380
452
|
const hasTarget = !meta.isSourceLocale && unit.translation !== "";
|
|
381
|
-
|
|
382
|
-
if (hasTarget) state = unit.fuzzy ? " state=\"initial\"" : unit.state === "ai" ? ` state="translated" subState="${AI_SUBSTATE}"` : " state=\"translated\"";
|
|
383
|
-
lines.push(` <segment${state}>`);
|
|
453
|
+
lines.push(` <segment${hasTarget ? stateAttributes(unit) : ""}>`);
|
|
384
454
|
lines.push(` <source${space(unit.source)}>${content(unit.source)}</source>`);
|
|
385
455
|
if (hasTarget) lines.push(` <target${space(unit.translation)}>${content(unit.translation)}</target>`);
|
|
386
456
|
lines.push(" </segment>", " </unit>");
|
|
@@ -399,7 +469,7 @@ function inlineText(node) {
|
|
|
399
469
|
if (child.ns === "urn:oasis:names:tc:xliff:document:2.0" && child.local === "cp") {
|
|
400
470
|
const hex = child.attrs.get("hex") ?? "";
|
|
401
471
|
const code = /^[0-9a-fA-F]{1,6}$/.test(hex) ? Number.parseInt(hex, 16) : NaN;
|
|
402
|
-
if (
|
|
472
|
+
if (Number.isNaN(code) || code > 1114111) return { unsupported: `<cp hex="${hex}"/>` };
|
|
403
473
|
text += String.fromCodePoint(code);
|
|
404
474
|
continue;
|
|
405
475
|
}
|
|
@@ -430,24 +500,14 @@ function parseXliff(text) {
|
|
|
430
500
|
} catch (error) {
|
|
431
501
|
if (!(error instanceof XmlError)) throw error;
|
|
432
502
|
return {
|
|
433
|
-
|
|
434
|
-
srcLang: null
|
|
435
|
-
records: [],
|
|
436
|
-
error: {
|
|
437
|
-
message: `not well-formed XML: ${error.message}`,
|
|
438
|
-
line: error.line
|
|
439
|
-
}
|
|
503
|
+
...failed(`not well-formed XML: ${error.message}`, error.line),
|
|
504
|
+
srcLang: null
|
|
440
505
|
};
|
|
441
506
|
}
|
|
442
507
|
const version = root.attrs.get("version") ?? "";
|
|
443
508
|
if (root.ns !== "urn:oasis:names:tc:xliff:document:2.0" || root.local !== "xliff" || !version.startsWith("2.")) return {
|
|
444
|
-
|
|
445
|
-
srcLang: null
|
|
446
|
-
records: [],
|
|
447
|
-
error: {
|
|
448
|
-
message: root.local === "xliff" ? `XLIFF ${version || "without a version"} is not supported; export and import XLIFF 2.0` : `the root element is <${root.name}>, not an XLIFF 2 <xliff>`,
|
|
449
|
-
line: root.line
|
|
450
|
-
}
|
|
509
|
+
...failed(root.local === "xliff" ? `XLIFF ${version || "without a version"} is not supported; export and import XLIFF 2.0` : `the root element is <${root.name}>, not an XLIFF 2 <xliff>`, root.line),
|
|
510
|
+
srcLang: null
|
|
451
511
|
};
|
|
452
512
|
const records = [];
|
|
453
513
|
const walk = (node) => {
|
|
@@ -465,6 +525,29 @@ function parseXliff(text) {
|
|
|
465
525
|
error: null
|
|
466
526
|
};
|
|
467
527
|
}
|
|
528
|
+
/** One `<segment>` or `<ignorable>`: its source and target text, or the markup that cannot be read. */
|
|
529
|
+
function partText(part) {
|
|
530
|
+
const [from] = childElements(part, XLIFF_NS, "source");
|
|
531
|
+
const [to] = childElements(part, XLIFF_NS, "target");
|
|
532
|
+
const source = from === void 0 ? { text: "" } : inlineText(from);
|
|
533
|
+
if ("unsupported" in source) return source;
|
|
534
|
+
if (to === void 0) return {
|
|
535
|
+
source: source.text,
|
|
536
|
+
target: null
|
|
537
|
+
};
|
|
538
|
+
const target = inlineText(to);
|
|
539
|
+
if ("unsupported" in target) return target;
|
|
540
|
+
return {
|
|
541
|
+
source: source.text,
|
|
542
|
+
target: target.text
|
|
543
|
+
};
|
|
544
|
+
}
|
|
545
|
+
/** A segment's `state`, or what it means when absent: a target makes it translated. */
|
|
546
|
+
function segmentState(part, hasTarget) {
|
|
547
|
+
const written = part.attrs.get("state");
|
|
548
|
+
if (isSegmentState(written)) return written;
|
|
549
|
+
return hasTarget ? "translated" : "initial";
|
|
550
|
+
}
|
|
468
551
|
function unitRecord(unit) {
|
|
469
552
|
const raw = unit.attrs.get("id") ?? "";
|
|
470
553
|
const { key, label } = parseExchangeKey(raw);
|
|
@@ -486,27 +569,19 @@ function unitRecord(unit) {
|
|
|
486
569
|
for (const part of unit.children) {
|
|
487
570
|
if (part.kind !== "element" || part.ns !== "urn:oasis:names:tc:xliff:document:2.0") continue;
|
|
488
571
|
if (part.local !== "segment" && part.local !== "ignorable") continue;
|
|
489
|
-
const
|
|
490
|
-
|
|
491
|
-
const sourceText = from === void 0 ? { text: "" } : inlineText(from);
|
|
492
|
-
const targetText = to === void 0 ? null : inlineText(to);
|
|
493
|
-
if ("unsupported" in sourceText) return {
|
|
572
|
+
const texts = partText(part);
|
|
573
|
+
if ("unsupported" in texts) return {
|
|
494
574
|
...record,
|
|
495
|
-
unsupported:
|
|
575
|
+
unsupported: texts.unsupported
|
|
496
576
|
};
|
|
497
|
-
|
|
498
|
-
...record,
|
|
499
|
-
unsupported: targetText.unsupported
|
|
500
|
-
};
|
|
501
|
-
source += sourceText.text;
|
|
577
|
+
source += texts.source;
|
|
502
578
|
if (part.local === "ignorable") {
|
|
503
|
-
target +=
|
|
579
|
+
target += texts.target ?? texts.source;
|
|
504
580
|
continue;
|
|
505
581
|
}
|
|
506
|
-
if (
|
|
507
|
-
else target +=
|
|
508
|
-
const
|
|
509
|
-
const state = STATES.includes(written ?? "") ? written : targetText === null ? "initial" : "translated";
|
|
582
|
+
if (texts.target === null) complete = false;
|
|
583
|
+
else target += texts.target;
|
|
584
|
+
const state = segmentState(part, texts.target !== null);
|
|
510
585
|
if (rank(state) < rank(least)) least = state;
|
|
511
586
|
if (state === "translated" && part.attrs.get("subState") === "vi18n:ai") ai = true;
|
|
512
587
|
}
|
|
@@ -517,8 +592,7 @@ function unitRecord(unit) {
|
|
|
517
592
|
record.state = least === "translated" && ai ? "ai" : "translated";
|
|
518
593
|
return record;
|
|
519
594
|
}
|
|
520
|
-
|
|
521
|
-
//#region src/core/exchange/formats.ts
|
|
595
|
+
// src/core/exchange/formats.ts
|
|
522
596
|
/**
|
|
523
597
|
* Export writers and import readers per format (PLAN §4.4, §5.19). Bilingual
|
|
524
598
|
* formats (PO, vi18n JSON, XLIFF 2, CSV) carry each unit's source; key-only
|
|
@@ -559,20 +633,26 @@ function serializeExport(format, units, meta) {
|
|
|
559
633
|
}
|
|
560
634
|
case "csv": {
|
|
561
635
|
const rows = [[...CSV_COLUMNS]];
|
|
562
|
-
for (const unit of units)
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
unit.entry.contexts.join("\n")
|
|
570
|
-
]);
|
|
571
|
-
}
|
|
636
|
+
for (const unit of units) rows.push([
|
|
637
|
+
exchangeKey(unit),
|
|
638
|
+
unit.source,
|
|
639
|
+
meta.isSourceLocale ? "" : unit.translation,
|
|
640
|
+
meta.isSourceLocale ? "" : csvState(unit),
|
|
641
|
+
unit.entry.contexts.join("\n")
|
|
642
|
+
]);
|
|
572
643
|
return serializeCsv(rows);
|
|
573
644
|
}
|
|
574
645
|
}
|
|
575
646
|
}
|
|
647
|
+
/** A target row's `state` cell: a fuzzy mark wins over the entry's state. */
|
|
648
|
+
function csvState(unit) {
|
|
649
|
+
return unit.fuzzy ? "fuzzy" : unit.state;
|
|
650
|
+
}
|
|
651
|
+
/** The label a catalog entry of `layout` stores: the `"id"` layout's msgid, a generated JSON key. */
|
|
652
|
+
function exportLabel(unit, layout) {
|
|
653
|
+
if (layout === "id") return unit.label;
|
|
654
|
+
return layout === "json" && !unit.manual ? unit.id : null;
|
|
655
|
+
}
|
|
576
656
|
function keyOnlyValue(unit, meta) {
|
|
577
657
|
if (meta.isSourceLocale) return unit.source;
|
|
578
658
|
return unit.usable ? unit.translation : "";
|
|
@@ -584,18 +664,17 @@ function catalogText(format, units, meta) {
|
|
|
584
664
|
const catalog = {
|
|
585
665
|
locale: meta.locale,
|
|
586
666
|
layout,
|
|
587
|
-
header: {
|
|
588
|
-
language: meta.locale,
|
|
667
|
+
header: emptyHeader(meta.locale, {
|
|
589
668
|
idScheme: meta.idScheme,
|
|
590
669
|
sourceLocale: meta.sourceLocale,
|
|
591
670
|
aiModel: meta.header?.aiModel ?? null,
|
|
592
671
|
extra: sameFormat ? meta.header?.extra ?? [] : []
|
|
593
|
-
},
|
|
672
|
+
}),
|
|
594
673
|
entries: units.map((unit) => createEntry({
|
|
595
674
|
...unit.entry,
|
|
596
675
|
manualId: unit.manual ? unit.id : null,
|
|
597
676
|
foreignContext: null,
|
|
598
|
-
label:
|
|
677
|
+
label: exportLabel(unit, layout),
|
|
599
678
|
source: unit.source,
|
|
600
679
|
translation: unit.translation,
|
|
601
680
|
state: unit.state,
|
|
@@ -606,11 +685,10 @@ function catalogText(format, units, meta) {
|
|
|
606
685
|
obsolete: [],
|
|
607
686
|
droppedObsolete: 0
|
|
608
687
|
};
|
|
609
|
-
|
|
688
|
+
return serializeCatalog(catalog, {
|
|
610
689
|
isSourceLocale: meta.isSourceLocale,
|
|
611
690
|
version: VERSION
|
|
612
|
-
};
|
|
613
|
-
return format === "json" ? serializeJsonCatalog(catalog, options) : serializePo(catalog, options);
|
|
691
|
+
});
|
|
614
692
|
}
|
|
615
693
|
/** Reads an import file; `msgid` is the PO layout to read it in (the project's `catalogs.msgid`). */
|
|
616
694
|
function readImport(format, text, options) {
|
|
@@ -623,70 +701,64 @@ function readImport(format, text, options) {
|
|
|
623
701
|
case "csv": return readCsv(text);
|
|
624
702
|
}
|
|
625
703
|
}
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
records: [],
|
|
630
|
-
error: {
|
|
631
|
-
message,
|
|
632
|
-
line
|
|
633
|
-
}
|
|
634
|
-
};
|
|
635
|
-
}
|
|
636
|
-
function readPo(text, layout) {
|
|
637
|
-
const { catalog, diagnostics } = parsePo(text, {
|
|
638
|
-
locale: "",
|
|
639
|
-
layout,
|
|
640
|
-
isSourceLocale: false
|
|
641
|
-
});
|
|
642
|
-
const error = diagnostics.find((diagnostic) => diagnostic.severity === "error");
|
|
704
|
+
/** A read catalog as import records, unless the reader found an error, which fails the whole file. */
|
|
705
|
+
function catalogRecords(read, toRecord) {
|
|
706
|
+
const error = read.diagnostics.find((diagnostic) => diagnostic.severity === "error");
|
|
643
707
|
if (error !== void 0) return failed(error.message, error.line ?? 0);
|
|
644
|
-
const records = catalog.entries.map((entry) => ({
|
|
708
|
+
const records = read.catalog.entries.map((entry) => ({
|
|
645
709
|
line: entry.line,
|
|
646
|
-
|
|
647
|
-
key: entry.foreignContext !== null ? `\u0004${entry.foreignContext}` : entry.manualId ?? (layout === "id" ? entry.label : null),
|
|
648
|
-
label: layout === "id" ? entry.label : null,
|
|
649
|
-
source: layout === "id" ? null : entry.source,
|
|
710
|
+
...toRecord(entry),
|
|
650
711
|
translation: entry.translation,
|
|
651
712
|
state: entry.state === "ai" ? "ai" : "translated",
|
|
652
713
|
draft: entry.fuzzy
|
|
653
714
|
}));
|
|
715
|
+
const language = read.catalog.header.language;
|
|
654
716
|
return {
|
|
655
|
-
locale:
|
|
717
|
+
locale: language === "" ? null : language,
|
|
656
718
|
records,
|
|
657
719
|
error: null
|
|
658
720
|
};
|
|
659
721
|
}
|
|
660
|
-
function
|
|
661
|
-
|
|
722
|
+
function readPo(text, layout) {
|
|
723
|
+
return catalogRecords(parsePo(text, {
|
|
662
724
|
locale: "",
|
|
725
|
+
layout,
|
|
663
726
|
isSourceLocale: false
|
|
727
|
+
}), (entry) => {
|
|
728
|
+
const msgid = layout === "id" ? entry.label : entry.source;
|
|
729
|
+
return {
|
|
730
|
+
ref: entry.manualId ?? entry.foreignContext ?? msgid ?? "",
|
|
731
|
+
key: entry.foreignContext === null ? entry.manualId ?? (layout === "id" ? entry.label : null) : `\u0004${entry.foreignContext}`,
|
|
732
|
+
label: layout === "id" ? entry.label : null,
|
|
733
|
+
source: layout === "id" ? null : entry.source
|
|
734
|
+
};
|
|
664
735
|
});
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
736
|
+
}
|
|
737
|
+
function readJson(text) {
|
|
738
|
+
return catalogRecords(parseJsonCatalog(text, {
|
|
739
|
+
locale: "",
|
|
740
|
+
isSourceLocale: false
|
|
741
|
+
}), (entry) => ({
|
|
669
742
|
ref: entry.manualId ?? entry.label ?? "",
|
|
670
743
|
key: entry.manualId ?? entry.label,
|
|
671
744
|
label: null,
|
|
672
|
-
source: entry.source
|
|
673
|
-
translation: entry.translation,
|
|
674
|
-
state: entry.state === "ai" ? "ai" : "translated",
|
|
675
|
-
draft: entry.fuzzy
|
|
745
|
+
source: entry.source
|
|
676
746
|
}));
|
|
677
|
-
return {
|
|
678
|
-
locale: catalog.header.language === "" ? null : catalog.header.language,
|
|
679
|
-
records,
|
|
680
|
-
error: null
|
|
681
|
-
};
|
|
682
747
|
}
|
|
683
748
|
/** 1-based line of each top-level member, found by a light scan (JSON.parse gives none). */
|
|
684
749
|
function memberLines(text) {
|
|
685
750
|
const lines = /* @__PURE__ */ new Map();
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
|
|
751
|
+
const pattern = /^[ \t]*("(?:[^"\\\n]|\\.)*")\s*:/gm;
|
|
752
|
+
let line = 1;
|
|
753
|
+
let counted = 0;
|
|
754
|
+
for (const match of text.matchAll(pattern)) {
|
|
755
|
+
for (let i = counted; i < match.index; i++) if (text.charCodeAt(i) === 10) line++;
|
|
756
|
+
counted = match.index;
|
|
757
|
+
try {
|
|
758
|
+
const key = JSON.parse(match[1]);
|
|
759
|
+
if (!lines.has(key)) lines.set(key, line);
|
|
760
|
+
} catch {}
|
|
761
|
+
}
|
|
690
762
|
return lines;
|
|
691
763
|
}
|
|
692
764
|
/**
|
|
@@ -737,6 +809,11 @@ function readKeyValue(text, arb) {
|
|
|
737
809
|
error: null
|
|
738
810
|
};
|
|
739
811
|
}
|
|
812
|
+
/** A row's state: `ai` when marked so, else translated; none at all without a `state` column. */
|
|
813
|
+
function csvRecordState(state, hasColumn) {
|
|
814
|
+
if (state === "ai") return "ai";
|
|
815
|
+
return hasColumn ? "translated" : null;
|
|
816
|
+
}
|
|
740
817
|
function readCsv(text) {
|
|
741
818
|
let rows;
|
|
742
819
|
try {
|
|
@@ -767,7 +844,7 @@ function readCsv(text) {
|
|
|
767
844
|
label,
|
|
768
845
|
source: source === "" ? null : source,
|
|
769
846
|
translation: row.cells[translationAt] ?? "",
|
|
770
|
-
state: state
|
|
847
|
+
state: csvRecordState(state, stateAt !== -1),
|
|
771
848
|
draft: state === "fuzzy"
|
|
772
849
|
});
|
|
773
850
|
}
|
|
@@ -777,5 +854,4 @@ function readCsv(text) {
|
|
|
777
854
|
error: null
|
|
778
855
|
};
|
|
779
856
|
}
|
|
780
|
-
//#endregion
|
|
781
857
|
export { serializeExport as n, readImport as t };
|