Skip to content

Commit 7228816

Browse files
committed
fix(i18n): prefer the session's own script when an inline label map holds both Chinese scripts
pickLocalized and its write-side twin localeWriteKey now share one limb function that, for a language the platform locale table splits by script (zh-Hans / zh-Hant), prefers a key meaning the session's own table tag (read through platformLocaleTagOf) before falling back to the first sibling. The spec twin resolveI18nLabel takes the same limb so the parity between the two resolvers holds. Maps without a same-script entry and every other language resolve exactly as before. Co-Authored-By: Claude <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01L9qibxSGAjHBvjATmenUGp
1 parent 0e88208 commit 7228816

9 files changed

Lines changed: 362 additions & 32 deletions

File tree

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
---
2+
'@object-ui/i18n': patch
3+
---
4+
5+
fix(i18n): a Traditional Chinese session reads and edits the Traditional entry of an inline label map
6+
7+
Clause-②: no
8+
Metadata-protocol: none
9+
10+
`pickLocalized`, `setLocalized` and `clearLocalized` now prefer an entry of the session's own script where the platform locale table splits a language by script. In a `zh-Hant` (or `zh-TW`, `zh-HK`) session, a label written as `{ en: 'Customer', 'zh-CN': '客户', 'zh-TW': '客戶' }` shows `客戶` and an edit replaces the `zh-TW` entry; before, both took the first `zh-*` key in authoring order, so a Traditional session showed the Simplified text and an edit overwrote it. A `zh-Hans`, `zh-CN` or bare `zh` session prefers the Simplified entry the same way.
11+
12+
Which tag a key means comes from `platformLocaleTagOf` in `@objectstack/spec/system`, and the preference applies only to a language the table names more than once (`zh` today). A map without an entry of the session's script, and every other language, resolve exactly as before. What an author can write is unchanged.
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
---
2+
'@objectstack/spec': patch
3+
---
4+
5+
fix(spec): `resolveI18nLabel` prefers an inline label entry of the locale's own script
6+
7+
Clause-②: no
8+
Metadata-protocol: none
9+
10+
Where the platform locale table splits a language by script (`zh-Hans`, `zh-Hant`), `resolveI18nLabel` now reads an entry of the requested locale's script before an entry of the other one: `zh-Hant`, `zh-TW` or `zh-HK` over `{ 'zh-CN': '客户', 'zh-TW': '客戶' }` answers `客戶` in any authoring order, and `zh`, `zh-CN` or `zh-Hans` answers `客户`. It used to take the first `zh-*` key. The script a key means is read from `platformLocaleTagOf`. A map without an entry of the locale's script, and every language the table names once, resolve exactly as before. This keeps the resolver limb for limb with objectui's `pickLocalized`, which takes the same change. The `InlineLocaleMapSchema` keys an author can write are unchanged.

‎packages/spec/src/ui/i18n-label-resolver.test.ts‎

Lines changed: 88 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,12 @@
1414
* ## How the expectations were derived
1515
*
1616
* `pickLocalizedReference` below is a VERBATIM copy of the reference
17-
* implementation, taken from
17+
* implementation (with its two private helpers), taken from
1818
*
19-
* repo objectstack-ai/objectui
20-
* path packages/i18n/src/pickLocalized.ts
21-
* rev origin/main d8d0d665dceba53dada4994f1eeef9f83bf1cf91
22-
* blob 30fcb0a86343b9b937432a1b2ea89e0a78321f46
23-
* last touched by objectui PR #4359 / objectui#3907 (2026-08-11)
19+
* repo objectstack-ai/objectstack
20+
* path ui/packages/i18n/src/pickLocalized.ts
21+
* blob 37b88c07aebfca42ce375c1e5b653f8ea26765c8
22+
* last touched by #30910 (the script-preference limb)
2423
*
2524
* Copied rather than imported because `@objectstack/spec` must not take a
2625
* workspace dependency on objectui — spec sits UNDER objectui in the dependency
@@ -48,11 +47,37 @@
4847
import { describe, it, expect } from 'vitest';
4948
import { resolveI18nLabel } from './i18n-label-resolver';
5049
import type { I18nLabel } from './i18n.zod';
50+
import { PLATFORM_LOCALE_TAGS, platformLocaleTagOf } from '../system/platform-locales';
5151

5252
// ---------------------------------------------------------------------------
5353
// The reference implementation — verbatim, see the header for provenance. Only
54-
// the NAME differs, so that a reader of a failing assertion can tell at a glance
55-
// which side is the copy.
54+
// the NAMES differ (a `Reference` suffix), so that a reader of a failing
55+
// assertion can tell at a glance which side is the copy.
56+
function splitScriptTagOfReference(lang: string): string | undefined {
57+
const tag = platformLocaleTagOf(lang);
58+
if (tag === undefined) return undefined;
59+
const base = tag.split('-')[0];
60+
return PLATFORM_LOCALE_TAGS.filter((t) => t.split('-')[0] === base).length > 1 ? tag : undefined;
61+
}
62+
63+
function localizedEntryKeyReference(
64+
lang: string,
65+
usable: (key: string) => boolean,
66+
keys: readonly string[],
67+
): string | undefined {
68+
if (usable(lang)) return lang;
69+
const base = lang.split('-')[0];
70+
const siblings = keys.filter((k) => k.split('-')[0] === base && usable(k));
71+
const script = splitScriptTagOfReference(lang);
72+
if (script !== undefined) {
73+
if (usable(base) && platformLocaleTagOf(base) === script) return base;
74+
const sameScript = siblings.find((k) => platformLocaleTagOf(k) === script);
75+
if (sameScript !== undefined) return sameScript;
76+
}
77+
if (usable(base)) return base;
78+
return siblings[0];
79+
}
80+
5681
function pickLocalizedReference(value: unknown, language: string | undefined | null): string {
5782
if (value == null) return '';
5883
if (typeof value === 'string') return value;
@@ -67,14 +92,11 @@ function pickLocalizedReference(value: unknown, language: string | undefined | n
6792
return typeof entry === 'string' ? entry : undefined;
6893
};
6994
const lang = (language || 'en').trim();
70-
const base = lang.split('-')[0];
7195
// Runtime language is often a bare base code ('zh') while metadata authors
72-
// write full BCP-47 tags ('zh-CN') — upgrade to any key sharing the base.
73-
const regional = Object.keys(o).find((k) => k.split('-')[0] === base && readReference(k) !== undefined);
96+
// write full BCP-47 tags ('zh-CN') — upgrade to a key sharing the base.
97+
const entry = localizedEntryKeyReference(lang, (k) => readReference(k) !== undefined, Object.keys(o));
7498
const pick =
75-
readReference(lang) ??
76-
readReference(base) ??
77-
(regional !== undefined ? readReference(regional) : undefined) ??
99+
(entry !== undefined ? readReference(entry) : undefined) ??
78100
readReference('default') ??
79101
readReference('en') ??
80102
Object.values(o).find((v): v is string => typeof v === 'string');
@@ -123,10 +145,62 @@ const PARITY_VECTORS: readonly ParityVector[] = [
123145
{ limb: '3 base key wins over the region upgrade', label: { zh: '基础', 'zh-CN': '区域' }, locale: 'zh', pick: '基础' },
124146
{
125147
limb: '3 first sibling in key order wins, not the "best" region',
148+
label: { 'pt-PT': 'Proprietário (PT)', 'pt-BR': 'Proprietário' },
149+
locale: 'pt',
150+
pick: 'Proprietário (PT)',
151+
},
152+
{
153+
limb: '3 first sibling in key order wins for a language the table names once (es-ES aliases es)',
154+
label: { 'es-MX': 'Propietario (MX)', 'es-ES': 'Propietario' },
155+
locale: 'es',
156+
pick: 'Propietario (MX)',
157+
},
158+
159+
// Limbs 2–3, script first (#30910) — where the platform table splits the
160+
// language by script (`zh-Hans`, `zh-Hant`), an entry of the locale's own
161+
// script wins over the other script, whatever the authoring order.
162+
{
163+
limb: '2–3 bare zh means zh-Hans: the Simplified sibling wins over an earlier Traditional one',
126164
label: { 'zh-TW': '擁有者', 'zh-CN': '负责人' },
127165
locale: 'zh',
166+
pick: '负责人',
167+
},
168+
{
169+
limb: '2–3 zh-Hant reads zh-TW over an earlier zh-CN',
170+
label: { en: 'Owner', 'zh-CN': '负责人', 'zh-TW': '擁有者' },
171+
locale: 'zh-Hant',
128172
pick: '擁有者',
129173
},
174+
{
175+
limb: '2–3 zh-HK reads zh-Hant over an earlier zh-Hans',
176+
label: { en: 'Owner', 'zh-Hans': '负责人', 'zh-Hant': '擁有者' },
177+
locale: 'zh-HK',
178+
pick: '擁有者',
179+
},
180+
{
181+
limb: '2–3 zh-TW skips a bare zh key (it means zh-Hans) for zh-Hant',
182+
label: { zh: '负责人', 'zh-Hant': '擁有者' },
183+
locale: 'zh-TW',
184+
pick: '擁有者',
185+
},
186+
{
187+
limb: '2–3 zh-CN keeps the bare zh key when it means the same script',
188+
label: { 'zh-Hans': '简体', zh: '负责人' },
189+
locale: 'zh-CN',
190+
pick: '负责人',
191+
},
192+
{
193+
limb: '2–3 no entry of the locale\'s script ⇒ the old limbs answer',
194+
label: { en: 'Owner', 'zh-CN': '负责人' },
195+
locale: 'zh-Hant',
196+
pick: '负责人',
197+
},
198+
{
199+
limb: '2–3 a script-subtag spelling the alias table does not name is not guessed at',
200+
label: { 'zh-CN': '负责人', 'zh-Hant-US': '擁有者' },
201+
locale: 'zh-Hant',
202+
pick: '负责人',
203+
},
130204
{
131205
limb: '3 runs BEFORE default — a wrong-region hit beats the untagged entry',
132206
label: { default: 'Owner', 'fr-FR': 'Propriétaire' },

‎packages/spec/src/ui/i18n-label-resolver.ts‎

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,9 @@
3131
* with neither side erroring — the server-rendered column header and the
3232
* client-rendered one simply differ.
3333
*
34-
* The reference is objectui `packages/i18n/src/pickLocalized.ts` (read at
35-
* `origin/main` `d8d0d66`, blob `30fcb0a8`, last touched by objectui PR #4359 /
36-
* objectui#3907). Its rule, mirrored here limb for limb:
34+
* The reference is `ui/packages/i18n/src/pickLocalized.ts` in this repository
35+
* (the blob is pinned in `i18n-label-resolver.test.ts`, last touched by #30910).
36+
* Its rule, mirrored here limb for limb:
3737
*
3838
* | # | limb | note |
3939
* |---|---|---|
@@ -42,6 +42,7 @@
4242
* | 1 | exact tag — `map[locale]` | `zh-CN` matches the key `zh-CN` |
4343
* | 2 | base language — `map[base]` | `zh-CN` → `zh` |
4444
* | 3 | first region-qualified sibling sharing the base | `zh` → `zh-CN`; **key insertion order** decides |
45+
* | 2–3 | …an entry of the locale's own script first | `zh-Hant` over `{ zh-CN, zh-TW }` → `zh-TW`; see {@link scriptEntryKey} |
4546
* | 4 | `map.default` | the untagged entry `InlineLocaleMapSchema` admits |
4647
* | 5 | `map.en` | the platform's source language |
4748
* | 6 | first string value | insertion order again |
@@ -64,6 +65,11 @@
6465
* no key of that map on limbs 1, 2 or 3, so the request falls through to
6566
* `default` / `en` / any-string and can land in the wrong language entirely.
6667
*
68+
* The one place case folds is the script preference of limbs 2–3, because it
69+
* asks {@link platformLocaleTagOf}, which folds: `zh-hant` prefers a `zh-TW`
70+
* key. It only reorders keys the base comparison above already admitted, so
71+
* `ZH-HANT` still matches no `zh-*` key at all.
72+
*
6773
* BCP-47 says subtags are case-insensitive, so a stricter reading would fold
6874
* case — but parity with the reference is the ruled acceptance, and a resolver
6975
* that folded case would answer differently from the renderer for exactly the
@@ -138,6 +144,33 @@
138144
*/
139145

140146
import type { I18nLabel } from './i18n.zod';
147+
import { PLATFORM_LOCALE_TAGS, platformLocaleTagOf } from '../system/platform-locales';
148+
149+
/**
150+
* Limbs 2–3's script preference, shared limb for limb with `pickLocalized`'s
151+
* `localizedEntryKey`: the key of `map` that means the same platform table
152+
* tag as `tag`, or `undefined`.
153+
*
154+
* It answers only where the platform table splits the base language into more
155+
* than one tag (`zh` into `zh-Hans` and `zh-Hant`), and reads both facts off
156+
* the table: which tag a code means is {@link platformLocaleTagOf} (`zh-TW`
157+
* means `zh-Hant`, `zh-CN` and bare `zh` mean `zh-Hans`). The bare base key
158+
* comes first when it means that tag; otherwise the first sibling that does,
159+
* in key insertion order. A base the table names once (`pt`, `en`), or a map
160+
* with no entry of the locale's script, answers `undefined`, and limbs 2 and
161+
* 3 then run exactly as they always did.
162+
*/
163+
function scriptEntryKey(map: Record<string, unknown>, tag: string, base: string): string | undefined {
164+
const script = platformLocaleTagOf(tag);
165+
if (script === undefined) return undefined;
166+
const scriptBase = script.split('-')[0];
167+
if (PLATFORM_LOCALE_TAGS.filter((t) => t.split('-')[0] === scriptBase).length < 2) return undefined;
168+
if (readEntry(map, base) !== undefined && platformLocaleTagOf(base) === script) return base;
169+
return Object.keys(map).find(
170+
(key) =>
171+
key.split('-')[0] === base && readEntry(map, key) !== undefined && platformLocaleTagOf(key) === script,
172+
);
173+
}
141174

142175
/**
143176
* The locale `pickLocalized` assumes when the caller has none — and the last
@@ -239,6 +272,12 @@ export function resolveI18nLabel(
239272
const exact = readEntry(map, tag);
240273
if (exact !== undefined) return exact;
241274

275+
// 2–3, script first — in a locale whose language the platform table splits
276+
// by script, an entry of the locale's own script (`zh-Hant` over
277+
// `{ zh-CN, zh-TW }` reads `zh-TW`) before any entry of the other one.
278+
const sameScript = scriptEntryKey(map, tag, base);
279+
if (sameScript !== undefined) return readEntry(map, sameScript);
280+
242281
// 2 — base language (`zh-CN` → `zh`).
243282
const baseHit = readEntry(map, base);
244283
if (baseHit !== undefined) return baseHit;

‎ui/packages/i18n/src/__tests__/clearLocalized.test.ts‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,3 +115,16 @@ describe('clearLocalized — paired with setLocalized', () => {
115115
expect(clearLocalized(stored, 'en')).toEqual({ 'zh-CN': '收入', bogus: 42 });
116116
});
117117
});
118+
119+
describe('clearLocalized — a script-split language clears the entry of its own script (#30910)', () => {
120+
it('a zh-Hant clear removes zh-TW and keeps the Simplified entry, in either authoring order', () => {
121+
expect(clearLocalized({ en: 'Customer', 'zh-CN': '客户', 'zh-TW': '客戶' }, 'zh-Hant')).toEqual({
122+
en: 'Customer',
123+
'zh-CN': '客户',
124+
});
125+
expect(clearLocalized({ en: 'Customer', 'zh-TW': '客戶', 'zh-CN': '客户' }, 'zh')).toEqual({
126+
en: 'Customer',
127+
'zh-TW': '客戶',
128+
});
129+
});
130+
});

‎ui/packages/i18n/src/__tests__/pickLocalized.test.ts‎

Lines changed: 74 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,8 @@ describe('pickLocalized — controls: every real language tag is byte-identical
180180
[{ en: 'Pricing', 'zh-CN': '定价' }, 'zh', '定价'],
181181
[{ en: 'Pricing', 'zh-CN': '定价' }, 'zh-TW', '定价'],
182182
[{ 'pt-BR': 'Preços' }, 'pt-PT', 'Preços'],
183-
[{ 'zh-TW': '區域', 'zh-CN': '区域' }, 'zh', '區域'],
183+
// `{ 'zh-TW', 'zh-CN' }` at `zh` moved to the script-preference block below:
184+
// it holds an entry of the session's own script, so it now reads `zh-CN`.
184185
// Limb 4 — `default` outranks `en`.
185186
[{ default: 'D', en: 'E' }, 'fr', 'D'],
186187
// Limb 5 — `en`.
@@ -213,3 +214,75 @@ describe('pickLocalized — controls: every real language tag is byte-identical
213214
expect(pickLocalized(true, 'en')).toBe('true');
214215
});
215216
});
217+
218+
/**
219+
* #30910 — where the platform table splits a language by script (`zh` into
220+
* `zh-Hans` and `zh-Hant`), the base-language and sibling limbs prefer an entry
221+
* of the session's own script, read through the spec's `platformLocaleTagOf`
222+
* (`zh-TW`, `zh-HK` mean `zh-Hant`; `zh`, `zh-CN` mean `zh-Hans`), whatever the
223+
* authoring order. Before, they took the FIRST `zh-*` key, so a `zh-Hant`
224+
* console showed (and, through `localeWriteKey`, overwrote) the `zh-CN` entry.
225+
*/
226+
describe('pickLocalized — an entry of the session\'s own script first (#30910)', () => {
227+
const SIMPLIFIED = ['zh-Hans', 'zh-CN', 'zh-SG', 'zh', 'zh-Hans-CN'] as const;
228+
const TRADITIONAL = ['zh-Hant', 'zh-TW', 'zh-HK', 'zh-MO', 'zh-Hant-TW'] as const;
229+
230+
/** `[name, map]` — every map holds one Simplified and one Traditional entry. */
231+
const BOTH_SCRIPTS: ReadonlyArray<readonly [string, Record<string, string>]> = [
232+
['zh-CN + zh-TW', { en: 'Customer', 'zh-CN': '客户', 'zh-TW': '客戶' }],
233+
['zh-TW + zh-CN (Traditional authored first)', { en: 'Customer', 'zh-TW': '客戶', 'zh-CN': '客户' }],
234+
['zh-Hans + zh-Hant', { en: 'Customer', 'zh-Hans': '客户', 'zh-Hant': '客戶' }],
235+
['zh-Hant + zh-Hans', { en: 'Customer', 'zh-Hant': '客戶', 'zh-Hans': '客户' }],
236+
['zh-CN + zh-HK', { en: 'Customer', 'zh-CN': '客户', 'zh-HK': '客戶' }],
237+
['bare zh + zh-TW', { en: 'Customer', zh: '客户', 'zh-TW': '客戶' }],
238+
];
239+
240+
for (const [name, map] of BOTH_SCRIPTS) {
241+
it.each(TRADITIONAL)(`${name}: a %s session reads the Traditional entry`, (lang) => {
242+
expect(pickLocalized(map, lang)).toBe('客戶');
243+
});
244+
it.each(SIMPLIFIED)(`${name}: a %s session reads the Simplified entry`, (lang) => {
245+
expect(pickLocalized(map, lang)).toBe('客户');
246+
});
247+
}
248+
249+
it('the vector that used to read the first sibling now reads the session\'s script', () => {
250+
// Before #30910 this answered '區域' — the first `zh-*` key in authoring
251+
// order — though bare `zh` means `zh-Hans`.
252+
expect(pickLocalized({ 'zh-TW': '區域', 'zh-CN': '区域' }, 'zh')).toBe('区域');
253+
});
254+
255+
it('the bare base key still wins when it means the session\'s script', () => {
256+
expect(pickLocalized({ zh: '基础', 'zh-CN': '区域', 'zh-TW': '區域' }, 'zh-CN')).toBe('区域');
257+
expect(pickLocalized({ 'zh-Hans': '简体', zh: '基础' }, 'zh-CN')).toBe('基础');
258+
expect(pickLocalized({ 'zh-Hans': '简体', zh: '基础' }, 'zh-Hans-SG')).toBe('基础');
259+
});
260+
261+
/**
262+
* Byte-identical controls: a map with no entry of the session's script, and
263+
* every language the table names once. `[map, locale, answer on main]`.
264+
*/
265+
const UNCHANGED: ReadonlyArray<readonly [Record<string, string>, string, string]> = [
266+
// One script only — nothing to prefer, so the old limbs answer.
267+
[{ en: 'Customer', 'zh-CN': '客户' }, 'zh-Hant', '客户'],
268+
[{ en: 'Customer', 'zh-HK': '客戶' }, 'zh-Hans', '客戶'],
269+
[{ en: 'Customer', 'zh-HK': '客戶' }, 'zh-CN', '客戶'],
270+
[{ en: 'Customer', zh: '客户' }, 'zh-Hant', '客户'],
271+
[{ en: 'Customer', zh: '客户' }, 'zh-TW', '客户'],
272+
// A script-subtag spelling the alias table does not name is not guessed at.
273+
[{ 'zh-CN': '客户', 'zh-Hant-US': '客戶' }, 'zh-Hant', '客户'],
274+
// Languages the table names once keep authoring order on the sibling limb.
275+
[{ 'pt-PT': 'PT', 'pt-BR': 'BR' }, 'pt', 'PT'],
276+
[{ 'pt-PT': 'PT', 'pt-BR': 'BR' }, 'pt-BR', 'BR'],
277+
[{ 'es-MX': 'MX', 'es-ES': 'ES' }, 'es', 'MX'],
278+
[{ 'en-GB': 'GB', 'en-US': 'US' }, 'en', 'GB'],
279+
[{ 'fr-CA': 'CA', 'fr-FR': 'FR' }, 'fr', 'CA'],
280+
[{ 'ja-JP': 'J', en: 'E' }, 'ja', 'J'],
281+
// The language subtag's case still decides the base limb.
282+
[{ 'zh-CN': '客户', 'zh-TW': '客戶', en: 'Customer' }, 'ZH-HANT', 'Customer'],
283+
];
284+
285+
it.each(UNCHANGED)('resolves %j at %j to %j, exactly as before', (map, locale, expected) => {
286+
expect(pickLocalized(map, locale)).toBe(expected);
287+
});
288+
});

0 commit comments

Comments
 (0)