Repository navigation
Expand file tree
/
Copy pathi18n-coverage.ts
More file actions
734 lines (698 loc) · 31.7 KB
/
Copy pathi18n-coverage.ts
File metadata and controls
734 lines (698 loc) · 31.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
// Copyright (c) 2025 ObjectStack. Licensed under the Apache-2.0 license.
/**
* I18n Coverage Detector
*
* Compares the translation keys a stack *should* carry against the bundles
* actually attached to it, and reports the ones that are missing or empty.
*
* The expected set is **not** computed here. It comes from
* {@link collectExpectedEntries} in `i18n-extract.ts` — the same walker that
* scaffolds bundles for `os i18n extract`. That sharing is the point: this
* detector used to keep its own parallel walk, and the two drifted until whole
* declared surfaces were extractable but ungated — most visibly the action
* labels declared *inline on an object* (`sys_approval_request`'s
* Approve/Reject/Reassign), which shipped English into a zh-CN workspace with
* no lint ever noticing (#3370). One walker, one surface, no drift.
*
* The inline `label:` in the metadata is the *source* string, authored in the
* default locale: the runtime resolver falls back to it when a bundle carries
* no entry, and `os i18n extract` seeds bundles from it. So an inline label
* satisfies the default locale on its own — a bundle is what other locales
* need. Keys with no source string anywhere are not reported here; a missing
* label is `required/label`'s finding.
*
* An inline LOCALE MAP — `{ en: 'Members', 'zh-CN': '成员' }`, the second form
* `I18nLabelSchema` authorizes — is a source string in every locale it
* carries, and is read as one here (#14749, maintainer ruling 2026-09-03,
* Q2 = B1). It used to be read as nothing at all: the walker narrowed it to
* `undefined` on its way here, which is also what an absent prop produces, so
* a prop localised into four languages and a prop nobody wrote were the same
* input to this file. Locales the map carries now count as covered; locales it
* omits are gaps, on the same footing as a bundle that omits them. The map is
* still never extracted and never gets a bundle row — that half is the
* extractor's header, and ⛔ no key is invented from a node path.
*
* Which locales get checked is the project's call, never an assumption: the
* `i18n.supportedLocales` block declares them, and absent that block only the
* locales a bundle already exists for are checked. A project that does not
* translate therefore reports nothing at all — see {@link computeI18nCoverage}.
*
* Pure: no filesystem or network. Safe to invoke from `os lint`, `os i18n
* check`, IDE tooling, and unit tests.
*/
import type { TranslationBundle, TranslationData } from '@objectstack/spec/system';
import { collectExpectedEntries, type ExpectedEntry } from './i18n-extract.js';
export type CoverageSeverity = 'error' | 'warning';
export interface CoverageIssue {
severity: CoverageSeverity;
/** BCP-47 locale code where the key is missing. */
locale: string;
/** Dot-path of the missing key (e.g. `objects.account._views.all_accounts.label`). */
key: string;
/** Source kind the key was harvested from. */
source:
| 'object'
| 'field'
| 'option'
| 'section'
| 'view'
| 'action'
| 'globalAction'
| 'app'
| 'navigation'
| 'dashboard'
| 'widget'
| 'dataset'
| 'report'
| 'picklist'
| 'page'
| 'flow'
| 'position'
| 'permission'
| 'metadataForm';
/** Human-readable explanation. */
message: string;
}
export interface CoverageStats {
locale: string;
expected: number;
translated: number;
missing: number;
/** Coverage percent rounded to one decimal (0–100). */
coveragePercent: number;
}
export interface CoverageReport {
/** Locales discovered across all bundles attached to the stack. */
locales: string[];
/** Default / source-of-truth locale (errors are raised against this one). */
defaultLocale: string;
/** Per-locale coverage statistics. */
stats: CoverageStats[];
/** Per-issue listing (errors + warnings, locale-scoped). */
issues: CoverageIssue[];
/** Aggregate counts. */
totals: {
expectedKeys: number;
issues: number;
errors: number;
warnings: number;
};
/**
* What this run did with the registry-driven `metadataForms.*` baseline, and
* how many keys that decision moved.
*
* Reported rather than left implicit because the decision moves the
* **denominator**: `stats[].expected` and `coveragePercent` mean different
* things under the two modes, and a consumer reading a percentage out of
* `os i18n check --json` has no other way to tell which one it is holding.
* The console hint line is rendered from these same two numbers, so the two
* faces of the command cannot disagree about it.
*
* `excludedKeys` is `0` under `'included'` — never absent, so a machine
* consumer keying off presence never has to distinguish "counted them" from
* "this version does not tell me".
*/
platformMetadataForms: {
mode: PlatformMetadataFormsMode;
/** Authored platform keys dropped from the expected set (0 when included). */
excludedKeys: number;
};
}
/** The disposition a report actually reached — never `'auto'`, which is a request. */
export type PlatformMetadataFormsMode = 'included' | 'excluded';
/**
* What a caller asks for; {@link resolvePlatformMetadataForms} turns it into a
* {@link PlatformMetadataFormsMode}.
*
* - `'include'` — count the baseline. The **default**, because `os lint` is
* the other caller and it folds the baseline away at the REPORT seam
* instead, off `CoverageIssue['source']`, so it needs the issues to exist
* in order to count them for its `--include-platform` hint line. ⛔ Flipping
* this default would zero that hint silently; `i18n-platform-bucket.test.ts`
* pins the coupling.
* - `'exclude'` — drop it.
* - `'auto'` — drop it unless this stack authors it (see
* {@link stackAuthorsMetadataForms}).
*/
export type PlatformMetadataFormsOption = 'include' | 'exclude' | 'auto';
export interface CoverageOptions {
/**
* The locale that *must* be translated. Missing keys here surface as
* errors; missing keys in other locales surface as warnings. Defaults to
* `'en'`.
*/
defaultLocale?: string;
/**
* Restrict the check to this set of locales (in addition to the default
* locale). When omitted, every locale that appears in any bundle is
* checked.
*/
locales?: string[];
/**
* When `true`, missing keys in non-default locales are also reported as
* errors. Useful for CI gates that demand full translation parity.
*/
strict?: boolean;
/**
* How to treat the registry-driven `metadataForms.*` baseline. Defaults to
* `'include'` — see {@link PlatformMetadataFormsOption} for why that, and not
* `'auto'`, is the default at THIS seam.
*/
platformMetadataForms?: PlatformMetadataFormsOption;
}
// ─── Bundle helpers ────────────────────────────────────────────────────
function mergeData(target: TranslationData | undefined, source: TranslationData): TranslationData {
if (!target) return JSON.parse(JSON.stringify(source));
// shallow object merge across the four well-known sub-records is enough for
// coverage detection; we never need a deep merge of leaf strings because
// duplicates are accepted (last-write-wins).
const out: TranslationData = { ...target };
if (source.objects) {
out.objects = { ...(out.objects ?? {}) };
for (const [name, data] of Object.entries(source.objects)) {
out.objects[name] = {
...(out.objects[name] ?? {}),
...data,
fields: { ...(out.objects[name]?.fields ?? {}), ...(data.fields ?? {}) },
_views: { ...(out.objects[name]?._views ?? {}), ...(data._views ?? {}) },
_actions: { ...(out.objects[name]?._actions ?? {}), ...(data._actions ?? {}) },
} as any;
}
}
if (source.globalActions) {
out.globalActions = { ...(out.globalActions ?? {}), ...source.globalActions };
}
if (source.apps) out.apps = { ...(out.apps ?? {}), ...source.apps };
if (source.messages) out.messages = { ...(out.messages ?? {}), ...source.messages };
if ((source as any).metadataForms) {
const tgt: Record<string, any> = { ...((out as any).metadataForms ?? {}) };
for (const [type, data] of Object.entries((source as any).metadataForms)) {
const existing = tgt[type] ?? {};
const incoming = (data ?? {}) as any;
tgt[type] = {
...existing,
...incoming,
sections: { ...(existing.sections ?? {}), ...(incoming.sections ?? {}) },
fields: { ...(existing.fields ?? {}), ...(incoming.fields ?? {}) },
};
}
(out as any).metadataForms = tgt;
}
return out;
}
function flattenBundles(bundles: TranslationBundle[]): { merged: TranslationBundle; locales: string[] } {
const merged: Record<string, TranslationData> = {};
const localesSet = new Set<string>();
for (const bundle of bundles) {
if (!bundle || typeof bundle !== 'object') continue;
for (const [locale, data] of Object.entries(bundle)) {
if (!data || typeof data !== 'object') continue;
localesSet.add(locale);
merged[locale] = mergeData(merged[locale], data as TranslationData);
}
}
return { merged, locales: Array.from(localesSet).sort() };
}
// ─── Who owns the platform baseline ────────────────────────────────────
/**
* Does this stack author the registry-driven `metadataForms.*` baseline
* itself?
*
* ## Why the question is asked of the CONFIG and not of a flag
*
* The `metadataForms.*` family is not walked out of the stack under
* examination at all: {@link collectExpectedEntries} builds it from
* `METADATA_FORM_REGISTRY` + `DEFAULT_METADATA_TYPE_REGISTRY`, identically for
* every config, empty ones included — ~773 Studio-form keys. For an
* application that is somebody else's surface: `@objectstack/platform-objects`
* ships those translations and the runtime serves them, so an app-shipped copy
* would *override* the platform's and go stale at the next upgrade. Counting
* them against an app's coverage percentage therefore reports a debt the app
* must not pay, which is what made `--strict` / `--threshold` unusable for an
* app package — the two flags whose entire purpose is CI gating.
*
* An unconditional exclusion is the wrong repair and is deliberately not what
* this is. It would turn the app side green by deleting the gate on the side
* that *does* own those strings: `platform-objects`' own extract config carries
* `metadataForms` in every locale bundle it declares, and its coverage number
* is a real number about real work. So ownership is **observed**, from the one
* place it is already written down — the bundles the stack itself attaches.
* Ship the baseline and you are asked to complete it; ship none of it and it
* is not yours.
*
* A non-empty **string leaf** is the test, not the mere presence of the group:
* an empty `metadataForms: {}`, or a scaffold of empty strings, is what `os
* i18n extract --fill=empty` leaves behind before anyone translates anything,
* and reading that as a claim of ownership would hand an app the 773-key debt
* on the strength of a placeholder. That is the same rule {@link lookupKey}
* applies on every other bundle read: an empty translation is not a
* translation.
*/
export function stackAuthorsMetadataForms(config: any): boolean {
const bundles: unknown[] = Array.isArray(config?.translations) ? config.translations : [];
const hasText = (node: unknown): boolean => {
if (typeof node === 'string') return node.length > 0;
if (!node || typeof node !== 'object' || Array.isArray(node)) return false;
return Object.values(node as Record<string, unknown>).some(hasText);
};
for (const bundle of bundles) {
if (!bundle || typeof bundle !== 'object') continue;
for (const data of Object.values(bundle as Record<string, unknown>)) {
if (!data || typeof data !== 'object') continue;
if (hasText((data as Record<string, unknown>).metadataForms)) return true;
}
}
return false;
}
/** Turn a caller's request into the disposition a report will record. */
function resolvePlatformMetadataForms(
option: PlatformMetadataFormsOption | undefined,
config: any,
): PlatformMetadataFormsMode {
switch (option ?? 'include') {
case 'exclude':
return 'excluded';
case 'auto':
return stackAuthorsMetadataForms(config) ? 'included' : 'excluded';
default:
return 'included';
}
}
// ─── Expected key extraction ───────────────────────────────────────────
interface ExpectedKey {
source: CoverageIssue['source'];
/** Lookup path expressed as an array of segments. */
path: string[];
/** Friendly display key (joined with dots). */
displayKey: string;
/** Description shown in the issue message when the key is missing. */
context: string;
/**
* The source string the reader sees in the default locale, when the metadata
* authors one. This *is* the default-locale text — see `computeI18nCoverage`.
*/
inline?: string;
/**
* The inline locale map the author wrote at this prop, when they wrote one
* (`{ en: 'Members', 'zh-CN': '成员' }`) — the second authorized form of an
* `I18nLabel`, carried here verbatim by the walker.
*
* Present ⇒ this key IS authored, whatever `inline` says. See
* {@link inlineLocaleText} for how a locale is read out of it, and
* `computeI18nCoverage` for why that read is narrower than the renderer's.
*/
inlineLocales?: Readonly<Record<string, string>>;
}
/**
* Map the shared walker's fine-grained kinds onto the coverage taxonomy.
*
* The three registry-driven kinds all describe the same Studio metadata-form
* baseline, so they collapse to one `metadataForm` bucket — that is the bucket
* `os lint` hides wholesale unless `--include-platform` is passed.
*/
const COVERAGE_SOURCE: Record<ExpectedEntry['source'], CoverageIssue['source']> = {
object: 'object',
field: 'field',
option: 'option',
// An object section heading (`objects.<o>._sections.<s>.label`) is the
// user's own metadata, not the Studio-form baseline — it keeps its own
// bucket so `os lint` reports it as `i18n/missing-section` rather than
// folding it away with `--include-platform`.
section: 'section',
view: 'view',
action: 'action',
globalAction: 'globalAction',
app: 'app',
navigation: 'navigation',
dashboard: 'dashboard',
widget: 'widget',
// Analytics dataset copy (`datasets.<d>.label`, `.description`, and each
// dimension's / measure's `label`) — the author's own semantic layer, drawn
// under every metric tile and on every chart axis, so it keeps its own
// bucket and reports as `i18n/missing-dataset` rather than folding away with
// `--include-platform`. A dataset is bound BY REFERENCE from N widgets
// across M dashboards (ADR-0021 D1), which is also why it is not folded into
// the `dashboard` bucket: the string is defined once, not once per
// presentation.
dataset: 'dataset',
// Report copy (`reports.<r>.label` / `.description`) — the author's own
// reports, listed by these two strings on the app's report index and headed
// by the label on the report page, so it keeps its own bucket and reports as
// `i18n/missing-report` rather than folding into `dashboard`.
report: 'report',
// Shared option-list copy (`picklists.<p>.label`, `.options.<value>`) — the
// author's own vocabulary, translated once for every field that references
// it, so it keeps its own bucket and reports as `i18n/missing-picklist`.
picklist: 'picklist',
page: 'page',
// Screen-flow copy (`flows.<f>.label`, `flows.<f>.screens.<n>.title` /
// `.description` (#22507), and the per-field `label` / `placeholder` /
// option labels), the terminal
// toasts (`flows.<f>.successMessage` / `.errorMessage`, #22507) and a refused
// `end` node's message (`flows.<f>.refusals.<n>.message`, #22450), and an
// approval flow's label with each `approval` node's step label
// (`flows.<f>.approvals.<n>.label`, #24916) — the author's own text, so
// it keeps its own bucket and reports as `i18n/missing-flow` rather than
// folding away with `--include-platform`. Until this bucket existed the
// family could not report a screen-flow gap at all: HotCRM measured
// `0 i18n/missing-*` while six screen dialogs rendered English in all four
// locales (#11485). Its six approval flows stayed that invisible until
// #24916.
flow: 'flow',
// Security-catalog copy (`positions.<p>.label` / `.description`,
// `permissions.<s>.label` / `.description`) — the author's own positions and
// permission sets, read in Setup and on every approval step that waits on a
// position, so each keeps its own bucket and reports as
// `i18n/missing-position` / `i18n/missing-permission`.
position: 'position',
permission: 'permission',
metadataType: 'metadataForm',
metadataFormSection: 'metadataForm',
metadataFormField: 'metadataForm',
};
const SOURCE_NOUN: Record<CoverageIssue['source'], string> = {
object: 'Object',
field: 'Field',
option: 'Option',
section: 'Section',
view: 'View',
action: 'Action',
globalAction: 'Global action',
app: 'App',
navigation: 'Navigation item',
dashboard: 'Dashboard',
widget: 'Widget',
dataset: 'Dataset',
report: 'Report',
picklist: 'Picklist',
page: 'Page',
flow: 'Flow',
position: 'Position',
permission: 'Permission set',
metadataForm: 'Metadata form',
};
/**
* The same taxonomy, spelled as the plural surface noun a *reader* of
* `os i18n check --help` needs — `SOURCE_NOUN` above is the singular subject of
* a per-issue message ("Object \"account\" fields.name.label"), which is a
* different sentence and cannot serve both.
*
* ⚠️ This is the SET, not a sample. `os i18n check --help` used to name five of
* these kinds in a parenthetical, and a five-item sample of a fifteen-member
* taxonomy does not read as an illustration — it reads as a scope statement, so
* a reader who wanted app navigation or dashboard widgets checked concluded the
* command did not cover them and went looking for a second tool that does not
* exist. The capability was shipped and hidden by its own description.
*
* The `Record<CoverageIssue['source'], string>` type is what keeps that from
* happening again: it is exhaustive by construction, so a new member of the
* union is a **compile error** here until it is named, and naming it publishes
* it in `--help` in the same edit. ⛔ Never widen this to a partial map or an
* index signature — that restores exactly the drift this file measured.
*
* Kinds that fold together do so here too, because they fold in the *report*:
* a filter-preset tab is reported as `view`, an action parameter as `action`,
* and the three registry-driven metadata-form kinds as `metadataForm`.
*/
const SOURCE_SURFACE: Record<CoverageIssue['source'], string> = {
object: 'objects',
field: 'fields',
option: 'options',
section: 'sections',
view: 'views',
action: 'actions',
globalAction: 'global actions',
app: 'apps',
navigation: 'navigation items',
dashboard: 'dashboards',
widget: 'widgets',
dataset: 'datasets',
report: 'reports',
picklist: 'picklists',
page: 'pages',
flow: 'flow screens, approval steps, toasts and refusals',
position: 'positions',
permission: 'permission sets',
metadataForm: 'metadata forms',
};
/**
* Every source kind a {@link CoverageIssue} can carry, in declaration order.
* Derived from {@link SOURCE_SURFACE}, never retyped.
*/
export const COVERAGE_SOURCE_KINDS = Object.keys(SOURCE_SURFACE) as ReadonlyArray<
CoverageIssue['source']
>;
/**
* The translatable surfaces this detector reports on, as one comma-separated
* phrase — the text `os i18n check --help` shows.
*
* Derived from the taxonomy rather than retyped beside it: the command's
* description and the kinds its report can emit are then one fact with one
* source, and cannot drift apart the way they did.
*/
export const COVERAGE_SURFACE_PHRASE = COVERAGE_SOURCE_KINDS.map((kind) => SOURCE_SURFACE[kind]).join(', ');
/** Subject line for the "missing translation" message. */
function describeEntry(entry: ExpectedEntry, source: CoverageIssue['source']): string {
const noun = SOURCE_NOUN[source];
const owner = entry.objectName ?? entry.appName ?? entry.metadataType ?? entry.flowName;
// Everything past the owning collection and its name reads as the attribute
// path: `objects.account.fields.name.label` → `fields.name.label`.
const attribute = entry.path.slice(2).join('.');
return owner && attribute ? `${noun} "${owner}" ${attribute}` : `${noun} ${entry.path.join('.')}`;
}
/**
* Every key a bundle *may* carry, paired with the text the metadata already
* shows for it. Sourced from the extractor's walker so the gated surface and
* the scaffolded surface can never disagree. Callers drop the keys that are
* authored nowhere — see {@link computeI18nCoverage}.
*/
function collectExpectedKeys(config: any): ExpectedKey[] {
return collectExpectedEntries(config).map((entry) => {
const source = COVERAGE_SOURCE[entry.source];
return {
source,
path: entry.path,
displayKey: entry.path.join('.'),
context: describeEntry(entry, source),
inline: entry.inline,
inlineLocales: entry.inlineLocales,
};
});
}
// ─── Lookup ────────────────────────────────────────────────────────────
/**
* The text an inline locale map holds **for `locale` specifically** — or
* `undefined`, which is this detector's word for "not translated here".
*
* ## Why this is narrower than the renderer's rule, deliberately
*
* `resolveI18nLabel` (and objectui's `pickLocalized`, which it is pinned to
* limb for limb) answers a different question: *what should I put on screen
* for this reader?* Its six limbs therefore end in three fallbacks — the
* untagged `default` entry, the `en` entry, then any string in the map — so it
* essentially never misses. Reading coverage off it would report every locale
* as covered the moment a map exists, which is the mirror image of the bug
* this function was added to fix: one answer standing for two opposite facts.
*
* So only the **tag-matching** limbs count as coverage, in the reference's own
* order and with its own case rules:
*
* 1. the exact tag — `zh-CN` reads the key `zh-CN`;
* 2. the base language — `zh-CN` reads the key `zh`;
* 3. the first region-qualified sibling sharing that base — `zh` reads
* `zh-CN`; `zh-TW` reads `zh-CN` too. Same language, other region: what
* the renderer really shows, and a translation into the language asked
* for.
*
* The fallback limbs are excluded because falling back **is** what an
* untranslated locale looks like: a `ja-JP` reader shown the `en` entry is
* precisely the gap this gate exists to report, and `default` is the entry an
* author writes for locales they did **not** translate.
*
* `i18n-label-resolver.ts` is the authority on the limb rule and on the case
* asymmetry mirrored here (region case is irrelevant because limb 3 compares
* language subtags only; language case is significant, because the reference
* folds neither side). `inline-locale-coverage-parity.test.ts` pins this
* function against `resolveI18nLabel`: whenever this says "covered", the
* renderer really shows that entry.
*
* Empty values do not count, matching {@link lookupKey}'s rule on the bundle
* side — an empty translation is not a translation, whichever side it is
* written on.
*/
export function inlineLocaleText(
map: Readonly<Record<string, string>> | undefined,
locale: string,
): string | undefined {
if (!map) return undefined;
const read = (tag: string): string | undefined => {
if (!Object.prototype.hasOwnProperty.call(map, tag)) return undefined;
const value = map[tag];
return typeof value === 'string' && value.length > 0 ? value : undefined;
};
const tag = (locale || 'en').trim();
const exact = read(tag);
if (exact !== undefined) return exact;
const base = tag.split('-')[0];
const baseHit = read(base);
if (baseHit !== undefined) return baseHit;
for (const key of Object.keys(map)) {
if (key.split('-')[0] === base) {
const sibling = read(key);
if (sibling !== undefined) return sibling;
}
}
return undefined;
}
/**
* Any text the inline map holds at all — the test for "did the author write
* something here", used only to decide whether the key is worth reporting on.
*
* ⛔ Not a coverage answer: see {@link inlineLocaleText} for that.
*/
function inlineLocaleAny(map: Readonly<Record<string, string>> | undefined): string | undefined {
if (!map) return undefined;
for (const value of Object.values(map)) {
if (typeof value === 'string' && value.length > 0) return value;
}
return undefined;
}
function lookupKey(data: TranslationData | undefined, path: string[]): string | undefined {
let current: any = data;
for (const segment of path) {
if (!current || typeof current !== 'object') return undefined;
current = current[segment];
}
return typeof current === 'string' && current.length > 0 ? current : undefined;
}
// ─── Public API ────────────────────────────────────────────────────────
/**
* Compute a coverage report for a normalized stack config.
*/
export function computeI18nCoverage(config: any, opts: CoverageOptions = {}): CoverageReport {
// Locale selection, most specific first: an explicit caller override, then
// the project's own `i18n` block, then 'en'. Reading the config matters for a
// monolingual non-English project — forcing 'en' on a stack whose source
// strings are Chinese would report a language it never claimed to speak.
const declared = config?.i18n;
const declaredLocales: string[] = Array.isArray(declared?.supportedLocales)
? declared.supportedLocales.filter((l: unknown): l is string => typeof l === 'string' && l.length > 0)
: [];
const defaultLocale =
opts.defaultLocale ??
(typeof declared?.defaultLocale === 'string' && declared.defaultLocale.length > 0
? declared.defaultLocale
: 'en');
const bundles: TranslationBundle[] = Array.isArray(config?.translations) ? config.translations : [];
const { merged, locales: discovered } = flattenBundles(bundles);
// A project only owes translations for locales it opted into. `supportedLocales`
// is that opt-in; without it the check falls back to the locales some bundle
// already exists for. Declare neither — the monolingual case — and the only
// active locale is the default one, which every inline label already satisfies,
// so the gate reports nothing rather than inventing a translation debt.
let activeLocales: string[];
if (opts.locales && opts.locales.length > 0) {
activeLocales = Array.from(new Set<string>([defaultLocale, ...opts.locales]));
} else if (declaredLocales.length > 0) {
activeLocales = Array.from(new Set<string>([defaultLocale, ...declaredLocales, ...discovered]));
} else if (discovered.length === 0) {
activeLocales = [defaultLocale];
} else {
activeLocales = discovered.includes(defaultLocale) ? discovered : [defaultLocale, ...discovered];
}
// A key is only worth translating if a source string is authored somewhere:
// inline in the metadata, or in some bundle (a project may externalize a
// string it never wrote inline — other locales still owe a translation for
// it). A key authored in neither place has no text to translate at all; a
// missing label is `required/label`'s finding, not an i18n gap.
//
// An inline LOCALE MAP is authored text too (#14749, maintainer ruling
// 2026-09-03, Q2 = B1). It used to fail this test — the walker narrowed a
// map to `undefined` on its way here, the same value an absent prop
// produces — so a prop written out in four languages was dropped from the
// expected set exactly as if nobody had written it, and a bundle entry for
// one of its locales pulled it back in and then reported every OTHER locale
// as missing. Both readings were the same defect: one diagnostic standing
// for two opposite facts.
const authoredInBundle = (path: string[]): boolean =>
Object.values(merged).some((data) => lookupKey(data, path) !== undefined);
const authored = collectExpectedKeys(config).filter(
(key) =>
key.inline !== undefined
|| inlineLocaleAny(key.inlineLocales) !== undefined
|| authoredInBundle(key.path),
);
// The platform baseline is dropped from the POPULATION, not from the issue
// list, because this report's headline number is a percentage: an app that
// has translated every string it owns reads 38.9% while 773 of its 1265
// "expected" keys belong to `@objectstack/platform-objects`. `os lint` folds
// the same family away one seam later (`foldCoverageIssues`, keyed on
// `CoverageIssue['source']`) and can afford to, because it reports findings
// and never a denominator.
const platformMode = resolvePlatformMetadataForms(opts.platformMetadataForms, config);
const expected =
platformMode === 'included' ? authored : authored.filter((key) => key.source !== 'metadataForm');
const excludedPlatformKeys = authored.length - expected.length;
const issues: CoverageIssue[] = [];
const stats: CoverageStats[] = [];
for (const locale of activeLocales) {
const data = merged[locale];
let translated = 0;
for (const key of expected) {
// Three sources, most specific first.
//
// 1. The bundle entry for this locale.
// 2. The inline locale map's entry FOR THIS LOCALE — tag-matched, never
// the renderer's fallback limbs (see `inlineLocaleText`). A locale
// the map carries is translated; one it does not is a gap, which is
// the whole of what the B1 ruling asks this gate to be able to say.
// 3. The inline `label:` IS the default-locale text — the runtime
// resolver falls back to it (i18n-resolver `translateObject`), and
// `os i18n extract` seeds bundles from it. Demanding a default-locale
// bundle entry that merely restates it reports a gap that does not
// exist. A map satisfies the default locale on the same grounds and
// by the same reading: whatever it resolves to there IS the source
// text a reader of the default locale sees, so `inlineLocaleAny`
// stands in for `inline` when the author wrote a map instead of a
// string.
const value = lookupKey(data, key.path)
?? inlineLocaleText(key.inlineLocales, locale)
?? (locale === defaultLocale ? (key.inline ?? inlineLocaleAny(key.inlineLocales)) : undefined);
if (value !== undefined) {
translated += 1;
continue;
}
const isError = locale === defaultLocale || opts.strict === true;
issues.push({
severity: isError ? 'error' : 'warning',
locale,
key: key.displayKey,
source: key.source,
message: `${key.context} missing translation for locale "${locale}"`,
});
}
const missing = expected.length - translated;
stats.push({
locale,
expected: expected.length,
translated,
missing,
coveragePercent: expected.length === 0 ? 100 : Math.round((translated / expected.length) * 1000) / 10,
});
}
const errors = issues.filter((i) => i.severity === 'error').length;
const warnings = issues.length - errors;
return {
locales: activeLocales,
defaultLocale,
stats,
issues,
totals: {
expectedKeys: expected.length,
issues: issues.length,
errors,
warnings,
},
platformMetadataForms: { mode: platformMode, excludedKeys: excludedPlatformKeys },
};
}