@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.
Files changed (119) hide show
  1. package/README.md +14 -14
  2. package/dist/{apply-Bre-lY9J.js → apply-FAHl1R6T.js} +69 -89
  3. package/dist/{captured-Bj8bO6vd.js → captured-CTjjDF5o.js} +196 -217
  4. package/dist/{check-DyrAbv73.js → check-DwFwOB1d.js} +66 -84
  5. package/dist/cli.js +2 -3
  6. package/dist/{collect-Cx4_SKOe.js → collect-CYATDjdo.js} +1 -2
  7. package/dist/collisions-uMTuq36M.js +215 -0
  8. package/dist/compile-CEvH-6sY.js +240 -0
  9. package/dist/{compile-Cc97JaW2.js → compile-DqkofbiO.js} +376 -331
  10. package/dist/concurrency-BCY_4zcr.js +1 -0
  11. package/dist/concurrency-Dy2ny50V.js +107 -0
  12. package/dist/config.d.ts +1 -5
  13. package/dist/config.js +4 -5
  14. package/dist/{types-D3QGM8XO.js → diagnostics-1onDF6Ua.js} +26 -26
  15. package/dist/{dir-zw6bGMKQ.js → dir-TGLYuOZb.js} +34 -37
  16. package/dist/{duplicates-yuRljQ6h.js → duplicates-CsUXRX2J.js} +10 -27
  17. package/dist/engine-Bf7nVigL.js +556 -0
  18. package/dist/engine.d.ts +1 -5
  19. package/dist/engine.js +297 -168
  20. package/dist/exchange-DYG9RZoT.js +41 -0
  21. package/dist/{exclude-Du-Zd_qI.js → exclude-vMnm3lqU.js} +2 -3
  22. package/dist/{export-DGsMbcTZ.js → export-BYq0gfLd.js} +17 -38
  23. package/dist/{extract-Dujlsgnk.js → extract-CYtuEo8u.js} +114 -82
  24. package/dist/{formats-KPIg-08V.js → formats-ChV1LVH_.js} +352 -276
  25. package/dist/fs-ezCMZeDV.js +52 -0
  26. package/dist/i18next-1waHziMy.js +1 -0
  27. package/dist/i18next-D66Icz3L.js +1907 -0
  28. package/dist/{import-Cwdhcywt.js → import-BApbQlmm.js} +81 -75
  29. package/dist/index.d.ts +1 -13
  30. package/dist/index.js +14 -16
  31. package/dist/{init-BecCTvKi.js → init-J7AwwA5e.js} +466 -501
  32. package/dist/{library-D5CWvTQs.js → library-BfSOBeBd.js} +975 -451
  33. package/dist/lingui-Ee2farVo.js +902 -0
  34. package/dist/lint.d.ts +0 -4
  35. package/dist/lint.js +69 -82
  36. package/dist/{load-gW-oJt1Q.js → load-BFLLqY8x.js} +272 -147
  37. package/dist/{load-CQRz4PpM.js → load-CMwhukZ4.js} +1211 -1115
  38. package/dist/{locales-DX4MOBFE.js → locales-DxLDrx-h.js} +271 -237
  39. package/dist/{lsp-Cq1Nuu-Q.js → lsp-BC7xDGON.js} +4 -5
  40. package/dist/{match-BGr8EXB3.js → match-BLDcj610.js} +11 -4
  41. package/dist/{mcp-Cf0T7qPW.js → mcp-Cka8ywDF.js} +4 -5
  42. package/dist/{merge-driver-CpiiSVIc.js → merge-driver-B04Apm29.js} +138 -122
  43. package/dist/migrate-D7TNl6SD.js +2169 -0
  44. package/dist/migrate-khCY8TE0.js +1 -0
  45. package/dist/{openai-CTaMDcl-.js → openai-RWw0YlAx.js} +128 -87
  46. package/dist/{output-DsaHKHEt.js → output-CF9uvk_-.js} +15 -9
  47. package/dist/parse.d.ts +1 -3
  48. package/dist/parse.js +7 -8
  49. package/dist/{pipeline-wxS0SApG.js → pipeline-B30irGMD.js} +1 -1
  50. package/dist/{pipeline-C7g11THL.js → pipeline-sgl_q_op.js} +387 -359
  51. package/dist/project-DSzWKU3C.js +544 -0
  52. package/dist/{pseudo-DRv30iN5.js → pseudo-DNbNNVGP.js} +125 -3
  53. package/dist/{raw-C4Y6vyGY.js → raw-YsYZMS9n.js} +52 -29
  54. package/dist/react-intl-tzGIk6G3.js +634 -0
  55. package/dist/restore-BG_zMpSj.js +199 -0
  56. package/dist/rewrite-hAcRJwiZ.js +415 -0
  57. package/dist/{rich-DCUKl5HM.js → rich-ApY1Dm4S.js} +35 -28
  58. package/dist/rolldown-runtime-C1I3wEg2.js +43 -0
  59. package/dist/{rolldown-runtime-D7D4PA-g.js → rolldown-runtime-D8jQpDG7.js} +1 -2
  60. package/dist/run-CFzR3V93.js +647 -0
  61. package/dist/scan-worker.d.ts +2 -4
  62. package/dist/scan-worker.js +1 -1
  63. package/dist/{scoped-BMYpSthj.js → scoped-h3KE8XWW.js} +3 -4
  64. package/dist/{server-BDkykMjb.js → server-WlxZH0oX.js} +275 -330
  65. package/dist/{server-qv3mz70d.js → server-ceuzzkal.js} +292 -386
  66. package/dist/server.d.ts +1 -17
  67. package/dist/server.js +415 -381
  68. package/dist/{sitemap-DV3o8QD7.js → sitemap-796jJKUT.js} +21 -33
  69. package/dist/{sources-D19czDSA.js → sources-DuOdM02f.js} +712 -373
  70. package/dist/{status-DZArW5aE.js → status-CTH3qI-0.js} +30 -30
  71. package/dist/{stdio-FPOKQAgu.js → stdio-D9kTzyqB.js} +2 -4
  72. package/dist/{stdio-CVaJfxA7.js → stdio-FWIVU3P4.js} +1 -2
  73. package/dist/sync-CpL0Qsgw.js +1352 -0
  74. package/dist/{t-DC-c9zE5.js → t-gvEXx25s.js} +76 -53
  75. package/dist/test.d.ts +5 -16
  76. package/dist/test.js +77 -48
  77. package/dist/translate-BrfVRkjl.js +336 -0
  78. package/dist/{ts-BaU5yOf0.js → ts-BX6kwig5.js} +25 -14
  79. package/dist/{types-DtvQ7UdD.js → types-3kwyaCAm.js} +1 -2
  80. package/dist/{types-DsdWBG0b.d.ts → types-BaG1LboH.d.ts} +2 -6
  81. package/dist/{types-CgUCkFU1.d.ts → types-Cy2bPdSg.d.ts} +14 -20
  82. package/dist/types-Ds_vebja.js +41 -0
  83. package/dist/{units-CQ5Q99RC.js → units-VVLgQ7x7.js} +7 -16
  84. package/dist/{url-CRfCOhUI.js → url-Bpc6wLea.js} +24 -11
  85. package/dist/{url-CsBkRaJy.js → url-D2_VlEeH.js} +25 -12
  86. package/dist/{validate-D6DJepZv.js → validate-PGgMsasz.js} +3 -5
  87. package/dist/{validate-BxpupdAf.js → validate-zv4-IZyZ.js} +690 -377
  88. package/dist/{version-BlyLGlOa.js → version-BFs370Ce.js} +3 -5
  89. package/dist/{vite-scan-B_YW4X_a.js → vite-scan-DFKSGVE9.js} +87 -92
  90. package/dist/vite.d.ts +2 -6
  91. package/dist/vite.js +6482 -6018
  92. package/dist/vue-i18n-CBmZqHbG.js +806 -0
  93. package/dist/{which-C6PFI1Dm.js → which-BpyJyBkX.js} +44 -37
  94. package/dist/{worker-84U6uYB3.js → worker-BjzVqJVO.js} +454 -608
  95. package/package.json +8 -8
  96. package/dist/check-D34P-S--.js +0 -1
  97. package/dist/codec-DuvU2VQy.js +0 -207
  98. package/dist/collisions-CfpZU8e4.js +0 -217
  99. package/dist/compile-B5Mz6WJm.js +0 -245
  100. package/dist/concurrency-C9UUWuDC.js +0 -96
  101. package/dist/concurrency-DONL7t4V.js +0 -1
  102. package/dist/engine-B7p40M__.js +0 -531
  103. package/dist/i18next-CrUnKFfh.js +0 -1596
  104. package/dist/i18next-Rgo-v9l0.js +0 -1
  105. package/dist/lingui--PcOOYry.js +0 -833
  106. package/dist/messages-Cxa-5tR0.js +0 -166
  107. package/dist/migrate-CntqvrW4.js +0 -1
  108. package/dist/migrate-dPgq9uzy.js +0 -1484
  109. package/dist/project-Dwx7iPm6.js +0 -381
  110. package/dist/project-pXipT8iG.js +0 -1046
  111. package/dist/react-intl-CaBVwJ9u.js +0 -519
  112. package/dist/restore-Btq66Tid.js +0 -175
  113. package/dist/rewrite-CiTRAICl.js +0 -803
  114. package/dist/run-DYD_VXKy.js +0 -694
  115. package/dist/runtime-DTMmliT1.js +0 -64
  116. package/dist/sync-CcVzger_.js +0 -658
  117. package/dist/translate-DZR3MkSA.js +0 -299
  118. package/dist/types-wSh9fCBY.js +0 -11
  119. package/dist/vue-i18n-CEMnpv7k.js +0 -651
@@ -1,10 +1,9 @@
1
- import { o as isDiagnosticError } from "./types-D3QGM8XO.js";
2
- import { a as isManualId, s as parseIdScheme } from "./validate-BxpupdAf.js";
3
- import { C as usableTranslation, S as entryKey, o as catalogLayout } from "./library-D5CWvTQs.js";
4
- import { r as resolveDuplicates, t as duplicateDiagnostics } from "./duplicates-yuRljQ6h.js";
5
- import { isAbsolute, relative, sep } from "node:path";
1
+ import { n as isDiagnosticError, s as PSEUDO_LOCALES } from "./diagnostics-1onDF6Ua.js";
2
+ import { _ as parseIdScheme, h as isManualId } from "./validate-zv4-IZyZ.js";
3
+ import { A as usableTranslation, E as entryKey, P as displayPath, T as entryCanonical, m as severityOf, o as catalogLayout } from "./library-BfSOBeBd.js";
4
+ import { r as resolveDuplicates, t as duplicateDiagnostics } from "./duplicates-CsUXRX2J.js";
6
5
  import { hash } from "node:crypto";
7
- //#region src/core/compile/hash8.ts
6
+ // src/core/compile/hash8.ts
8
7
  /**
9
8
  * First 8 characters of `base64url(sha256(utf8(text)))` (INTERNALS §2.9.1,
10
9
  * §6 D36): the same family as message ids, and safe in URLs, file names and
@@ -13,8 +12,7 @@ import { hash } from "node:crypto";
13
12
  function hash8(text) {
14
13
  return hash("sha256", text, "base64url").slice(0, 8);
15
14
  }
16
- //#endregion
17
- //#region src/core/compile/assets.ts
15
+ // src/core/compile/assets.ts
18
16
  /**
19
17
  * Server catalogs read from static assets (`ssr.catalogs: "assets"`, PLAN
20
18
  * §5.11, §5.13; spike S12): the versioned layout that the client build and
@@ -79,8 +77,34 @@ async function read(path) {
79
77
  try {
80
78
  assets = (await (workers ??= import(${workers}))).env?.ASSETS;\n } catch (error) {\n workers = undefined;\n throw fail("catalogs are read from static assets through cloudflare:workers, which this runtime cannot import (" + why(error) + "): run this server on workerd, or " + ${modules});\n }\n if (typeof assets?.fetch !== "function") {\n throw fail('catalogs are read through the static-assets binding env.ASSETS, which this Worker does not have: add "assets": { "binding": "ASSETS" } to its wrangler config');\n }\n let response;\n try {\n response = await assets.fetch("https://assets.invalid/" + path);\n } catch (error) {\n throw fail("reading /" + path + " through env.ASSETS failed: " + why(error));\n }\n // A missing file is a 404, or an HTML page under not_found_handling.\n const type = response.headers.get("content-type") ?? "";\n if (!response.ok || /html/i.test(type)) {\n throw fail("/" + path + " is not in the static assets (" + response.status + (response.ok ? " " + type : "") + "): " + ${deploy});\n }\n try {\n return await response.json();\n } catch (error) {\n throw fail("/" + path + " is not JSON: " + why(error));\n }\n}\n`}${options.dir !== null ? `\nconst load = (locale) => read(${JSON.stringify(options.dir)} + locale + "/${SERVER_FILES.catalog}");\n` : `\nconst plan = () => import(${JSON.stringify(options.plan ?? "virtual:vi18n/client-plan")});\n\nasync function load(locale) {\n const p = (await plan()).default;\n if (typeof p !== "object" || p === null || typeof p.serverCatalogs !== "string") {\n throw fail("this server reads its catalogs from static assets, but the client build that writes them did not run (the client plan was never filled in): build the client environment with it, or " + ${modules});\n }\n const dir = p.serverCatalogs + locale + "/";\n // The slice names this deploy's bundle hashes for payloads; server JS\n // carries none, so it stays the same across translation-only deploys.\n const [catalog, slice] = await Promise.all([\n read(dir + "${SERVER_FILES.catalog}"),\n Object.hasOwn(p.hashes, locale) ? null : read(dir + "${SERVER_FILES.slice}"),\n ]);\n if (slice !== null) {\n if (slice?.v !== p.v) {\n throw fail("/" + dir + "${SERVER_FILES.slice} is from message set " + slice?.v + ", this server's is " + p.v + ": " + ${deploy});\n }\n p.hashes[locale] = slice.bundles;\n }\n return catalog;\n}\n`}\nexport default {\n${loaders.join("\n")}\n};\n`;
81
79
  }
82
- //#endregion
83
- //#region src/runtime/types.ts
80
+ // src/core/catalog/bundle.ts
81
+ /**
82
+ * Compact JSON of an id → message map with keys in the given (sorted) order.
83
+ * Written by hand because `JSON.stringify` of an object moves integer-like
84
+ * keys (a manual id such as `"404"`) to the front, and bundle files, server
85
+ * catalogs and their hashes must follow the sorted order exactly.
86
+ */
87
+ function bundleJson(ids, messages) {
88
+ let out = "{";
89
+ let first = true;
90
+ for (const id of ids) {
91
+ const message = messages.get(id);
92
+ if (message === void 0) continue;
93
+ out += (first ? "" : ",") + JSON.stringify(id) + ":" + JSON.stringify(message);
94
+ first = false;
95
+ }
96
+ return out + "}";
97
+ }
98
+ /** The complete server catalog of one locale as JSON, ids sorted. */
99
+ function serverCatalogJson(messages) {
100
+ return bundleJson([...messages.keys()].sort(compareText), messages);
101
+ }
102
+ /** Code-unit order, as `Array.prototype.sort` uses without a comparator. */
103
+ function compareText(a, b) {
104
+ if (a === b) return 0;
105
+ return a < b ? -1 : 1;
106
+ }
107
+ // src/runtime/types.ts
84
108
  /**
85
109
  * Opcodes of the IR. The runtime may inline the literal numbers to save
86
110
  * bytes; 9 (`list`) and 10 (MF2 `.match`) are reserved and never emitted in 1.0.
@@ -99,8 +123,7 @@ const OP = {
99
123
  };
100
124
  /** The `globalThis[Symbol.for("menapo.vi18n")]` key. */
101
125
  const STATE_KEY = "menapo.vi18n";
102
- //#endregion
103
- //#region src/core/ir/compile.ts
126
+ // src/core/ir/compile.ts
104
127
  /**
105
128
  * Compiles a parsed ICU message to the JSON IR the runtime interprets
106
129
  * (INTERNALS §2.3, PLAN §5.9).
@@ -149,7 +172,7 @@ function compileNode(node) {
149
172
  case "number":
150
173
  case "date":
151
174
  case "time": {
152
- const op = node.type === "number" ? OP.number : node.type === "date" ? OP.date : OP.time;
175
+ const op = OP[node.type];
153
176
  const options = copyOptions(node.options);
154
177
  return options === null ? [node.name, op] : [
155
178
  node.name,
@@ -229,8 +252,7 @@ function copyOptions(options) {
229
252
  }
230
253
  return copy;
231
254
  }
232
- //#endregion
233
- //#region src/core/ir/pseudo.ts
255
+ // src/core/ir/pseudo.ts
234
256
  /**
235
257
  * Accented look-alikes of `A–Z` then `a–z` (one precomposed code point each),
236
258
  * readable at a glance yet impossible to mistake for untranslated text.
@@ -373,9 +395,11 @@ function wrap(nodes, open, close) {
373
395
  });
374
396
  return nodes;
375
397
  }
376
- //#endregion
377
- //#region src/core/compile/fallback.ts
378
- const PSEUDO_LOCALES$1 = /* @__PURE__ */ new Set(["en-XA", "ar-XB"]);
398
+ // src/core/compile/fallback.ts
399
+ const PSEUDO = new Set(PSEUDO_LOCALES);
400
+ function isPseudoLocale(locale) {
401
+ return PSEUDO.has(locale);
402
+ }
379
403
  /**
380
404
  * The locales whose catalogs supply `locale`'s messages, most specific first
381
405
  * and always ending with `sourceLocale` (INTERNALS §2.8.1).
@@ -394,7 +418,7 @@ const PSEUDO_LOCALES$1 = /* @__PURE__ */ new Set(["en-XA", "ar-XB"]);
394
418
  function fallbackChain(locale, config) {
395
419
  const source = config.sourceLocale;
396
420
  if (locale === source) return [locale];
397
- if (PSEUDO_LOCALES$1.has(locale)) return [locale, source];
421
+ if (isPseudoLocale(locale)) return [locale, source];
398
422
  const chain = [locale];
399
423
  for (let end = locale.lastIndexOf("-"); end > 0; end = locale.lastIndexOf("-", end - 1)) {
400
424
  const ancestor = locale.slice(0, end);
@@ -409,16 +433,7 @@ function fallbackChain(locale, config) {
409
433
  chain.push(source);
410
434
  return chain;
411
435
  }
412
- //#endregion
413
- //#region src/core/compile/locales.ts
414
- /**
415
- * Locale compilation (INTERNALS §2.8.2, PLAN §5.5 D10, §5.9): catalogs and
416
- * code messages become one IR map per locale, fallbacks applied, with the
417
- * reason every untranslated id fell back. The build, `vi18n compile`, dev
418
- * catalog modules, `check` and `status` all read translations through here,
419
- * so "what ships" has one definition.
420
- */
421
- const PSEUDO_LOCALES = /* @__PURE__ */ new Set(["en-XA", "ar-XB"]);
436
+ // src/core/compile/locales.ts
422
437
  /** Ids listed in `catalog-missing-translation`; the rest are only counted. */
423
438
  const MISSING_LISTED = 20;
424
439
  const NO_ENTRY = {
@@ -430,157 +445,99 @@ const SCHEME_MISMATCH = {
430
445
  reason: "scheme-mismatch"
431
446
  };
432
447
  function compileLocales(input) {
433
- const { config, codec, messages, catalogs } = input;
434
- const sourceFallback = input.sourceFallback ?? true;
435
- const layout = catalogLayout(config);
436
- const sourceLocale = config.sourceLocale;
437
- const invalidSeverity = severityOf(config.strict.invalid);
438
- const missingSeverity = severityOf(config.strict.missing);
439
- const prepared = /* @__PURE__ */ new Map();
440
- const prepare = (locale) => {
441
- let found = prepared.get(locale);
442
- if (found === void 0) {
443
- found = prepareCatalog(catalogs, locale, layout, codec, config.root);
444
- prepared.set(locale, found);
445
- }
446
- return found;
447
- };
448
- let sink = [];
449
- const report = (catalog) => {
450
- if (catalog !== null && catalog.pending !== null) {
451
- for (const diagnostic of catalog.pending) sink.push(diagnostic);
452
- catalog.pending = null;
453
- }
454
- return catalog;
455
- };
456
- const lookup = (catalog, id, message) => {
457
- if (catalog === null) return NO_ENTRY;
458
- const cached = catalog.lookups.get(id);
459
- if (cached !== void 0) return cached;
460
- const result = evaluate(catalog, id, message);
461
- catalog.lookups.set(id, result);
462
- return result;
463
- };
464
- const libraries = /* @__PURE__ */ new Map();
465
- /** A package's language, for its catalogs that differ from the app's (§2.6.11). */
466
- const librarySource = /* @__PURE__ */ new Map();
467
- const librariesOf = (locale) => {
468
- let found = libraries.get(locale);
469
- if (found === void 0) {
470
- found = [];
471
- for (const loaded of catalogs.get(locale)?.libraries ?? []) {
472
- const one = prepareCatalog(/* @__PURE__ */ new Map([[locale, loaded]]), locale, layout, codec, config.root);
473
- if (one === null) continue;
474
- const prepared = {
475
- ...one,
476
- pending: null,
477
- library: true
478
- };
479
- if (loaded.librarySource !== void 0) librarySource.set(prepared, loaded.librarySource);
480
- found.push(prepared);
481
- }
482
- libraries.set(locale, found);
483
- }
484
- return found;
485
- };
486
- /** A chain step: the app's catalog, then each library's for the same locale. */
487
- const step = (catalog, locale, id, message) => {
488
- const own = lookup(catalog, id, message);
489
- if (own.ok) return own;
490
- for (const library of librariesOf(locale)) {
491
- const found = lookup(library, id, message);
492
- if (found.ok) return found;
493
- }
494
- return own;
495
- };
496
- const evaluate = (catalog, id, message) => {
497
- if (catalog.schemeMismatch && (layout === "id" || !message.manual)) return SCHEME_MISMATCH;
498
- const entry = catalog.index.get(id);
499
- if (entry === void 0) return NO_ENTRY;
500
- const usable = usableTranslation(entry, layout, message, codec, catalog.locale);
501
- if (usable.ok) return {
502
- ok: true,
503
- ir: compileMessage(usable.parsed),
504
- state: entry.state === "ai" ? "ai" : "translated"
505
- };
506
- if (catalog.library) return {
507
- ok: false,
508
- reason: usable.reason
509
- };
510
- if (usable.reason === "invalid" && invalidSeverity !== null) sink.push(invalidDiagnostic(catalog, entry, id, usable.diagnostics, invalidSeverity));
511
- else if (usable.reason === "source-mismatch" && entry.manualId !== null) sink.push(sourceMismatchDiagnostic(catalog, entry, id, message, layout));
512
- return {
513
- ok: false,
514
- reason: usable.reason
515
- };
516
- };
517
- const sourceIRs = /* @__PURE__ */ new Map();
518
- const sourceIR = (id, message) => {
519
- let ir = sourceIRs.get(id);
520
- if (ir === void 0) {
521
- ir = parseOrReport(codec, message, sink, (ast) => compileMessage(ast));
522
- sourceIRs.set(id, ir);
523
- }
524
- return ir;
525
- };
526
- const all = withKeep(messages, input.keep ?? config.extraction.keep, {
448
+ const { config, codec, catalogs } = input;
449
+ const compiler = new LocaleCompiler(config, codec, catalogs);
450
+ const keep = input.keep ?? config.extraction.keep;
451
+ const preamble = [];
452
+ compiler.reportTo(preamble);
453
+ const all = withKeep(input.messages, keep, {
527
454
  config,
528
455
  codec,
529
- layout,
530
456
  catalogs,
531
- prepare,
532
- sink: (diagnostic) => sink.push(diagnostic)
457
+ prepare: (locale) => compiler.prepare(locale),
458
+ sink: (diagnostic) => preamble.push(diagnostic)
533
459
  });
534
- const keepSet = new Set(input.keep ?? config.extraction.keep);
535
- const preamble = sink;
460
+ const target = {
461
+ sourceFallback: input.sourceFallback ?? true,
462
+ keep: new Set(keep),
463
+ missingSeverity: severityOf(config.strict.missing)
464
+ };
536
465
  const result = /* @__PURE__ */ new Map();
537
466
  for (const locale of input.locales) {
538
- const chain = fallbackChain(locale, config);
539
467
  const compiled = {
540
468
  locale,
541
- chain,
469
+ chain: fallbackChain(locale, config),
542
470
  messages: /* @__PURE__ */ new Map(),
543
471
  status: /* @__PURE__ */ new Map(),
544
472
  labels: /* @__PURE__ */ new Map(),
545
473
  diagnostics: result.size === 0 ? preamble : []
546
474
  };
547
- sink = compiled.diagnostics;
548
475
  result.set(locale, compiled);
549
- if (PSEUDO_LOCALES.has(locale)) {
550
- compilePseudo(compiled, locale, all, codec);
551
- continue;
552
- }
553
- if (locale === sourceLocale) {
554
- report(prepare(locale));
555
- const status = {
556
- state: "translated",
557
- from: sourceLocale
558
- };
559
- const foreign = librariesOf(locale);
560
- for (const [id, message] of all) {
561
- let ir = null;
562
- for (const library of foreign) {
563
- const found = lookup(library, id, message);
564
- if (found.ok) {
565
- ir = found.ir;
566
- break;
567
- }
568
- }
569
- ir ??= sourceIR(id, message);
570
- if (ir === null) continue;
571
- compiled.messages.set(id, ir);
572
- compiled.status.set(id, status);
573
- if (message.manual) compiled.labels.set(id, message.label);
574
- }
575
- continue;
476
+ compiler.reportTo(compiled.diagnostics);
477
+ if (isPseudoLocale(locale)) compilePseudo(compiled, locale, all, codec);
478
+ else if (locale === config.sourceLocale) compiler.compileSource(compiled, all);
479
+ else compiler.compileTarget(compiled, all, target);
480
+ }
481
+ return result;
482
+ }
483
+ /**
484
+ * One compilation's catalogs, prepared once and shared by every locale whose
485
+ * chain reaches them: lookups are memoised per catalog, source IR per id.
486
+ */
487
+ var LocaleCompiler = class {
488
+ #config;
489
+ #codec;
490
+ #catalogs;
491
+ #layout;
492
+ #invalidSeverity;
493
+ #prepared = /* @__PURE__ */ new Map();
494
+ /** Opted-in packages' catalogs of each locale (§2.6.11). */
495
+ #libraries = /* @__PURE__ */ new Map();
496
+ /** Source IR is the same for every locale that falls back to it. */
497
+ #sourceIRs = /* @__PURE__ */ new Map();
498
+ /**
499
+ * Where findings land: the locale being compiled. A memoised lookup reports
500
+ * once, into the locale that first made it, so each finding appears once.
501
+ */
502
+ #diagnostics = [];
503
+ constructor(config, codec, catalogs) {
504
+ this.#config = config;
505
+ this.#codec = codec;
506
+ this.#catalogs = catalogs;
507
+ this.#layout = catalogLayout(config);
508
+ this.#invalidSeverity = severityOf(config.strict.invalid);
509
+ }
510
+ reportTo(diagnostics) {
511
+ this.#diagnostics = diagnostics;
512
+ }
513
+ prepare(locale) {
514
+ let found = this.#prepared.get(locale);
515
+ if (found === void 0) {
516
+ found = prepareCatalog(this.#catalogs, locale, this.#layout, this.#codec, this.#config.root);
517
+ this.#prepared.set(locale, found);
576
518
  }
577
- const own = report(prepare(locale));
578
- const walk = [];
579
- for (let i = 1; i < chain.length; i++) {
580
- const member = chain[i];
581
- if (member !== sourceLocale) walk.push(report(prepare(member)));
519
+ return found;
520
+ }
521
+ /** The source locale: code text, or a foreign package's catalog for it (§2.6.11). */
522
+ compileSource(compiled, all) {
523
+ const { locale } = compiled;
524
+ this.#report(this.prepare(locale));
525
+ const status = {
526
+ state: "translated",
527
+ from: locale
528
+ };
529
+ for (const [id, message] of all) {
530
+ const ir = this.#libraryHit(locale, id, message) ?? this.#sourceIR(id, message);
531
+ if (ir === null) continue;
532
+ compiled.messages.set(id, ir);
533
+ compiled.status.set(id, status);
534
+ if (message.manual) compiled.labels.set(id, message.label);
582
535
  }
583
- const walkLocales = chain.slice(1).filter((member) => member !== sourceLocale);
536
+ }
537
+ compileTarget(compiled, all, options) {
538
+ const { locale } = compiled;
539
+ const own = this.#report(this.prepare(locale));
540
+ const walk = this.#walk(compiled.chain);
584
541
  const translated = {
585
542
  state: "translated",
586
543
  from: locale
@@ -590,10 +547,10 @@ function compileLocales(input) {
590
547
  from: locale
591
548
  };
592
549
  const missing = /* @__PURE__ */ new Map();
593
- let missingCount = 0;
594
550
  const missingIds = [];
551
+ let missingCount = 0;
595
552
  for (const [id, message] of all) {
596
- const mine = step(own, locale, id, message);
553
+ const mine = this.#step(own, locale, id, message);
597
554
  if (mine.ok) {
598
555
  compiled.messages.set(id, mine.ir);
599
556
  compiled.status.set(id, mine.state === "ai" ? ai : translated);
@@ -602,56 +559,158 @@ function compileLocales(input) {
602
559
  }
603
560
  missingCount++;
604
561
  if (missingIds.length < MISSING_LISTED) missingIds.push(id);
605
- let from = sourceLocale;
606
- let ir = null;
607
- for (let i = 0; i < walk.length; i++) {
608
- const found = step(walk[i], walkLocales[i], id, message);
609
- if (found.ok) {
610
- ir = found.ir;
611
- from = walkLocales[i];
612
- break;
613
- }
614
- }
615
- if (ir === null) for (const library of librariesOf(sourceLocale)) {
616
- if (librarySource.get(library) === locale) continue;
617
- const found = lookup(library, id, message);
618
- if (found.ok) {
619
- ir = found.ir;
620
- break;
621
- }
622
- }
623
- const suppliedByEntry = ir !== null;
624
- if (ir === null && (sourceFallback || keepSet.has(id))) ir = sourceIR(id, message);
562
+ const fallback = this.#fallback(walk, locale, id, message);
563
+ const suppliedByEntry = fallback.ir !== null;
564
+ let ir = fallback.ir;
565
+ if (ir === null && (options.sourceFallback || options.keep.has(id))) ir = this.#sourceIR(id, message);
625
566
  if (ir !== null) compiled.messages.set(id, ir);
626
- const statusKey = `${from}\0${mine.reason}`;
567
+ const statusKey = `${fallback.from}\0${mine.reason}`;
627
568
  let status = missing.get(statusKey);
628
569
  if (status === void 0) {
629
570
  status = {
630
571
  state: "missing",
631
- from,
572
+ from: fallback.from,
632
573
  reason: mine.reason
633
574
  };
634
575
  missing.set(statusKey, status);
635
576
  }
636
577
  compiled.status.set(id, status);
637
578
  if (message.manual) {
638
- const label = suppliedByEntry ? message.label : ownLabel(own, id, layout, codec);
579
+ const label = suppliedByEntry ? message.label : this.#ownLabel(own, id);
639
580
  if (label !== null) compiled.labels.set(id, label);
640
581
  else if (ir !== null) compiled.labels.set(id, message.label);
641
582
  }
642
583
  }
643
- if (missingCount > 0 && missingSeverity !== null) {
644
- const more = missingCount > missingIds.length ? ", …" : "";
645
- compiled.diagnostics.push({
646
- code: "catalog-missing-translation",
647
- severity: missingSeverity,
648
- message: `${missingCount} ${missingCount === 1 ? "message has" : "messages have"} no usable ${locale} translation (${missingIds.join(", ")}${more}); ${missingCount === 1 ? "it falls" : "they fall"} back along ${chain.join(" → ")}`,
649
- locale,
650
- ...own === null ? {} : { file: own.file }
651
- });
584
+ if (missingCount > 0 && options.missingSeverity !== null) compiled.diagnostics.push(missingDiagnostic(compiled, missingCount, missingIds, options.missingSeverity, own));
585
+ }
586
+ /** The chain after the locale itself, without the source locale, which `#fallback` handles last. */
587
+ #walk(chain) {
588
+ const walk = [];
589
+ for (const member of chain.slice(1)) if (member !== this.#config.sourceLocale) walk.push({
590
+ locale: member,
591
+ catalog: this.#report(this.prepare(member))
592
+ });
593
+ return walk;
594
+ }
595
+ /** The first fallback that translates `id`, and where it came from (the source locale when none does). */
596
+ #fallback(walk, locale, id, message) {
597
+ for (const member of walk) {
598
+ const found = this.#step(member.catalog, member.locale, id, message);
599
+ if (found.ok) return {
600
+ ir: found.ir,
601
+ from: member.locale
602
+ };
603
+ }
604
+ const sourceLocale = this.#config.sourceLocale;
605
+ return {
606
+ ir: this.#libraryHit(sourceLocale, id, message, (library) => library.librarySource === locale),
607
+ from: sourceLocale
608
+ };
609
+ }
610
+ /** Hands a catalog's file-level findings to the current locale, once. */
611
+ #report(catalog) {
612
+ if (catalog !== null && catalog.pending !== null) {
613
+ for (const diagnostic of catalog.pending) this.#diagnostics.push(diagnostic);
614
+ catalog.pending = null;
652
615
  }
616
+ return catalog;
653
617
  }
654
- return result;
618
+ #librariesOf(locale) {
619
+ let found = this.#libraries.get(locale);
620
+ if (found !== void 0) return found;
621
+ found = [];
622
+ for (const loaded of this.#catalogs.get(locale)?.libraries ?? []) {
623
+ const one = prepareCatalog(/* @__PURE__ */ new Map([[locale, loaded]]), locale, this.#layout, this.#codec, this.#config.root);
624
+ if (one === null) continue;
625
+ const prepared = {
626
+ ...one,
627
+ pending: null,
628
+ library: true
629
+ };
630
+ if (loaded.librarySource !== void 0) prepared.librarySource = loaded.librarySource;
631
+ found.push(prepared);
632
+ }
633
+ this.#libraries.set(locale, found);
634
+ return found;
635
+ }
636
+ /** The IR of the first library catalog of `locale` that translates `id` usably. */
637
+ #libraryHit(locale, id, message, skip) {
638
+ for (const library of this.#librariesOf(locale)) {
639
+ if (skip?.(library) === true) continue;
640
+ const found = this.#lookup(library, id, message);
641
+ if (found.ok) return found.ir;
642
+ }
643
+ return null;
644
+ }
645
+ /** A chain step: the app's catalog, then each library's for the same locale. */
646
+ #step(catalog, locale, id, message) {
647
+ const own = this.#lookup(catalog, id, message);
648
+ if (own.ok) return own;
649
+ for (const library of this.#librariesOf(locale)) {
650
+ const found = this.#lookup(library, id, message);
651
+ if (found.ok) return found;
652
+ }
653
+ return own;
654
+ }
655
+ #lookup(catalog, id, message) {
656
+ if (catalog === null) return NO_ENTRY;
657
+ const cached = catalog.lookups.get(id);
658
+ if (cached !== void 0) return cached;
659
+ const result = this.#evaluate(catalog, id, message);
660
+ catalog.lookups.set(id, result);
661
+ return result;
662
+ }
663
+ #evaluate(catalog, id, message) {
664
+ const layout = this.#layout;
665
+ if (catalog.schemeMismatch && (layout === "id" || !message.manual)) return SCHEME_MISMATCH;
666
+ const entry = catalog.index.get(id);
667
+ if (entry === void 0) return NO_ENTRY;
668
+ const usable = usableTranslation(entry, layout, message, this.#codec, catalog.locale);
669
+ if (usable.ok) return {
670
+ ok: true,
671
+ ir: compileMessage(usable.parsed),
672
+ state: entry.state === "ai" ? "ai" : "translated"
673
+ };
674
+ if (catalog.library) return {
675
+ ok: false,
676
+ reason: usable.reason
677
+ };
678
+ const severity = this.#invalidSeverity;
679
+ if (usable.reason === "invalid" && severity !== null) this.#diagnostics.push(invalidDiagnostic(catalog, entry, id, usable.diagnostics, severity));
680
+ else if (usable.reason === "source-mismatch" && entry.manualId !== null) this.#diagnostics.push(sourceMismatchDiagnostic(catalog, entry, id, message, layout));
681
+ return {
682
+ ok: false,
683
+ reason: usable.reason
684
+ };
685
+ }
686
+ #sourceIR(id, message) {
687
+ let ir = this.#sourceIRs.get(id);
688
+ if (ir === void 0) {
689
+ ir = parseOrReport(this.#codec, message, this.#diagnostics, (ast) => compileMessage(ast));
690
+ this.#sourceIRs.set(id, ir);
691
+ }
692
+ return ir;
693
+ }
694
+ /** The label of the source a locale's own manual entry was made from, valid or not. */
695
+ #ownLabel(own, id) {
696
+ const entry = own?.index.get(id);
697
+ if (entry === void 0) return null;
698
+ if (this.#layout === "id") return entry.label;
699
+ const canonical = entryCanonical(entry, this.#codec);
700
+ return canonical === null ? null : this.#codec.label(canonical);
701
+ }
702
+ };
703
+ /** The one `catalog-missing-translation` of a locale: how many fall back, the first ids, and along what. */
704
+ function missingDiagnostic(compiled, count, ids, severity, own) {
705
+ const { locale, chain } = compiled;
706
+ const more = count > ids.length ? ", …" : "";
707
+ return {
708
+ code: "catalog-missing-translation",
709
+ severity,
710
+ message: `${count} ${count === 1 ? "message has" : "messages have"} no usable ${locale} translation (${ids.join(", ")}${more}); ${count === 1 ? "it falls" : "they fall"} back along ${chain.join(" → ")}`,
711
+ locale,
712
+ ...own === null ? {} : { file: own.file }
713
+ };
655
714
  }
656
715
  function compilePseudo(compiled, locale, messages, codec) {
657
716
  const status = {
@@ -678,14 +737,6 @@ function parseOrReport(codec, message, sink, build) {
678
737
  return null;
679
738
  }
680
739
  }
681
- function severityOf(level) {
682
- return level === "off" ? null : level === "warn" ? "warning" : "error";
683
- }
684
- function displayPath(root, file) {
685
- const rel = relative(root, file);
686
- if (rel === "" || rel.startsWith("..") || isAbsolute(rel)) return file.split(sep).join("/");
687
- return rel.split(sep).join("/");
688
- }
689
740
  function prepareCatalog(catalogs, locale, layout, codec, root) {
690
741
  const loaded = catalogs.get(locale);
691
742
  if (loaded === void 0 || loaded.catalog === null) return null;
@@ -721,12 +772,10 @@ function schemeDiagnostic(recorded, codec, layout, locale, file) {
721
772
  };
722
773
  }
723
774
  function invalidDiagnostic(catalog, entry, id, found, severity) {
724
- const first = found[0];
725
- const reason = first === void 0 ? "it does not validate" : found.length === 1 ? first.message : `${first.message}, and ${found.length - 1} more`;
726
775
  const diagnostic = {
727
776
  code: "catalog-invalid-translation",
728
777
  severity,
729
- message: `${id}: the ${catalog.locale} translation is not used (${reason}); fix it or clear it`,
778
+ message: `${id}: the ${catalog.locale} translation is not used (${invalidReason(found)}); fix it or clear it`,
730
779
  file: catalog.file,
731
780
  locale: catalog.locale,
732
781
  id
@@ -739,6 +788,11 @@ function invalidDiagnostic(catalog, entry, id, found, severity) {
739
788
  }));
740
789
  return diagnostic;
741
790
  }
791
+ function invalidReason(found) {
792
+ const first = found[0];
793
+ if (first === void 0) return "it does not validate";
794
+ return found.length === 1 ? first.message : `${first.message}, and ${found.length - 1} more`;
795
+ }
742
796
  function sourceMismatchDiagnostic(catalog, entry, id, message, layout) {
743
797
  const old = entry.source ?? (layout === "id" ? entry.label : null);
744
798
  const was = old === null ? "" : ` is for ${JSON.stringify(old)}`;
@@ -753,19 +807,6 @@ function sourceMismatchDiagnostic(catalog, entry, id, message, layout) {
753
807
  if (entry.line > 0) diagnostic.line = entry.line;
754
808
  return diagnostic;
755
809
  }
756
- /** The label of the source a locale's own manual entry was made from, valid or not. */
757
- function ownLabel(own, id, layout, codec) {
758
- const entry = own?.index.get(id);
759
- if (entry === void 0) return null;
760
- if (layout === "id") return entry.label;
761
- if (entry.source === null) return null;
762
- try {
763
- return codec.label(codec.canonicalize(entry.source));
764
- } catch (error) {
765
- if (isDiagnosticError(error)) return null;
766
- throw error;
767
- }
768
- }
769
810
  /**
770
811
  * The messages plus `keep` ids no message declares, whose identity comes from
771
812
  * their catalog entries (INTERNALS §2.8.2): the source locale's entry first
@@ -789,7 +830,7 @@ function withKeep(messages, keep, context) {
789
830
  if (entry === void 0) continue;
790
831
  found = true;
791
832
  if (entry.source === null) continue;
792
- identity = keepIdentity(id, entry.source, codec);
833
+ identity = keepIdentity(id, entry, codec);
793
834
  if (identity !== null) break;
794
835
  }
795
836
  if (identity !== null) {
@@ -806,22 +847,15 @@ function withKeep(messages, keep, context) {
806
847
  }
807
848
  return all;
808
849
  }
809
- function keepIdentity(id, source, codec) {
810
- let canonical;
811
- try {
812
- canonical = codec.canonicalize(source);
813
- } catch (error) {
814
- if (isDiagnosticError(error)) return null;
815
- throw error;
816
- }
817
- const sourceHash = codec.sourceHash(canonical);
850
+ function keepIdentity(id, entry, codec) {
851
+ const canonical = entryCanonical(entry, codec);
852
+ if (canonical === null) return null;
818
853
  return {
819
854
  id,
820
855
  manual: isManualId(id),
821
856
  canonical,
822
- sourceHash,
823
- label: sourceHash.slice(0, codec.idLength)
857
+ sourceHash: codec.sourceHash(canonical),
858
+ label: codec.label(canonical)
824
859
  };
825
860
  }
826
- //#endregion
827
- export { assetCatalogsModule as a, hash8 as c, SERVER_FILES as i, compileMessage as n, serverCatalogVersion as o, STATE_KEY as r, serverCatalogsDir as s, compileLocales as t };
861
+ export { compareText as a, assetCatalogsModule as c, hash8 as d, bundleJson as i, serverCatalogVersion as l, compileMessage as n, serverCatalogJson as o, STATE_KEY as r, SERVER_FILES as s, compileLocales as t, serverCatalogsDir as u };