Skip to content

Commit 4b4e003

Browse files
committed
fix(cli): i18n coverage reads a two-script inline map with the shared resolver
inlineLocaleText now narrows the map to the reader's language and asks resolveI18nLabel, so a zh-Hant reader is covered by its own script's entry rather than by the first zh-* key. Co-Authored-By: Claude <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01L9qibxSGAjHBvjATmenUGp
1 parent 7422324 commit 4b4e003

2 files changed

Lines changed: 31 additions & 20 deletions

File tree

‎packages/cli/src/utils/i18n-coverage.ts‎

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444

4545
import { deepMerge } from '@objectstack/core';
4646
import type { TranslationBundle, TranslationData } from '@objectstack/spec/system';
47+
import { resolveI18nLabel } from '@objectstack/spec/ui';
4748
import { collectExpectedEntries, type ExpectedEntry } from './i18n-bundle-extract.js';
4849

4950
export type CoverageSeverity = 'error' | 'warning';
@@ -518,15 +519,16 @@ function collectExpectedKeys(config: any): ExpectedKey[] {
518519
* as covered the moment a map exists, which is the mirror image of the bug
519520
* this function was added to fix: one answer standing for two opposite facts.
520521
*
521-
* So only the **tag-matching** limbs count as coverage, in the reference's own
522-
* order and with its own case rules:
523-
*
524-
* 1. the exact tag — `zh-CN` reads the key `zh-CN`;
525-
* 2. the base language — `zh-CN` reads the key `zh`;
526-
* 3. the first region-qualified sibling sharing that base — `zh` reads
527-
* `zh-CN`; `zh-TW` reads `zh-CN` too. Same language, other region: what
528-
* the renderer really shows, and a translation into the language asked
529-
* for.
522+
* So only the **tag-matching** limbs count as coverage: the entries whose
523+
* language subtag is the locale's (the exact tag, the bare language, a
524+
* region-qualified sibling). Which of them the reader sees is asked of
525+
* `resolveI18nLabel` itself, over the map cut down to those entries, so this
526+
* function keeps no copy of the resolver's limb order: `zh-CN` reads `zh-CN`,
527+
* else `zh`; `zh` reads `zh-CN`; `zh-TW` reads `zh-CN` when the map has no
528+
* Traditional entry, and `zh-TW` over `zh-CN` when it has both (the script
529+
* preference where the platform table splits a language). Same language,
530+
* other region: what the renderer really shows, and a translation into the
531+
* language asked for.
530532
*
531533
* The fallback limbs are excluded because falling back **is** what an
532534
* untranslated locale looks like: a `ja-JP` reader shown the `en` entry is
@@ -536,7 +538,8 @@ function collectExpectedKeys(config: any): ExpectedKey[] {
536538
* `i18n-label-resolver.ts` is the authority on the limb rule and on the case
537539
* asymmetry mirrored here (region case is irrelevant because limb 3 compares
538540
* language subtags only; language case is significant, because the reference
539-
* folds neither side). `inline-locale-coverage-parity.test.ts` pins this
541+
* folds neither side). The describe block "coverage reads the map the way the
542+
* renderer does" in `test/i18n-inline-locale-map-coverage.test.ts` pins this
540543
* function against `resolveI18nLabel`: whenever this says "covered", the
541544
* renderer really shows that entry.
542545
*
@@ -555,18 +558,14 @@ export function inlineLocaleText(
555558
return typeof value === 'string' && value.length > 0 ? value : undefined;
556559
};
557560
const tag = (locale || 'en').trim();
558-
const exact = read(tag);
559-
if (exact !== undefined) return exact;
560561
const base = tag.split('-')[0];
561-
const baseHit = read(base);
562-
if (baseHit !== undefined) return baseHit;
562+
const sameLanguage: Record<string, string> = {};
563563
for (const key of Object.keys(map)) {
564-
if (key.split('-')[0] === base) {
565-
const sibling = read(key);
566-
if (sibling !== undefined) return sibling;
567-
}
564+
const value = read(key);
565+
if (value !== undefined && key.split('-')[0] === base) sameLanguage[key] = value;
568566
}
569-
return undefined;
567+
if (Object.keys(sameLanguage).length === 0) return undefined;
568+
return resolveI18nLabel(sameLanguage, tag);
570569
}
571570

572571
/**

‎packages/cli/test/i18n-inline-locale-map-coverage.test.ts‎

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -272,8 +272,13 @@ describe('coverage reads the map the way the renderer does', () => {
272272
{ 'zh-CN': '成员' },
273273
{ default: 'Members', 'zh-CN': '成员' },
274274
{ en: 'Members' },
275+
// Both Chinese scripts, in either authoring order (#30910): the renderer
276+
// reads the entry of the reader's own script, and so does coverage.
277+
{ en: 'Members', 'zh-CN': '成员', 'zh-TW': '成員' },
278+
{ en: 'Members', 'zh-TW': '成員', 'zh-CN': '成员' },
279+
{ en: 'Members', zh: '成员', 'zh-Hant': '成員' },
275280
];
276-
const locales = ['en', 'zh', 'zh-CN', 'zh-TW', 'ja-JP', 'es-ES'];
281+
const locales = ['en', 'zh', 'zh-CN', 'zh-TW', 'zh-Hans', 'zh-Hant', 'zh-HK', 'ja-JP', 'es-ES'];
277282

278283
it('a covered locale is the entry the renderer picks', () => {
279284
for (const map of maps) {
@@ -300,4 +305,11 @@ describe('coverage reads the map the way the renderer does', () => {
300305
expect(inlineLocaleText({ 'zh-CN': '成员' }, 'zh-TW')).toBe('成员');
301306
expect(inlineLocaleText({ zh: '成员' }, 'zh-CN')).toBe('成员');
302307
});
308+
309+
it("a map with both Chinese scripts covers each reader with its own script's entry (#30910)", () => {
310+
expect(inlineLocaleText({ 'zh-CN': '成员', 'zh-TW': '成員' }, 'zh-Hant')).toBe('成員');
311+
expect(inlineLocaleText({ 'zh-TW': '成員', 'zh-CN': '成员' }, 'zh')).toBe('成员');
312+
// An empty entry of the reader's script is no translation: the other script still covers.
313+
expect(inlineLocaleText({ 'zh-CN': '成员', 'zh-TW': '' }, 'zh-Hant')).toBe('成员');
314+
});
303315
});

0 commit comments

Comments
 (0)