2626 */
2727
2828import { resolveI18nLabel , type I18nLabel } from '@objectstack/spec/ui' ;
29+ import { clearLocalized , setLocalized } from '@object-ui/i18n' ;
2930import { resolveNavItemLabel , type NavTargetLabelResolver } from '@object-ui/layout' ;
3031import 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 */
124127export 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 */
143147export 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}
0 commit comments