Skip to content

Commit 7422324

Browse files
committed
fix(app-shell): the Studio nav label editor writes the entry the resolver shows
ownEntryKey asked for the exact tag, else the bare language. Where the resolver now prefers a sibling of the locale's own script over a bare language of the other script, the editor overwrote and cleared the entry it was not showing. It now asks resolveI18nLabel which entry it reads, over a map of each key to its own name, and keeps no copy of the resolver's limbs. Co-Authored-By: Claude <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01L9qibxSGAjHBvjATmenUGp
1 parent 4d0fb81 commit 7422324

2 files changed

Lines changed: 88 additions & 6 deletions

File tree

‎ui/packages/app-shell/src/views/metadata-admin/previews/navItemLabel.inherit-11196.test.ts‎

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,13 @@
2626
import { describe, it, expect } from 'vitest';
2727
import { resolveNavItemLabel, type NavTargetLabelResolver } from '@object-ui/layout';
2828
import type { NavigationItem } from '@object-ui/types';
29+
import { resolveI18nLabel, type I18nLabel } from '@objectstack/spec/ui';
2930
import {
3031
clearedLabel,
3132
inheritedNavEntryText,
3233
navEntryLabelText,
34+
navItemLabelText,
35+
renamedLabel,
3336
type NavEntryLike,
3437
} from './navItemLabel';
3538

@@ -150,3 +153,57 @@ describe('objectui#11196 — clearedLabel restores inheritance, the inverse of r
150153
expect(clearedLabel({ 'zh-CN': '客户' }, 'zh-CN')).toBeUndefined();
151154
});
152155
});
156+
157+
/**
158+
* #30910 — where the platform table splits a language by script, the resolver
159+
* reads a sibling of the locale's own script over a bare language of the other
160+
* one (`zh-TW` under `zh-Hant` in `{ zh, 'zh-TW' }`, bare `zh` meaning
161+
* `zh-Hans`). The entry a rename writes and a clear deletes must be that same
162+
* entry, or a Traditional editor overwrites the Simplified text while the
163+
* Traditional text stays on screen. Pinned against `resolveI18nLabel` itself.
164+
*/
165+
describe('#30910 — renamedLabel and clearedLabel change the entry the resolver shows', () => {
166+
/** `[map, designer locale, the key the resolver reads and an edit owns]` */
167+
const OWNED: ReadonlyArray<readonly [Record<string, string>, string, string]> = [
168+
[{ en: 'Customer', zh: '客户', 'zh-TW': '客戶' }, 'zh-Hant', 'zh-TW'],
169+
[{ en: 'Customer', zh: '客户', 'zh-TW': '客戶' }, 'zh-TW', 'zh-TW'],
170+
[{ en: 'Customer', zh: '客户', 'zh-HK': '客戶' }, 'zh-HK', 'zh-HK'],
171+
[{ en: 'Customer', zh: '客户', 'zh-Hant': '客戶' }, 'zh-HK', 'zh-Hant'],
172+
[{ en: 'Customer', 'zh-TW': '客戶', zh: '客户' }, 'zh-CN', 'zh'],
173+
[{ en: 'Customer', 'zh-TW': '客戶', zh: '客户' }, 'zh-Hans', 'zh'],
174+
];
175+
176+
it.each(OWNED)('%j in %s: the resolver reads %s', (map, locale, key) => {
177+
expect(resolveI18nLabel(map as I18nLabel, locale)).toBe(map[key]);
178+
});
179+
180+
it.each(OWNED)('%j renamed in %s writes %s, shows the new text, and keeps every other entry', (map, locale, key) => {
181+
const next = renamedLabel(map as I18nLabel, 'RENAMED', locale) as Record<string, string>;
182+
expect(next[key]).toBe('RENAMED');
183+
for (const other of Object.keys(map).filter((k) => k !== key)) expect(next[other]).toBe(map[other]);
184+
expect(Object.keys(next)).toEqual(Object.keys(map));
185+
expect(navItemLabelText(next as I18nLabel, locale)).toBe('RENAMED');
186+
});
187+
188+
it.each(OWNED)('%j cleared in %s deletes %s and nothing else', (map, locale, key) => {
189+
const rest = Object.fromEntries(Object.entries(map).filter(([k]) => k !== key));
190+
expect(clearedLabel(map as I18nLabel, locale)).toEqual(rest);
191+
});
192+
193+
/** Byte-identical controls: `[map, locale, what main's rename wrote]`. */
194+
const UNCHANGED: ReadonlyArray<readonly [Record<string, string>, string, Record<string, string>]> = [
195+
// Neither the exact tag nor the bare language: the exact tag is added, a sibling region is not owned.
196+
[{ en: 'Customer', 'zh-CN': '客户', 'zh-TW': '客戶' }, 'zh-Hant', { en: 'Customer', 'zh-CN': '客户', 'zh-TW': '客戶', 'zh-Hant': 'RENAMED' }],
197+
[{ en: 'Customer', 'zh-TW': '客戶' }, 'zh-Hant', { en: 'Customer', 'zh-TW': '客戶', 'zh-Hant': 'RENAMED' }],
198+
// The bare language with no sibling of the locale's script: the bare language, as before.
199+
[{ en: 'Customer', zh: '客户' }, 'zh-Hant', { en: 'Customer', zh: 'RENAMED' }],
200+
[{ en: 'Customer', zh: '客户', 'zh-TW': '客戶' }, 'zh-CN', { en: 'Customer', zh: 'RENAMED', 'zh-TW': '客戶' }],
201+
// Languages the table names once.
202+
[{ en: 'Accounts', 'en-GB': 'Accounts (GB)' }, 'en-US', { en: 'RENAMED', 'en-GB': 'Accounts (GB)' }],
203+
[{ pt: 'Contas', 'pt-PT': 'Contas (PT)' }, 'pt-BR', { pt: 'RENAMED', 'pt-PT': 'Contas (PT)' }],
204+
];
205+
206+
it.each(UNCHANGED)('%j renamed in %s writes exactly as before', (map, locale, expected) => {
207+
expect(renamedLabel(map as I18nLabel, 'RENAMED', locale)).toEqual(expected);
208+
});
209+
});

‎ui/packages/app-shell/src/views/metadata-admin/previews/navItemLabel.ts‎

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -111,10 +111,13 @@ export function navEntryLabelText(
111111
* language's text. Only the designer locale's entry changes, and every other
112112
* entry is kept. The entry written is the one `resolveI18nLabel` reads first
113113
* for this locale — the exact tag, else its bare language (`en` under
114-
* `en-US`) — so the editor shows the new text straight after the edit. A map
115-
* with neither gains an entry under the exact tag; an entry the resolver only
116-
* reached as another language's fallback (`default`, `en` under `zh-CN`, a
117-
* sibling region) is left as it was. Because the new entry is under the exact
114+
* `en-US`), else, where the resolver prefers it over that bare language, a
115+
* sibling of the locale's own script (`zh-TW` under `zh-Hant` in
116+
* `{ zh, 'zh-TW' }`, because bare `zh` means `zh-Hans`) — so the editor shows
117+
* the new text straight after the edit. A map with none of these gains an
118+
* entry under the exact tag; an entry the resolver only reached as another
119+
* language's fallback (`default`, `en` under `zh-CN`, a sibling region) is left
120+
* as it was. Because the new entry is under the exact
118121
* tag, the next edit finds it first: an editor that writes on every keystroke
119122
* adds that entry once, never one per keystroke.
120123
*/
@@ -145,9 +148,31 @@ export function clearedLabel(cur: I18nLabel | undefined, locale: string): I18nLa
145148
return Object.values(map).some((v) => typeof v === 'string') ? map : undefined;
146149
}
147150

148-
/** The map entry an edit in `locale` owns: the exact tag, else its bare language; `undefined` when the map has neither. */
151+
/**
152+
* The map entry an edit in `locale` owns: the exact tag, else the entry
153+
* `resolveI18nLabel` reads in place of the bare language, else the bare
154+
* language; `undefined` when the map has neither the tag nor the language.
155+
*
156+
* Which entry the resolver reads is asked of the resolver itself, over a map
157+
* of each usable key to its own name, so this module keeps no copy of the
158+
* resolver's limbs. With the bare language present the resolver reads it,
159+
* unless it prefers a sibling of the locale's own script (`zh-TW` under
160+
* `zh-Hant`, where bare `zh` means `zh-Hans`); then the sibling is the entry
161+
* on screen and the one an edit owns. A sibling the resolver reaches only
162+
* because the bare language is ABSENT stays another region's fallback, as
163+
* before: the edit adds the exact tag.
164+
*/
149165
function ownEntryKey(map: Record<string, string>, locale: string): string | undefined {
150166
const tag = locale.trim();
151167
const own = (key: string) => Object.prototype.hasOwnProperty.call(map, key) && typeof map[key] === 'string';
152-
return [tag, tag.split('-')[0]].find(own);
168+
if (own(tag)) return tag;
169+
const base = tag.split('-')[0];
170+
if (!own(base)) return undefined;
171+
const keyOfEachEntry: Record<string, string> = Object.fromEntries(
172+
Object.keys(map)
173+
.filter(own)
174+
.map((key) => [key, key]),
175+
);
176+
const read = resolveI18nLabel(keyOfEachEntry, locale);
177+
return read !== undefined && read !== base && read.split('-')[0] === base ? read : base;
153178
}

0 commit comments

Comments
 (0)