Skip to content

Commit c4b41a1

Browse files
committed
fix(app-shell): Studio designs a Traditional Chinese session as zh-Hant
useMetadataLocale resolves the session language through the console's own resolveBuiltInLanguage, so the nav designer reads and edits the entry the sidebar shows. Chrome falls back to the Simplified catalogue; three strict zh-CN chrome comparisons move to isZhLocale. navItemLabel's rename and clear go through the shared write limb. Co-Authored-By: Claude <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01L9qibxSGAjHBvjATmenUGp
1 parent cb28b7c commit c4b41a1

7 files changed

Lines changed: 171 additions & 43 deletions

File tree

‎ui/packages/app-shell/src/views/metadata-admin/AssignedUsersSection.tsx‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ import { usePermissions } from '@object-ui/permissions';
8686
import { extractWriteErrorMessage, useAdapter, useMetadata } from '@object-ui/react';
8787
import { Plus, X, Users, Loader2, AlertCircle } from 'lucide-react';
8888
import { useMetadataLocale } from './i18n.js';
89+
import { isZhLocale } from './schema-field-label.js';
8990
import { membershipOrganization, readOrganizationMemberIds, type MemberFilter } from './organization-members.js';
9091

9192
/** The object a permission set's direct grants are rows of: ADD creates one, REMOVE deletes one. */
@@ -129,7 +130,7 @@ interface AssignedRow {
129130
* them: a space in English, none after a CJK full stop.
130131
*/
131132
function useCopy() {
132-
const zh = useMetadataLocale() === 'zh-CN';
133+
const zh = isZhLocale(useMetadataLocale());
133134
return React.useMemo(
134135
() =>
135136
zh

‎ui/packages/app-shell/src/views/metadata-admin/PositionHoldersSection.tsx‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ import { usePermissions } from '@object-ui/permissions';
6161
import { extractWriteErrorMessage, useAdapter, useMetadata } from '@object-ui/react';
6262
import { AlertCircle, Loader2, Plus, UserCheck, X } from 'lucide-react';
6363
import { useMetadataLocale } from './i18n.js';
64+
import { isZhLocale } from './schema-field-label.js';
6465
import { membershipOrganization, readOrganizationMemberIds, type MemberFilter } from './organization-members.js';
6566

6667
export interface PositionHoldersSectionProps {
@@ -107,7 +108,7 @@ const PICKER_EMAIL_FIELD = 'email';
107108
const refusalSentence = (err: unknown): string => extractWriteErrorMessage(err) ?? '';
108109

109110
function useCopy() {
110-
const zh = useMetadataLocale() === 'zh-CN';
111+
const zh = isZhLocale(useMetadataLocale());
111112
return React.useMemo(
112113
() =>
113114
zh

‎ui/packages/app-shell/src/views/metadata-admin/ResourceEditPage.tsx‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ import { useRegisterAssistantEditor, type AssistantEditorContext } from '../../a
147147
import { useRegisteredMetadataInspector } from './inspector-registry.js';
148148
import { useRegisteredMetadataDefaultInspector } from './default-inspector-registry.js';
149149
import { InspectorReadOnlyReason } from './inspectors/_shared.js';
150-
import { useMetadataLocale, t, tFormat, translateMetadataType, translateSchemaIssue, translateValidationMessage } from './i18n.js';
150+
import { isZhLocale, useMetadataLocale, t, tFormat, translateMetadataType, translateSchemaIssue, translateValidationMessage } from './i18n.js';
151151
import { JsonSourceEditor } from '../item-tools/JsonSourceEditor.js';
152152
import { validateMetadataDraft, hasClientValidator, type DraftMode } from './clientValidation.js';
153153
import {
@@ -3056,7 +3056,7 @@ function MetadataResourceEditPageImpl({
30563056
rel="noopener noreferrer"
30573057
className="mt-1 inline-flex items-center gap-1 text-amber-800 underline hover:text-amber-900 dark:text-amber-200 dark:hover:text-amber-100"
30583058
>
3059-
{locale === 'zh-CN' ? '查看文档' : 'View docs'} →
3059+
{isZhLocale(locale) ? '查看文档' : 'View docs'} →
30603060
</a>
30613061
)}
30623062
{layered?.packageId && (

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

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@
5858
* `ui/scripts/__tests__/check-i18n-dead-keys.test.ts`.
5959
*/
6060

61-
import { useObjectTranslation } from '@object-ui/i18n';
61+
import { resolveBuiltInLanguage, useObjectTranslation } from '@object-ui/i18n';
6262
// The designer strings' new home. Its `en` catalogue is resident; its other
6363
// catalogues are fetched on demand, and `useMetadataLocale()` re-renders its
6464
// caller when one lands. ⚠️ `ui/scripts/check-i18n-dead-keys.mjs` reads the
@@ -74,7 +74,15 @@ import type {
7474
MetadataOverlayScope,
7575
} from '@object-ui/data-objectstack';
7676

77-
export type SupportedLocale = 'en-US' | 'zh-CN';
77+
/**
78+
* The designer locale. `'zh-Hant'` is a Traditional Chinese session: Studio
79+
* reads and writes the Traditional entry of an inline label in it, and its
80+
* chrome strings fall back to the namespace's `zh` (Simplified) catalogue,
81+
* which is the only Chinese one it ships (`resolveStudioLanguage`). Compare a
82+
* value with `isZhLocale`, never `=== 'zh-CN'`, when the question is "Chinese
83+
* chrome?".
84+
*/
85+
export type SupportedLocale = 'en-US' | 'zh-CN' | 'zh-Hant';
7886

7987
const TYPE_LABELS_EN: Record<string, string> = {
8088
// Data
@@ -931,9 +939,23 @@ export function detectLocale(): SupportedLocale {
931939
return 'en-US';
932940
}
933941

942+
/**
943+
* The designer locale for an i18n `language` — {@link useMetadataLocale}'s
944+
* answer, as a pure function.
945+
*/
946+
export function designerLocaleOf(language: string | undefined): SupportedLocale {
947+
if (!/^zh/i.test(language ?? '')) return 'en-US';
948+
return resolveBuiltInLanguage(language ?? '') === 'zh-Hant' ? 'zh-Hant' : 'zh-CN';
949+
}
950+
934951
/**
935952
* React hook — the Studio/metadata-admin locale derived from the app's ACTIVE
936-
* i18n language, collapsed to one of the two locales this catalog ships.
953+
* i18n language: `'zh-Hant'` for a session the console serves in Traditional
954+
* Chinese (`zh-Hant`, `zh-TW`, `zh-HK`, … through `resolveBuiltInLanguage`,
955+
* the console's own resolution), `'zh-CN'` for every other Chinese session,
956+
* `'en-US'` otherwise. The Traditional value exists for the label entries an
957+
* author reads and edits (the nav designer shows and renames the entry the
958+
* sidebar shows); its chrome strings are the Simplified ones.
937959
*
938960
* Prefer this over {@link detectLocale} in components: `detectLocale()` reads
939961
* `navigator.language`, which never changes when the user switches languages
@@ -944,15 +966,15 @@ export function detectLocale(): SupportedLocale {
944966
*
945967
* ⛔ Never hand this value to `Intl` (`Intl.*Format`, `toLocale*String`) as the
946968
* locale a date, time or number is formatted in. It picks one of this file's
947-
* TWO string tables, so it is `'en-US'` for every language that is not zh — a
969+
* string tables, so it is `'en-US'` for every language that is not zh — a
948970
* de-DE session would read its faces in US English (objectui#10232). Faces
949971
* format with `useDisplayLocale()` from `@object-ui/i18n`, the channel every
950972
* date and number renderer resolves through; keep this value for `t()` /
951973
* `tFormat()` and the other lookups into the tables here.
952974
*/
953975
export function useMetadataLocale(): SupportedLocale {
954976
const { language } = useObjectTranslation();
955-
const locale: SupportedLocale = /^zh/i.test(language) ? 'zh-CN' : 'en-US';
977+
const locale = designerLocaleOf(language);
956978
// Starts the fetch of the `studio` namespace catalogue this locale reads and
957979
// re-renders the caller when it lands, so a moved key does not stay on the
958980
// `en` answer the first render gave.

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

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -195,8 +195,8 @@ describe('#30910 — renamedLabel and clearedLabel change the entry the resolver
195195
// Neither the exact tag nor the bare language: the exact tag is added, a sibling region is not owned.
196196
[{ en: 'Customer', 'zh-CN': '客户', 'zh-TW': '客戶' }, 'zh-Hant', { en: 'Customer', 'zh-CN': '客户', 'zh-TW': '客戶', 'zh-Hant': 'RENAMED' }],
197197
[{ 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' }],
198+
// The bare language of the locale's own script: the bare language, as before.
199+
// (`{ en, zh }` renamed in `zh-Hant`, which wrote `zh` here, moved to the #31074 block.)
200200
[{ en: 'Customer', zh: '客户', 'zh-TW': '客戶' }, 'zh-CN', { en: 'Customer', zh: 'RENAMED', 'zh-TW': '客戶' }],
201201
// Languages the table names once.
202202
[{ en: 'Accounts', 'en-GB': 'Accounts (GB)' }, 'en-US', { en: 'RENAMED', 'en-GB': 'Accounts (GB)' }],
@@ -207,3 +207,29 @@ describe('#30910 — renamedLabel and clearedLabel change the entry the resolver
207207
expect(renamedLabel(map as I18nLabel, 'RENAMED', locale)).toEqual(expected);
208208
});
209209
});
210+
211+
/**
212+
* #31074 — a rename in a script the map lacks adds the locale's own entry
213+
* (the shared write limb, `setLocalized`), and a clear there removes nothing:
214+
* a Traditional designer over `{ en, zh }` used to overwrite the Simplified
215+
* text. The entry is shown in the other script until then, as the sidebar
216+
* shows it.
217+
*/
218+
describe('#31074 — a rename in a script the map lacks adds its own entry', () => {
219+
const CROSS: ReadonlyArray<readonly [Record<string, string>, string]> = [
220+
[{ en: 'Customer', zh: '客户' }, 'zh-Hant'],
221+
[{ en: 'Customer', 'zh-CN': '客户' }, 'zh-Hant'],
222+
[{ en: 'Customer', zh: '客户' }, 'zh-TW'],
223+
[{ en: 'Customer', 'zh-Hant': '客戶' }, 'zh-CN'],
224+
];
225+
226+
it.each(CROSS)('%j renamed in %s adds that tag, keeps the other script, and shows the new text', (map, locale) => {
227+
const next = renamedLabel(map as I18nLabel, 'RENAMED', locale) as Record<string, string>;
228+
expect(next).toEqual({ ...map, [locale]: 'RENAMED' });
229+
expect(navItemLabelText(next as I18nLabel, locale)).toBe('RENAMED');
230+
});
231+
232+
it.each(CROSS)('%j cleared in %s removes nothing', (map, locale) => {
233+
expect(clearedLabel(map as I18nLabel, locale)).toEqual(map);
234+
});
235+
});

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

Lines changed: 26 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
*/
2727

2828
import { resolveI18nLabel, type I18nLabel } from '@objectstack/spec/ui';
29+
import { clearLocalized, setLocalized } from '@object-ui/i18n';
2930
import { resolveNavItemLabel, type NavTargetLabelResolver } from '@object-ui/layout';
3031
import type { NavigationItem } from '@object-ui/types';
3132

@@ -116,16 +117,19 @@ export function navEntryLabelText(
116117
* `{ zh, 'zh-TW' }`, because bare `zh` means `zh-Hans`) — so the editor shows
117118
* the new text straight after the edit. A map with none of these gains an
118119
* 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
120+
* language's fallback (`default`, `en` under `zh-CN`, a sibling region, or the
121+
* other Chinese script's entry: `zh` under `zh-Hant`) is left as it was. The
122+
* entry is chosen by the shared write limb (`setLocalized`, see
123+
* {@link ownsAnEntry}). Because the new entry is under the exact
121124
* tag, the next edit finds it first: an editor that writes on every keystroke
122125
* adds that entry once, never one per keystroke.
123126
*/
124127
export function renamedLabel(cur: I18nLabel | undefined, next: string, locale: string): I18nLabel {
125128
if (!cur || typeof cur !== 'object' || Array.isArray(cur)) return next;
126129
const map: Record<string, string> = cur;
127-
const key = ownEntryKey(map, locale) ?? locale.trim();
128-
return { ...map, [key]: next };
130+
const tag = locale.trim();
131+
if (!ownsAnEntry(map, tag)) return { ...map, [tag]: next };
132+
return setLocalized(map, tag, next) as Record<string, string>;
129133
}
130134

131135
/**
@@ -142,37 +146,27 @@ export function renamedLabel(cur: I18nLabel | undefined, next: string, locale: s
142146
*/
143147
export function clearedLabel(cur: I18nLabel | undefined, locale: string): I18nLabel | undefined {
144148
if (!cur || typeof cur !== 'object' || Array.isArray(cur)) return undefined;
145-
const map: Record<string, string> = { ...cur };
146-
const key = ownEntryKey(map, locale);
147-
if (key !== undefined) delete map[key];
148-
return Object.values(map).some((v) => typeof v === 'string') ? map : undefined;
149+
const tag = locale.trim();
150+
const map = ownsAnEntry(cur, tag) ? clearLocalized(cur, tag) : cur;
151+
return map !== undefined && Object.values(map).some((v) => typeof v === 'string')
152+
? (map as Record<string, string>)
153+
: undefined;
149154
}
150155

151156
/**
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.
157+
* Whether an edit in `tag` owns an entry of `map` at all: the exact tag or the
158+
* bare language is present. Which entry it then owns is the shared write limb's
159+
* answer (`setLocalized` / `clearLocalized` in `@object-ui/i18n`), the entry
160+
* the resolver shows: the exact tag, else the bare language, else, where the
161+
* resolver prefers it, a sibling of the locale's own script (`zh-TW` under
162+
* `zh-Hant` in `{ zh, 'zh-TW' }`). Where the platform table splits the language
163+
* by script, an entry of the OTHER script is never owned: a `zh-Hant` edit over
164+
* `{ en, zh }` adds `zh-Hant` and keeps the Simplified text. A sibling the
165+
* resolver reaches only because the bare language is ABSENT stays another
166+
* region's fallback, as before: the edit adds the exact tag. So this module
167+
* keeps no copy of either side's limbs.
164168
*/
165-
function ownEntryKey(map: Record<string, string>, locale: string): string | undefined {
166-
const tag = locale.trim();
169+
function ownsAnEntry(map: Record<string, string>, tag: string): boolean {
167170
const own = (key: string) => Object.prototype.hasOwnProperty.call(map, key) && typeof map[key] === 'string';
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;
171+
return own(tag) || own(tag.split('-')[0]);
178172
}
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license.
2+
3+
/**
4+
* #31074 — Studio's designer locale follows the session's Chinese script.
5+
*
6+
* `useMetadataLocale()` collapsed every `zh*` language to `'zh-CN'`, so a
7+
* Traditional Chinese session designed in Simplified: the nav designer listed
8+
* `{ en, zh: '简体项目', 'zh-TW': '繁體專案' }` as 简体项目 while the sidebar of
9+
* the same session showed 繁體專案, and a rename there overwrote the Simplified
10+
* `zh` text. The hook now resolves the session language through the console's
11+
* own resolution (`resolveBuiltInLanguage`), so a session the console serves
12+
* in `zh-Hant` designs as `'zh-Hant'`.
13+
*
14+
* Pinned through the real hook under a real `I18nProvider`, and through the
15+
* two things a designer does with its answer: read the label, rename it.
16+
* Chrome is the other half: the studio namespace ships one Chinese catalogue,
17+
* so a Traditional designer reads Simplified chrome, never English.
18+
*/
19+
20+
import * as React from 'react';
21+
import { afterEach, describe, expect, it } from 'vitest';
22+
import { cleanup, render, screen } from '@testing-library/react';
23+
import { I18nProvider } from '@object-ui/i18n';
24+
import type { I18nLabel } from '@objectstack/spec/ui';
25+
26+
import { isZhLocale, t, useMetadataLocale } from './i18n';
27+
import { navItemLabelText, renamedLabel } from './previews/navItemLabel';
28+
29+
afterEach(cleanup);
30+
31+
const MAP: I18nLabel = { en: 'Proj EN', zh: '简体项目', 'zh-TW': '繁體專案' };
32+
33+
function Probe() {
34+
const locale = useMetadataLocale();
35+
return (
36+
<output data-testid="probe" data-locale={locale}>
37+
{navItemLabelText(MAP, locale)}
38+
</output>
39+
);
40+
}
41+
42+
function designerLocaleIn(lang: string): string {
43+
render(
44+
<I18nProvider config={{ defaultLanguage: lang, detectBrowserLanguage: false }}>
45+
<Probe />
46+
</I18nProvider>,
47+
);
48+
return screen.getByTestId('probe').getAttribute('data-locale') ?? '';
49+
}
50+
51+
describe('useMetadataLocale follows the session script (#31074)', () => {
52+
it.each(['zh-Hant', 'zh-TW', 'zh-HK'])('a %s session designs as zh-Hant and reads the Traditional entry', (lang) => {
53+
expect(designerLocaleIn(lang)).toBe('zh-Hant');
54+
expect(screen.getByTestId('probe').textContent).toBe('繁體專案');
55+
});
56+
57+
it('a rename in that designer locale replaces zh-TW and leaves zh untouched', () => {
58+
const locale = designerLocaleIn('zh-Hant');
59+
expect(renamedLabel(MAP, '改名後', locale)).toEqual({ en: 'Proj EN', zh: '简体项目', 'zh-TW': '改名後' });
60+
});
61+
62+
it('a rename in that designer locale over a Simplified-only map adds zh-Hant', () => {
63+
const locale = designerLocaleIn('zh-Hant');
64+
expect(renamedLabel({ en: 'Proj EN', zh: '简体项目' }, '改名後', locale)).toEqual({
65+
en: 'Proj EN',
66+
zh: '简体项目',
67+
'zh-Hant': '改名後',
68+
});
69+
});
70+
71+
it.each(['zh-Hans', 'zh-CN', 'zh'])('CONTROL: a %s session designs as zh-CN and reads the Simplified entry', (lang) => {
72+
expect(designerLocaleIn(lang)).toBe('zh-CN');
73+
expect(screen.getByTestId('probe').textContent).toBe('简体项目');
74+
});
75+
76+
it.each(['en', 'de', 'ja'])('CONTROL: a %s session designs as en-US', (lang) => {
77+
expect(designerLocaleIn(lang)).toBe('en-US');
78+
});
79+
80+
it('a zh-Hant designer reads Chinese chrome (the Simplified catalogue), never English', () => {
81+
expect(isZhLocale('zh-Hant')).toBe(true);
82+
expect(t('engine.studio.cancel', 'zh-Hant')).toBe(t('engine.studio.cancel', 'zh-CN'));
83+
});
84+
});

0 commit comments

Comments
 (0)