@menapo/vi18n 0.1.1 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (121) hide show
  1. package/README.md +19 -0
  2. package/dist/{apply-Bre-lY9J.js → apply-Bg-3rB2m.js} +69 -89
  3. package/dist/{captured-Bj8bO6vd.js → captured-DfLk50Ji.js} +196 -217
  4. package/dist/{check-BIKEuu6B.js → check-CunekuYs.js} +76 -85
  5. package/dist/cli.js +2 -3
  6. package/dist/{collect-Cx4_SKOe.js → collect-CYATDjdo.js} +1 -2
  7. package/dist/collisions-CDSdtLQm.js +215 -0
  8. package/dist/{compile-Cc97JaW2.js → compile-DqkofbiO.js} +376 -331
  9. package/dist/compile-hva0qjhZ.js +240 -0
  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-LgIWyeSp.js} +28 -27
  15. package/dist/{dir-zw6bGMKQ.js → dir-TGLYuOZb.js} +34 -37
  16. package/dist/{duplicates-yuRljQ6h.js → duplicates-8cudcHNj.js} +10 -27
  17. package/dist/engine-ecRD8-IE.js +556 -0
  18. package/dist/engine.d.ts +1 -5
  19. package/dist/engine.js +297 -168
  20. package/dist/exchange-DjwlrTQ9.js +41 -0
  21. package/dist/{exclude-Du-Zd_qI.js → exclude-BFETmvde.js} +2 -3
  22. package/dist/{export-C9Sy6UWv.js → export-D2tsqWIr.js} +17 -38
  23. package/dist/{extract-C3S8btx0.js → extract-BxYchVmY.js} +114 -82
  24. package/dist/{formats-9unqj_fZ.js → formats-BaryZ2N7.js} +352 -276
  25. package/dist/fs-ezCMZeDV.js +52 -0
  26. package/dist/i18next-COO6o0tX.js +1907 -0
  27. package/dist/i18next-Dr7DCQlQ.js +1 -0
  28. package/dist/{import-nANvQutw.js → import-vSC29Cn4.js} +81 -75
  29. package/dist/index.d.ts +1 -13
  30. package/dist/index.js +14 -16
  31. package/dist/{init-C2ScuW6a.js → init-Cc7A-8VX.js} +466 -501
  32. package/dist/{library-D5CWvTQs.js → library-tp_xqzSY.js} +975 -451
  33. package/dist/lingui-BnnlqGXT.js +902 -0
  34. package/dist/lint.d.ts +0 -4
  35. package/dist/lint.js +70 -82
  36. package/dist/{load-gW-oJt1Q.js → load-BSIeGJ5v.js} +275 -148
  37. package/dist/{load-CQRz4PpM.js → load-SSrAeJVB.js} +1214 -1116
  38. package/dist/{locales-DX4MOBFE.js → locales-D0U-yVRm.js} +271 -237
  39. package/dist/{lsp-RMMMc88P.js → lsp-DvC11pBZ.js} +4 -5
  40. package/dist/{match-BGr8EXB3.js → match-BLDcj610.js} +11 -4
  41. package/dist/{mcp-CHVwBoRP.js → mcp-D2zzOTBy.js} +4 -5
  42. package/dist/{merge-driver-BTX87aVm.js → merge-driver-BLI8OtAc.js} +138 -122
  43. package/dist/migrate-1r43OBfe.js +1 -0
  44. package/dist/migrate-ChDGhlC-.js +2169 -0
  45. package/dist/{openai-CTaMDcl-.js → openai-CLn4puY8.js} +128 -87
  46. package/dist/{output-DWE3SIUA.js → output-D5tAt8q4.js} +15 -9
  47. package/dist/parse.d.ts +1 -3
  48. package/dist/parse.js +7 -8
  49. package/dist/{pipeline-C9Dbotkh.js → pipeline-BJSkkRYC.js} +387 -359
  50. package/dist/{pipeline-DbX6rlo5.js → pipeline-BeCc441z.js} +1 -1
  51. package/dist/project-B5KIO0q-.js +544 -0
  52. package/dist/{pseudo-DRv30iN5.js → pseudo-B2DUpoLu.js} +126 -3
  53. package/dist/{raw-C4Y6vyGY.js → raw-YsYZMS9n.js} +52 -29
  54. package/dist/react-intl-C8JehBBL.js +634 -0
  55. package/dist/rename-ZVGq9ZG1.js +577 -0
  56. package/dist/repeated-uuI1tUGg.js +85 -0
  57. package/dist/restore-xlBIrtRt.js +199 -0
  58. package/dist/rewrite-BwzXoBiA.js +415 -0
  59. package/dist/{rich-DCUKl5HM.js → rich-ApY1Dm4S.js} +35 -28
  60. package/dist/rolldown-runtime-C1I3wEg2.js +43 -0
  61. package/dist/{rolldown-runtime-D7D4PA-g.js → rolldown-runtime-D8jQpDG7.js} +1 -2
  62. package/dist/run-CQ9pDLSa.js +674 -0
  63. package/dist/scan-worker.d.ts +2 -4
  64. package/dist/scan-worker.js +1 -1
  65. package/dist/{scoped-BMYpSthj.js → scoped-h3KE8XWW.js} +3 -4
  66. package/dist/{server-Da1Ot9IN.js → server-Br8tAuG2.js} +312 -386
  67. package/dist/{server-deBOq2vq.js → server-DJRpUQVZ.js} +1059 -333
  68. package/dist/server.d.ts +1 -17
  69. package/dist/server.js +419 -379
  70. package/dist/{sitemap-DJXzKoza.js → sitemap-CSAkm6oi.js} +21 -33
  71. package/dist/{sources-D19czDSA.js → sources-DNVbYsgs.js} +713 -373
  72. package/dist/{status-zr4jY59Y.js → status-q6M3rBrh.js} +30 -30
  73. package/dist/{stdio-FPOKQAgu.js → stdio-D9kTzyqB.js} +2 -4
  74. package/dist/{stdio-CVaJfxA7.js → stdio-FWIVU3P4.js} +1 -2
  75. package/dist/sync-BNIAwMhR.js +1352 -0
  76. package/dist/{t-DC-c9zE5.js → t-gvEXx25s.js} +76 -53
  77. package/dist/test.d.ts +5 -16
  78. package/dist/test.js +126 -56
  79. package/dist/translate-BP4iVhOR.js +336 -0
  80. package/dist/{ts-BaU5yOf0.js → ts-BX6kwig5.js} +25 -14
  81. package/dist/{types-DtvQ7UdD.js → types-3kwyaCAm.js} +1 -2
  82. package/dist/{types-DsdWBG0b.d.ts → types-B4wNX6PA.d.ts} +6 -8
  83. package/dist/{types-CgUCkFU1.d.ts → types-_4UdFhR9.d.ts} +24 -21
  84. package/dist/types-xR2exI3L.js +42 -0
  85. package/dist/{units-CQ5Q99RC.js → units-DPPfCN7j.js} +7 -16
  86. package/dist/{url-CRfCOhUI.js → url-Bpc6wLea.js} +24 -11
  87. package/dist/{url-CsBkRaJy.js → url-D2_VlEeH.js} +25 -12
  88. package/dist/{validate-BxpupdAf.js → validate-CgzaDdOY.js} +690 -377
  89. package/dist/{validate-D6DJepZv.js → validate-PGgMsasz.js} +3 -5
  90. package/dist/{version-Byqi2guX.js → version-DC-J5WvV.js} +3 -5
  91. package/dist/{vite-scan-CT_vsp4g.js → vite-scan-kM8Ta3rO.js} +87 -92
  92. package/dist/vite.d.ts +2 -6
  93. package/dist/vite.js +6500 -6032
  94. package/dist/vue-i18n-_uIXlFVh.js +806 -0
  95. package/dist/{which-BOemZ5Q8.js → which-DFWk_KwN.js} +44 -37
  96. package/dist/{worker-84U6uYB3.js → worker-zlhNj4y3.js} +454 -608
  97. package/package.json +5 -5
  98. package/dist/check-HsmmpJZg.js +0 -1
  99. package/dist/codec-DuvU2VQy.js +0 -207
  100. package/dist/collisions-CfpZU8e4.js +0 -217
  101. package/dist/compile-BaDh0cHG.js +0 -245
  102. package/dist/concurrency-C9UUWuDC.js +0 -96
  103. package/dist/concurrency-DONL7t4V.js +0 -1
  104. package/dist/engine-C2ENBRJe.js +0 -531
  105. package/dist/i18next-9HAoxwUI.js +0 -1
  106. package/dist/i18next-BEJgv_S8.js +0 -1596
  107. package/dist/lingui-zUiCl9Um.js +0 -833
  108. package/dist/messages-BfjIExyB.js +0 -166
  109. package/dist/migrate-IpLDMXmq.js +0 -1484
  110. package/dist/migrate-rCQuOGsf.js +0 -1
  111. package/dist/project-Bw9UPIdx.js +0 -1046
  112. package/dist/project-C4GpaN-n.js +0 -381
  113. package/dist/react-intl-BP0u-D5m.js +0 -519
  114. package/dist/restore-CiYv-jxS.js +0 -175
  115. package/dist/rewrite-EsIw_lsq.js +0 -803
  116. package/dist/run-BEcnBbu-.js +0 -694
  117. package/dist/runtime-DTMmliT1.js +0 -64
  118. package/dist/sync-B9V2KFwb.js +0 -658
  119. package/dist/translate-CQnJfLjO.js +0 -299
  120. package/dist/types-wSh9fCBY.js +0 -11
  121. package/dist/vue-i18n-g0iGy6CZ.js +0 -651
@@ -1,5 +1,5 @@
1
- import { _ as DEV, b as trim, g as BUILD, l as quiet, n as S, t as textDirection, x as warnOnce, y as own } from "./dir-zw6bGMKQ.js";
2
- //#region src/runtime/bidi.ts
1
+ import { b as warnOnce, c as quiet, g as DEV, h as BUILD, n as S, t as textDirection, v as own, y as trim } from "./dir-TGLYuOZb.js";
2
+ // src/runtime/bidi.ts
3
3
  /**
4
4
  * Automatic bidi isolation of interpolated strings (INTERNALS §2.3, PLAN
5
5
  * §4.2): a user-supplied name in Arabic inside an English sentence, or a Latin
@@ -15,8 +15,7 @@ const RTL_OR_CONTROL = /[\u0590-\u08FF\u200E\u200F\u202A-\u202E\u2066-\u2069\uFB
15
15
  * counts the value's open isolates (a parameter, not a local: no block body).
16
16
  */
17
17
  const isolate = (value, locale, open = 0) => value && (textDirection(locale) === "rtl" || RTL_OR_CONTROL.test(value)) ? "⁨" + value.replace(/[\u2066-\u2069]/g, (c) => (c < "⁩" ? ++open : open && open--) ? c : "") + "⁩".repeat(open + 1) : value;
18
- //#endregion
19
- //#region src/runtime/intl.ts
18
+ // src/runtime/intl.ts
20
19
  /**
21
20
  * Intl formatter caches and the scope-taking implementations behind `fmt`
22
21
  * (PLAN §5.12, §5.17). Constructing an Intl formatter costs 23–228 µs and a
@@ -70,7 +69,7 @@ const intern = (o) => {
70
69
  */
71
70
  function formatPart(op, options, value, scope) {
72
71
  if (value == null) return "";
73
- const o = options ? DEV ? intern(options) : options : NO_OPTIONS[op - 4];
72
+ const o = DEV && options ? intern(options) : options ?? NO_OPTIONS[op - 4];
74
73
  let formatters = irCache.get(o);
75
74
  if (!formatters) irCache.set(o, formatters = /* @__PURE__ */ new Map());
76
75
  const { locale, timeZone: zone } = scope;
@@ -306,28 +305,23 @@ const fDuration = (scope, duration, options) => {
306
305
  const parts = [];
307
306
  const display = style === "digital" ? "short" : style;
308
307
  const v = (u) => duration[u + "s"] ?? 0;
309
- for (const u of DURATION_UNITS) {
310
- if (style === "digital" && u === "hour") {
311
- const two = nf(scope, "n", { minimumIntegerDigits: 2 });
312
- parts.push(fNumber(scope, v("hour")) + ":" + two.format(v("minute")) + ":" + two.format(v("second") + v("millisecond") / 1e3));
313
- break;
314
- }
315
- if (v(u)) parts.push(nf(scope, "U" + u + display, { unitDisplay: display }, {
316
- style: "unit",
317
- unit: u
318
- }).format(v(u)));
319
- }
320
- if (!parts.length) parts.push(nf(scope, "Usecond" + display, { unitDisplay: display }, {
308
+ const unitText = (u, n) => nf(scope, "U" + u + display, { unitDisplay: display }, {
321
309
  style: "unit",
322
- unit: "second"
323
- }).format(0));
310
+ unit: u
311
+ }).format(n);
312
+ const digital = style === "digital";
313
+ for (const u of digital ? DURATION_UNITS.slice(0, 4) : DURATION_UNITS) if (v(u)) parts.push(unitText(u, v(u)));
314
+ if (digital) {
315
+ const two = nf(scope, "n", { minimumIntegerDigits: 2 });
316
+ parts.push(fNumber(scope, v("hour")) + ":" + two.format(v("minute")) + ":" + two.format(v("second") + v("millisecond") / 1e3));
317
+ }
318
+ if (!parts.length) parts.push(unitText("second", 0));
324
319
  return fList(scope, parts, {
325
320
  type: "unit",
326
321
  style: display === "long" ? "long" : display
327
322
  });
328
323
  };
329
- //#endregion
330
- //#region src/runtime/format.ts
324
+ // src/runtime/format.ts
331
325
  /**
332
326
  * The IR interpreter (INTERNALS §2.3 "Interpretation"). Normative: the server
333
327
  * and the client must render byte-identical text, so every rule lives here
@@ -354,8 +348,23 @@ function caseOf(part, value, locale) {
354
348
  }
355
349
  return own(cases, key) ? cases[key] : cases.other;
356
350
  }
357
- /** `#` value inside a plural case: the raw number minus the offset; null (nothing) without a value. */
358
- const poundOf = (part, value) => value == null ? null : Number(value) - (part[3] ?? 0);
351
+ /**
352
+ * `#` value inside a case: a select's keeps `n`, the enclosing plural's; a
353
+ * plural's is the raw number minus the offset, null (nothing) without a value.
354
+ */
355
+ function poundOf(part, value, n) {
356
+ if (part[1] === 1) return n;
357
+ return value == null ? null : Number(value) - (part[3] ?? 0);
358
+ }
359
+ /**
360
+ * Dev only: warns about a value part whose argument has no value. A select
361
+ * without one takes `other`, as for any value it lacks a case for, and tags
362
+ * (7, 8) have handlers, not values.
363
+ */
364
+ function warnMissing(value, part, message) {
365
+ const op = part[1];
366
+ if (value == null && op !== 1 && !(op > 6)) warnOnce(`no value for {${part[0]}} in ${JSON.stringify(message)}`);
367
+ }
359
368
  /** `{x, date, medium}`'s options (FormatJS's default `formats`), one object so its formatters are cached. */
360
369
  const MEDIUM = {
361
370
  month: "short",
@@ -387,61 +396,76 @@ function formatMessage(message, values, scope, n = 0, from = 0) {
387
396
  const p = message[i];
388
397
  if (typeof p === "string") out += p;
389
398
  else if (p === 0) out += formatPart(4, void 0, n, scope);
399
+ else if (p[1] > 6) out += formatMessage(p, values, scope, n, 2);
390
400
  else {
391
401
  const op = p[1];
392
- if (op === 7) out += formatMessage(p, values, scope, n, 2);
393
- else if (op !== 8) {
394
- const value = valueOf(values, p[0]);
395
- if (DEV && value == null && op !== 1) warnOnce(`no value for {${p[0]}} in ${JSON.stringify(message)}`);
396
- if (op === void 0) out += argText(value, scope);
397
- else if (op < 4) {
398
- const c = caseOf(p, value, scope.locale);
399
- if (c !== void 0) out += formatMessage(c, values, scope, op === 1 ? n : poundOf(p, value));
400
- } else out += formatPart(op, p[2], value, scope);
401
- }
402
+ const value = valueOf(values, p[0]);
403
+ if (DEV) warnMissing(value, p, message);
404
+ if (op > 3) out += formatPart(op, p[2], value, scope);
405
+ else if (op) {
406
+ const c = caseOf(p, value, scope.locale);
407
+ if (c) out += formatMessage(c, values, scope, poundOf(p, value, n));
408
+ } else out += argText(value, scope);
402
409
  }
403
410
  }
404
411
  return out;
405
412
  }
406
- //#endregion
407
- //#region src/runtime/t.ts
413
+ // src/runtime/t.ts
408
414
  /**
409
415
  * `t` and `msg` (PLAN §5.3, §5.12; INTERNALS §2.11.2). The hot path for a
410
416
  * plain string is: resolve the scope, one `Map.get`, one `typeof`.
411
417
  */
412
418
  const { resolve, track: notify } = S;
413
419
  /**
420
+ * Dev only: the code's built-in message where the dev catalog's translation
421
+ * is for other text. The dev catalog is not recomputed when code changes, so
422
+ * its message for a manual id may translate older text (D10): the code's
423
+ * text wins. Only dev catalogs carry labels.
424
+ */
425
+ function staleTranslation(scope, id, d) {
426
+ if (d?.h === void 0 || d.ir === void 0) return void 0;
427
+ const label = scope.labels?.get(id);
428
+ if (label === void 0 || label === d.h) return void 0;
429
+ warnOnce(`${id}: translation is for other text than ${JSON.stringify(d.message)}; run vi18n extract`);
430
+ S.missing(scope, id, "source-mismatch");
431
+ return d.ir;
432
+ }
433
+ /**
434
+ * What renders for an id no catalog has. Client JS carries ids, not text: a
435
+ * production client renders nothing rather than an id; the server renders
436
+ * the key (PLAN §4.9). Dev renders the text and says why.
437
+ */
438
+ function unknownId(scope, id, text) {
439
+ S.missing(scope, id, "unknown-id");
440
+ if (DEV) {
441
+ warnOnce(`no message ${JSON.stringify(text)} in ${scope.locale}: unknown id, or a call the plugin did not process`);
442
+ return text;
443
+ }
444
+ return scope.kind === "page" && !S.serverResolver && S.registration?.consumer !== "server" ? "" : text;
445
+ }
446
+ /**
414
447
  * The message to render for `key` in `scope`. `track` notifies the framework
415
448
  * trackers; translators bound to a fixed scope pass false.
416
449
  */
417
450
  function lookup(scope, key, track) {
418
451
  let text;
452
+ let id;
419
453
  let d;
420
454
  if (key && typeof key === "object") {
421
455
  d = key;
422
- text = d.message ?? d.id ?? "";
456
+ id = d.id;
457
+ text = d.message ?? id ?? "";
423
458
  } else text = String(key);
424
- const id = d?.id ?? scope.sources?.get(text) ?? text;
459
+ id ??= scope.sources?.get(text) ?? text;
425
460
  if (!quiet) scope.used?.add(id);
426
461
  if (track) notify();
427
462
  if (DEV) scope.seen?.(id);
428
463
  if (!BUILD && scope.test) return scope.test(id, d, text);
429
- if (DEV && d && d.h !== void 0 && d.ir !== void 0) {
430
- const label = scope.labels?.get(id);
431
- if (label !== void 0 && label !== d.h) {
432
- warnOnce(`${id}: translation is for other text than ${JSON.stringify(d.message)}; run vi18n extract`);
433
- S.missing(scope, id, "source-mismatch");
434
- return d.ir;
435
- }
436
- }
437
- const m = scope.messages.get(id) ?? d?.ir;
438
- if (m !== void 0) return m;
439
- S.missing(scope, id, "unknown-id");
440
464
  if (DEV) {
441
- warnOnce(`no message ${JSON.stringify(text)} in ${scope.locale}: unknown id, or a call the plugin did not process`);
442
- return text;
465
+ const stale = staleTranslation(scope, id, d);
466
+ if (stale !== void 0) return stale;
443
467
  }
444
- return scope.kind === "page" && !S.serverResolver && S.registration?.consumer !== "server" ? "" : text;
468
+ return scope.messages.get(id) ?? d?.ir ?? unknownId(scope, id, text);
445
469
  }
446
470
  const t = ((key, values) => {
447
471
  const scope = resolve();
@@ -450,5 +474,4 @@ const t = ((key, values) => {
450
474
  function msg(message) {
451
475
  return message;
452
476
  }
453
- //#endregion
454
- export { fTime as C, fSegment as S, formatPart as T, fDuration as _, caseOf as a, fPercent as b, valueOf as c, fCompare as d, fCurrency as f, fDisplayName as g, fDateRange as h, argText as i, fCollator as l, fDate as m, msg as n, formatMessage as o, fCurrencyMinor as p, t as r, poundOf as s, lookup as t, fCompact as u, fList as v, fUnit as w, fRelative as x, fNumber as y };
477
+ export { fSegment as C, formatPart as E, fRelative as S, fUnit as T, fDisplayName as _, caseOf as a, fNumber as b, valueOf as c, fCompact as d, fCompare as f, fDateRange as g, fDate as h, argText as i, warnMissing as l, fCurrencyMinor as m, msg as n, formatMessage as o, fCurrency as p, t as r, poundOf as s, lookup as t, fCollator as u, fDuration as v, fTime as w, fPercent as x, fList as y };
package/dist/test.d.ts CHANGED
@@ -1,5 +1,4 @@
1
- import { a as Message, y as Translator } from "./types-DsdWBG0b.js";
2
- //#region src/test/i18n.d.ts
1
+ import { a as Message, y as Translator } from "./types-B4wNX6PA.js";
3
2
  /** What every message renders as: its translation or source, its id, or pseudo text. */
4
3
  type TestMode = "source" | "ids" | "pseudo";
5
4
  /** One locale's translations from the project's catalogs (`loadCatalog`). */
@@ -44,8 +43,6 @@ export declare function resetTestI18n(): void;
44
43
  * a call, tests under the plugin render the source locale as before.
45
44
  */
46
45
  export declare function createTestI18n(options?: TestI18nOptions): TestI18n;
47
- //#endregion
48
- //#region src/test/catalog.d.ts
49
46
  interface LoadCatalogOptions {
50
47
  /**
51
48
  * Where to look for `vi18n.config.ts` (walking up). Default: the project
@@ -61,16 +58,9 @@ interface LoadCatalogOptions {
61
58
  * `vite dev`. Memoised per project and locale for the test file.
62
59
  */
63
60
  export declare function loadCatalog(locale: string, options?: LoadCatalogOptions): Promise<TestCatalog>;
64
- //#endregion
65
- //#region src/test/pseudo.d.ts
66
- /**
67
- * Pseudo-locale checks for browser tests (PLAN §4.14, §5.21; INTERNALS §6
68
- * D193): `en-XA` and `ar-XB` bracket every rendered message, so on a page
69
- * switched to one, letters outside any bracket pair are text that never went
70
- * through `t()`. The page is typed by the two methods used, so Playwright's
71
- * `Page` fits and nothing here depends on Playwright. This module imports no
72
- * runtime: the scanners run in the test runner, the page has its own copy.
73
- */
61
+ /** The two generated pseudo-locales (PLAN §4.7, §5.21). */
62
+ declare const PSEUDO_LOCALES: readonly ["en-XA", "ar-XB"];
63
+ type PseudoLocale = (typeof PSEUDO_LOCALES)[number];
74
64
  /** What the helpers call on a Playwright `Page` (or anything with the same methods). */
75
65
  interface PseudoPage {
76
66
  evaluate(fn: (arg: unknown) => unknown, arg?: unknown): Promise<unknown>;
@@ -109,8 +99,7 @@ export declare function scanPseudo(page: PseudoPage, options?: PseudoScanOptions
109
99
  * exist in `vite dev` and non-production builds that list them in
110
100
  * `pseudoLocales`.
111
101
  */
112
- export declare function setPseudoLocale(page: PseudoPage, locale?: "en-XA" | "ar-XB", options?: {
102
+ export declare function setPseudoLocale(page: PseudoPage, locale?: PseudoLocale, options?: {
113
103
  timeout?: number;
114
104
  }): Promise<void>;
115
- //#endregion
116
105
  export type { LoadCatalogOptions, PseudoLeak, PseudoPage, PseudoScanOptions, TestCatalog, TestI18n, TestI18nOptions, TestMode };
package/dist/test.js CHANGED
@@ -1,8 +1,8 @@
1
- import { c as page, n as S, p as subs, r as changed, t as textDirection } from "./dir-zw6bGMKQ.js";
2
- import { c as parseICU, i as normalizeSource, n as canonicalize, t as compileMessage } from "./compile-Cc97JaW2.js";
3
- import { t as createScopedApi } from "./scoped-BMYpSthj.js";
4
- import { t as pseudoLocalize } from "./pseudo-DRv30iN5.js";
5
- //#region src/test/catalog.ts
1
+ import { f as subs, n as S, r as changed, s as page, t as textDirection } from "./dir-TGLYuOZb.js";
2
+ import { a as ID_LENGTH, o as PSEUDO_LOCALES, r as DEFAULT_CONFIG, t as pseudoLocalize } from "./pseudo-B2DUpoLu.js";
3
+ import { c as parseICU, i as normalizeSource, n as canonicalize, t as compileMessage } from "./compile-DqkofbiO.js";
4
+ import { t as createScopedApi } from "./scoped-h3KE8XWW.js";
5
+ // src/test/catalog.ts
6
6
  /**
7
7
  * `loadCatalog` (INTERNALS §3.8, §6 D194): one locale's translations read
8
8
  * from the project's catalog files, as the build compiles them, for
@@ -26,14 +26,13 @@ function loadCatalog(locale, options = {}) {
26
26
  ]);
27
27
  let load = loads.get(key);
28
28
  if (load === void 0) {
29
- load = import("./load-CQRz4PpM.js").then((m) => m.compileCatalog(locale, root, configFile));
29
+ load = import("./load-SSrAeJVB.js").then((m) => m.compileCatalog(locale, root, configFile));
30
30
  loads.set(key, load);
31
31
  load.catch(() => loads.delete(key));
32
32
  }
33
33
  return load;
34
34
  }
35
- //#endregion
36
- //#region src/test/i18n.ts
35
+ // src/test/i18n.ts
37
36
  /**
38
37
  * `createTestI18n` (PLAN §5.22, INTERNALS §3.8): unit and component tests
39
38
  * render in a locale, with real or hand-written translations, with message
@@ -46,7 +45,7 @@ function loadCatalog(locale, options = {}) {
46
45
  * picks every message, because only it sees the descriptor (§6 D189).
47
46
  */
48
47
  const DEFAULT_NOW = Date.UTC(2026, 0, 1);
49
- const PSEUDO = /* @__PURE__ */ new Set(["en-XA", "ar-XB"]);
48
+ const PSEUDO = new Set(PSEUDO_LOCALES);
50
49
  /** What the page held before the first `createTestI18n`; null while nothing is applied. */
51
50
  let saved = null;
52
51
  const html = () => typeof document === "object" && document?.documentElement ? document.documentElement : null;
@@ -59,6 +58,7 @@ function resetTestI18n() {
59
58
  const s = saved;
60
59
  if (s === null) return;
61
60
  saved = null;
61
+ s.untaught?.();
62
62
  page.locale = s.locale;
63
63
  page.messages = s.messages;
64
64
  page.timeZone = s.timeZone;
@@ -83,27 +83,55 @@ function testRegistration(locale) {
83
83
  pseudoLocales: locales.filter((l) => PSEUDO.has(l)),
84
84
  fallbacks: {},
85
85
  routing: {
86
- strategy: ["default"],
86
+ strategy: [
87
+ "cookie",
88
+ "header",
89
+ "default"
90
+ ],
87
91
  routeStrategies: {},
88
92
  url: "no_prefix",
89
93
  redirectRoot: false,
90
94
  propagate: false,
91
95
  switch: "in-place",
92
- cookie: {
93
- name: "vi18n_locale",
94
- maxAge: 31536e3
95
- },
96
+ cookie: { ...DEFAULT_CONFIG.routing.cookie },
96
97
  base: "/"
97
98
  },
98
99
  ssr: {
99
- timeZone: "UTC",
100
+ timeZone: DEFAULT_CONFIG.ssr.timeZone,
100
101
  timeZoneFrom: {},
101
- normalizeSpaces: true
102
+ normalizeSpaces: DEFAULT_CONFIG.ssr.normalizeSpaces
102
103
  }
103
104
  },
104
- test: true
105
+ test: true,
106
+ serverCatalogs: Object.fromEntries(locales.map((l) => [l, async () => ({})]))
105
107
  };
106
108
  }
109
+ /**
110
+ * `/server` scopes open in any locale a test names, the pseudo-locales
111
+ * included, but a registration lists only the project's: teach it the
112
+ * others, with an empty catalog (`createTestI18n` supplies their text),
113
+ * and return how to forget them. Done here rather than in `/server`, whose
114
+ * size budget production servers pay.
115
+ */
116
+ function teachPseudo(reg) {
117
+ const undo = [];
118
+ const empty = async () => ({ default: {
119
+ messages: {},
120
+ labels: {}
121
+ } });
122
+ for (const l of PSEUDO_LOCALES) {
123
+ if (!reg.config.locales.includes(l)) {
124
+ reg.config.locales.push(l);
125
+ undo.push(() => reg.config.locales.splice(reg.config.locales.indexOf(l), 1));
126
+ }
127
+ const loaders = reg.serverCatalogs ?? (reg.catalogs ??= {});
128
+ if (!(l in loaders)) {
129
+ loaders[l] = reg.serverCatalogs ? async () => ({}) : empty;
130
+ undo.push(() => delete loaders[l]);
131
+ }
132
+ }
133
+ return () => undo.forEach((f) => f());
134
+ }
107
135
  function memo(cache, key, make) {
108
136
  let value = cache.get(key);
109
137
  if (value === void 0) {
@@ -155,18 +183,7 @@ function check(condition, message) {
155
183
  * a call, tests under the plugin render the source locale as before.
156
184
  */
157
185
  function createTestI18n(options = {}) {
158
- const mode = options.mode ?? "source";
159
- check([
160
- "source",
161
- "ids",
162
- "pseudo"
163
- ].includes(mode), `unknown test mode ${JSON.stringify(mode)}`);
164
- const timeZone = options.timeZone ?? "UTC";
165
- new Intl.DateTimeFormat("en", { timeZone });
166
- const now = options.now instanceof Date ? options.now.getTime() : options.now ?? DEFAULT_NOW;
167
- check(Number.isFinite(now), "now must be a finite time");
168
- const idLength = options.idLength ?? 12;
169
- check(Number.isInteger(idLength) && idLength >= 8 && idLength <= 16, "idLength must be an integer from 8 to 16");
186
+ const { mode, timeZone, now, idLength } = checkedOptions(options);
170
187
  const catalog = options.catalog;
171
188
  const requested = options.locale ?? catalog?.locale;
172
189
  if (catalog && requested !== catalog.locale) throw new RangeError(`vi18n: the catalog is for ${catalog.locale}, not ${requested}`);
@@ -177,6 +194,61 @@ function createTestI18n(options = {}) {
177
194
  Intl.getCanonicalLocales(locale);
178
195
  check(base === null || base.config.locales.includes(locale) || PSEUDO.has(locale), `unknown locale ${locale} (${base?.config.locales.join(", ")})`);
179
196
  const h = html();
197
+ const before = saveOnce(h);
198
+ register(base, locale, before);
199
+ page.locale = locale;
200
+ page.messages = catalog?.messages ?? before.messages;
201
+ page.timeZone = timeZone;
202
+ page.now = now;
203
+ page.test = testHook({
204
+ mode,
205
+ locale,
206
+ own,
207
+ idLength
208
+ });
209
+ if (h) Object.assign(h, {
210
+ lang: locale,
211
+ dir: textDirection(locale)
212
+ });
213
+ notify();
214
+ return {
215
+ ...createScopedApi(page),
216
+ mode,
217
+ reset: resetTestI18n
218
+ };
219
+ }
220
+ /**
221
+ * Without the plugin nothing registered: `register` records a test
222
+ * registration (and resets the page's locale, set by the caller after).
223
+ * Either way `/server` is taught the pseudo-locales, once per test file run.
224
+ */
225
+ function register(base, locale, before) {
226
+ if (base === null) S.register(testRegistration(locale));
227
+ before.untaught ??= S.registration?.test ? teachPseudo(S.registration) : null;
228
+ }
229
+ /** The options with their defaults, each checked; throws a RangeError naming the bad one. */
230
+ function checkedOptions(options) {
231
+ const mode = options.mode ?? "source";
232
+ check([
233
+ "source",
234
+ "ids",
235
+ "pseudo"
236
+ ].includes(mode), `unknown test mode ${JSON.stringify(mode)}`);
237
+ const timeZone = options.timeZone ?? DEFAULT_CONFIG.ssr.timeZone;
238
+ new Intl.DateTimeFormat("en", { timeZone });
239
+ const now = options.now instanceof Date ? options.now.getTime() : options.now ?? DEFAULT_NOW;
240
+ check(Number.isFinite(now), "now must be a finite time");
241
+ const idLength = options.idLength ?? ID_LENGTH.default;
242
+ check(Number.isInteger(idLength) && idLength >= ID_LENGTH.min && idLength <= ID_LENGTH.max, `idLength must be an integer from ${ID_LENGTH.min} to ${ID_LENGTH.max}`);
243
+ return {
244
+ mode,
245
+ timeZone,
246
+ now,
247
+ idLength
248
+ };
249
+ }
250
+ /** Records what the page holds before the first `createTestI18n`, once. */
251
+ function saveOnce(h) {
180
252
  saved ??= {
181
253
  locale: page.locale,
182
254
  messages: page.messages,
@@ -188,37 +260,26 @@ function createTestI18n(options = {}) {
188
260
  html: h && {
189
261
  lang: h.lang,
190
262
  dir: h.dir
191
- }
263
+ },
264
+ untaught: null
192
265
  };
193
- if (base === null) S.register(testRegistration(locale));
194
- const style = locale === "ar-XB" ? "ar-XB" : "en-XA";
195
- page.locale = locale;
196
- page.messages = catalog?.messages ?? saved.messages;
197
- page.timeZone = timeZone;
198
- page.now = now;
199
- page.test = (id, d, text) => {
266
+ return saved;
267
+ }
268
+ /** The scope's `test` hook: what every message renders as in this mode and locale. */
269
+ function testHook(input) {
270
+ const { mode, locale, own, idLength } = input;
271
+ return function(id, d, text) {
200
272
  const source = d?.message ?? text;
201
- if (mode === "ids") return d?.id ?? (page.messages.has(id) ? id : generatedId(canonicalOf(source), idLength));
202
- if (mode === "pseudo") return pseudo(source, style);
203
- if (page.locale === locale && own.size > 0) {
273
+ if (mode === "ids") return d?.id ?? (this.messages.has(id) ? id : generatedId(canonicalOf(source), idLength));
274
+ if (mode === "pseudo") return pseudo(source, this.locale === "ar-XB" ? "ar-XB" : "en-XA");
275
+ if (this.locale === locale && own.size > 0) {
204
276
  const m = own.get(id) ?? own.get(canonicalOf(source));
205
277
  if (m !== void 0) return m;
206
278
  }
207
- return page.messages.get(id) ?? d?.ir ?? compiled(source);
208
- };
209
- if (h) Object.assign(h, {
210
- lang: locale,
211
- dir: textDirection(locale)
212
- });
213
- notify();
214
- return {
215
- ...createScopedApi(page),
216
- mode,
217
- reset: resetTestI18n
279
+ return this.messages.get(id) ?? d?.ir ?? compiled(source);
218
280
  };
219
281
  }
220
- //#endregion
221
- //#region src/test/pseudo.ts
282
+ // src/test/pseudo.ts
222
283
  const LETTER = /\p{L}/u;
223
284
  const BIDI_CONTROL = /[\u202A-\u202E\u2066-\u2069\u200E\u200F]/g;
224
285
  /**
@@ -295,6 +356,8 @@ function pageText(arg) {
295
356
  async function scanPseudo(page, options = {}) {
296
357
  return findPseudoLeaks(await page.evaluate(pageText, [...options.ignore ?? []]), options);
297
358
  }
359
+ /** Playwright's rejection when the page navigated away during `evaluate`. */
360
+ const NAVIGATED = /context was destroyed|navigat/i;
298
361
  /**
299
362
  * Switches the page to a pseudo-locale through its own vi18n runtime
300
363
  * (`setLocale`, in place or by navigation, as the app's routing does it) and
@@ -308,12 +371,19 @@ async function setPseudoLocale(page, locale = "en-XA", options = {}) {
308
371
  const state = globalThis[Symbol.for("menapo.vi18n")];
309
372
  if (!state?.locales) return "vi18n is not loaded on this page";
310
373
  if (!state.locales.includes(l)) return `${l} is not an active locale here (${state.locales.join(", ")})`;
311
- await state.setLocale(l);
374
+ try {
375
+ await state.setLocale(l);
376
+ } catch (error) {
377
+ return { failed: error instanceof Error ? error.message : String(error) };
378
+ }
312
379
  return null;
313
- }, locale).catch(() => null);
380
+ }, locale).catch((error) => {
381
+ if (NAVIGATED.test(error instanceof Error ? error.message : String(error))) return null;
382
+ throw error;
383
+ });
384
+ if (typeof problem === "object" && problem !== null && "failed" in problem) throw new Error(`vi18n: setLocale(${JSON.stringify(locale)}) failed in the page: ${String(problem.failed)}`);
314
385
  if (typeof problem === "string") throw new Error(`vi18n: ${problem}; pseudo-locales exist in vite dev and in builds of another mode than production (vite build --mode staging) that list them in pseudoLocales`);
315
386
  await page.waitForFunction((arg) => document.documentElement.lang === arg, locale, options.timeout === void 0 ? {} : { timeout: options.timeout });
316
387
  await page.evaluate(() => globalThis[Symbol.for("menapo.vi18n")]?.ready());
317
388
  }
318
- //#endregion
319
389
  export { createTestI18n, findPseudoLeaks, loadCatalog, resetTestI18n, scanPseudo, setPseudoLocale };