Skip to content

Commit cb28b7c

Browse files
committed
fix(i18n): an edit in a Chinese script the label lacks adds its own entry
The shared write limb no longer owns the other script's entry: a zh-Hant edit over { en, zh } adds zh-Hant and keeps the Simplified text, and a clear there removes nothing. Reading is unchanged. Co-Authored-By: Claude <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01L9qibxSGAjHBvjATmenUGp
1 parent cb8f520 commit cb28b7c

4 files changed

Lines changed: 93 additions & 5 deletions

File tree

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
---
2+
'@object-ui/i18n': patch
3+
---
4+
5+
fix(i18n): an edit in a Chinese script the label lacks adds its own entry instead of overwriting the other script
6+
7+
Clause-②: no
8+
Metadata-protocol: none
9+
10+
`setLocalized` and `clearLocalized` no longer treat the other Chinese script's entry as the editor's own. A `zh-Hant` (or `zh-TW`, `zh-HK`) edit of a label written as `{ en: 'Customer', zh: '客户' }` now adds a `zh-Hant` entry and keeps the Simplified text; before, it overwrote `zh` with Traditional text. A clear in that case removes nothing, because the box was showing the other script's text as a fallback. A Simplified editor over a Traditional-only map is handled the same way. Reading is unchanged: `pickLocalized` still shows the other script's entry until one of the reader's own script exists. Every other language, and every map that has an entry of the editor's script, writes exactly as before.

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,3 +128,23 @@ describe('clearLocalized — a script-split language clears the entry of its own
128128
});
129129
});
130130
});
131+
132+
/**
133+
* #31074 — a clear in a script the map lacks removes nothing: the box showed
134+
* the other script's text as a borrowed fallback, and clearing a borrowed
135+
* string must not delete the locale it was borrowed from.
136+
*/
137+
describe('clearLocalized — a clear in a script the map lacks keeps the other script (#31074)', () => {
138+
it.each([
139+
[{ en: 'Customer', zh: '客户' }, 'zh-Hant'],
140+
[{ en: 'Customer', 'zh-CN': '客户' }, 'zh-TW'],
141+
[{ en: 'Customer', 'zh-TW': '客戶' }, 'zh-CN'],
142+
] as const)('%j cleared in %s is returned unchanged', (map, language) => {
143+
expect(clearLocalized(map, language)).toEqual(map);
144+
});
145+
146+
it('an entry of the editor\'s own script is still cleared', () => {
147+
expect(clearLocalized({ en: 'Customer', zh: '客户' }, 'zh-CN')).toEqual({ en: 'Customer' });
148+
expect(clearLocalized({ en: 'Customer', 'zh-TW': '客戶' }, 'zh-HK')).toEqual({ en: 'Customer' });
149+
});
150+
});

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

Lines changed: 47 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -222,10 +222,10 @@ describe('setLocalized — a script-split language writes the entry of its own s
222222
});
223223

224224
/** Byte-identical controls: `[map, language, the key main writes]`. */
225+
// The three one-script Chinese rows that stood here (`{ en, 'zh-CN' }` at
226+
// `zh-Hant`, `{ en, zh }` at `zh-TW`, `{ en, 'zh-HK' }` at `zh-Hans`) pinned
227+
// a cross-script overwrite; #31074 moved them to the block below.
225228
const UNCHANGED: ReadonlyArray<readonly [Record<string, string>, string, string]> = [
226-
[{ en: 'Customer', 'zh-CN': '客户' }, 'zh-Hant', 'zh-CN'],
227-
[{ en: 'Customer', zh: '客户' }, 'zh-TW', 'zh'],
228-
[{ en: 'Customer', 'zh-HK': '客戶' }, 'zh-Hans', 'zh-HK'],
229229
[{ 'pt-PT': 'PT', 'pt-BR': 'BR' }, 'pt', 'pt-PT'],
230230
[{ 'en-GB': 'GB', 'en-US': 'US' }, 'en', 'en-GB'],
231231
[{ 'es-MX': 'MX', 'es-ES': 'ES' }, 'es', 'es-MX'],
@@ -237,3 +237,47 @@ describe('setLocalized — a script-split language writes the entry of its own s
237237
expect(written[key]).toBe('WRITTEN');
238238
});
239239
});
240+
241+
/**
242+
* #31074 — an edit in a script the map lacks ADDS its own script's entry. The
243+
* other script's text is a borrowed display fallback for this editor, the way
244+
* `en` is for `fr`: `{ en, zh }` edited in `zh-Hant` used to overwrite the
245+
* Simplified `zh` text with Traditional. The read side is unchanged, so the
246+
* editor saw the Simplified text before the edit and sees its own after it.
247+
*/
248+
describe('setLocalized — an edit in a script the map lacks adds its own entry (#31074)', () => {
249+
/** `[map, language, what the map reads in that language before the edit]` */
250+
const ADDS: ReadonlyArray<readonly [Record<string, string>, string, string]> = [
251+
[{ en: 'Customer', zh: '客户' }, 'zh-Hant', '客户'],
252+
[{ en: 'Customer', 'zh-CN': '客户' }, 'zh-Hant', '客户'],
253+
[{ en: 'Customer', zh: '客户' }, 'zh-TW', '客户'],
254+
[{ en: 'Customer', 'zh-Hans': '客户' }, 'zh-HK', '客户'],
255+
[{ en: 'Customer', 'zh-HK': '客戶' }, 'zh-Hans', '客戶'],
256+
[{ en: 'Customer', 'zh-TW': '客戶' }, 'zh', '客戶'],
257+
[{ en: 'Customer', 'zh-TW': '客戶' }, 'zh-CN', '客戶'],
258+
// A script subtag the platform table does not name is not guessed at to be
259+
// the editor's own.
260+
[{ en: 'Customer', 'zh-Hant-US': '客戶' }, 'zh-Hant', '客戶'],
261+
];
262+
263+
it.each(ADDS)('%j edited in %s adds that tag and keeps every entry', (map, language, before) => {
264+
expect(pickLocalized(map, language)).toBe(before);
265+
const written = setLocalized(map, language, 'WRITTEN') as Record<string, unknown>;
266+
expect(written).toEqual({ ...map, [language]: 'WRITTEN' });
267+
expect(pickLocalized(written, language)).toBe('WRITTEN');
268+
});
269+
270+
/** `[map, language, the key the edit replaces]`: an entry of the editor's own script is still owned. */
271+
const OWN_SCRIPT: ReadonlyArray<readonly [Record<string, string>, string, string]> = [
272+
[{ en: 'Customer', zh: '客户' }, 'zh-CN', 'zh'],
273+
[{ en: 'Customer', zh: '客户' }, 'zh-Hans', 'zh'],
274+
[{ en: 'Customer', 'zh-CN': '客户' }, 'zh', 'zh-CN'],
275+
[{ en: 'Customer', 'zh-TW': '客戶' }, 'zh-HK', 'zh-TW'],
276+
[{ en: 'Customer', 'zh-Hant': '客戶' }, 'zh-TW', 'zh-Hant'],
277+
];
278+
279+
it.each(OWN_SCRIPT)('%j edited in %s still replaces %s', (map, language, key) => {
280+
const written = setLocalized(map, language, 'WRITTEN') as Record<string, unknown>;
281+
expect(written).toEqual({ ...map, [key]: 'WRITTEN' });
282+
});
283+
});

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

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,8 @@ export function pickLocalized(value: unknown, language: string | undefined | nul
126126
*
127127
* It deliberately follows only the first three limbs (exact tag, base
128128
* language, region-qualified sibling — one {@link localizedEntryKey} call, the
129-
* very function the read side calls) and then STOPS. `pickLocalized` continues
129+
* very function the read side calls) and then STOPS (one script check aside,
130+
* below). `pickLocalized` continues
130131
* into `default` -> `en` -> first value, and those last three limbs must never
131132
* be write targets: they are DISPLAY fallbacks that hand back *another*
132133
* locale's string. Following them would mean an author editing in `fr`, shown
@@ -139,6 +140,16 @@ export function pickLocalized(value: unknown, language: string | undefined | nul
139140
* would leave the edited-away `zh-CN` string in place — invisible in the panel
140141
* that just "changed" it, still live for any consumer that asks for `zh-CN`.
141142
*
143+
* One more stop, where the platform table splits the language by script
144+
* ({@link splitScriptTagOf}: `zh` today): the entry those limbs pick must mean
145+
* the editor's own script. A map with only the other script's entry is shown
146+
* to the editor as a borrowed fallback, the same as `en` is to `fr`, so an
147+
* edit in `zh-Hant` over `{ en, zh }` or `{ en, 'zh-CN' }` ADDS `zh-Hant` and
148+
* the Simplified text survives. A key whose script the table cannot name
149+
* (`zh-Hant-US`) is not guessed at to be the editor's own either. The read
150+
* side is unchanged: that `zh-Hant` reader still sees the `zh` text until a
151+
* Traditional entry exists.
152+
*
142153
* Own properties only, `string` values only, for the same reason every
143154
* `pickLocalized` limb narrowed that way in objectui#3907: a locale tag that
144155
* names an `Object.prototype` member is not an entry, so it cannot be the entry
@@ -151,7 +162,10 @@ function localeWriteKey(
151162
const lang = (language || 'en').trim() || 'en';
152163
const has = (key: string): boolean =>
153164
Object.prototype.hasOwnProperty.call(o, key) && typeof o[key] === 'string';
154-
return localizedEntryKey(lang, has, Object.keys(o)) ?? lang;
165+
const key = localizedEntryKey(lang, has, Object.keys(o));
166+
if (key === undefined) return lang;
167+
const script = splitScriptTagOf(lang);
168+
return script === undefined || platformLocaleTagOf(key) === script ? key : lang;
155169
}
156170

157171
/**

0 commit comments

Comments
 (0)