Skip to content
15 changes: 15 additions & 0 deletions .changeset/34903-flow-designer-label-translations.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
---
'@object-ui/app-shell': patch
'@object-ui/i18n': patch
---

fix(app-shell): the Studio flow designer shows a package's translated flow label and approval-step labels

Clause-②: yes
Metadata-protocol: none

A package can translate a flow's label (`flows.<flow>.label`) and an approval step's label (`flows.<flow>.approvals.<node_id>.label`). The flow runner already used both. The Studio flow designer ignored them, so a zh-CN admin saw the authored English flow name and step names. The designer now reads the same bundle the runner reads, through the spec's `translateFlow`, in the viewer's language. The automation header names the flow by its translation. Approval nodes show their translated step label on the canvas (nested ones included) and in the inspector header. The inspector also shows the authored label on a second line ("Authored label: …"), because Studio is where that label is edited. With no translation, every label shows as authored, as before. The draft is never rewritten with a translation.

The spec has no key for a decision or other node's label, or for an edge label, so those still show the authored text.

`@object-ui/i18n` gains one lazy `studio`-namespace key, `engine.inspector.flowNode.sourceLabel` (en and zh). The eager `locales/en.ts` pack is unchanged.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,18 @@ import { extractRegions, NODE_H } from './flow-canvas-layout';
import { predictExpandedNodeHeight } from './flow-region-metrics';
import type { FlowProblem } from './flow-problems';
import { browserClick, browserDrag } from './__tests__/browserClick';
// FlowPreview reaches a dynamic `import('@objectstack/lint')`; pay its load at collection, not in a test's clock.
import '@objectstack/lint';
import { createI18n, I18nProvider, transformSpecTranslations } from '@object-ui/i18n';
import type { TranslationData } from '@objectstack/spec/system';
import { FlowPreview, FlowSelectionTitle, flowSelectionHeading } from './FlowPreview';
import {
FlowLabelTranslationsProvider,
flowLabelTranslations,
useFlowLabelTranslations,
type FlowLabelTranslations,
} from './flow-label-translations';
import { activeFlowsBundle } from '../../../utils/flowsBundle';

afterEach(cleanup);

Expand Down Expand Up @@ -596,3 +608,128 @@ describe('FlowCanvas — a node press on a read-only design canvas selects, and
expect(onPatch).not.toHaveBeenCalled();
});
});


/**
* The flow designer reads a package's `flows` translations: the flow's label
* (`flows.<flow>.label`) and an approval node's step label
* (`flows.<flow>.approvals.<node_id>.label`), in the viewer's language, from
* the bundle `FlowRunner` reads. A decision node and an edge have no key, so
* they stay authored; with no translation everything is authored, as before.
*/
describe('Flow designer — package label translations (flows.<flow>.label, approvals.<step>.label)', () => {
const ZH_CN: TranslationData = {
flows: {
opportunity_approval: {
label: '大额商机审批',
approvals: { manager_review: { label: '经理审核' }, director_signoff: { label: '总监签批' } },
},
},
};
const NODES = [
{ id: 'start', type: 'start', label: 'Start' },
{ id: 'manager_review', type: 'approval', label: 'Manager review' },
{ id: 'big', type: 'decision', label: 'Over 1M?' },
{
id: 'each',
type: 'loop',
label: 'For each signer',
config: { body: { nodes: [{ id: 'director_signoff', type: 'approval', label: 'Director sign-off' }], edges: [] } },
},
];
const EDGES = [
{ source: 'start', target: 'manager_review' },
{ source: 'manager_review', target: 'big', label: 'approve' },
{ source: 'big', target: 'each', label: 'Yes, over 1M' },
];
const DRAFT = { name: 'opportunity_approval', label: 'Large Deal Approval', nodes: NODES, edges: EDGES };

function zhInstance() {
const instance = createI18n({ defaultLanguage: 'zh-CN', detectBrowserLanguage: false });
instance.addResourceBundle('zh-CN', 'translation', transformSpecTranslations(ZH_CN as Record<string, unknown>), true, true);
return instance;
}
const zh = (): FlowLabelTranslations => {
const t = flowLabelTranslations(activeFlowsBundle(zhInstance(), 'zh-CN', 'opportunity_approval'), 'opportunity_approval', 'zh-CN');
expect(t).toBeDefined();
return t!;
};

it('resolves the flow label and approval-step labels out of the loaded resource tree, and nothing else', () => {
const t = zh();
expect(t.flowLabel).toBe('大额商机审批');
expect(t.stepLabel({ id: 'manager_review', type: 'approval' })).toBe('经理审核');
expect(t.stepLabel({ id: 'director_signoff', type: 'approval' })).toBe('总监签批');
// No key for any other node type, even under an approval's id.
expect(t.stepLabel({ id: 'manager_review', type: 'decision' })).toBeUndefined();
expect(t.stepLabel({ id: 'unknown_step', type: 'approval' })).toBeUndefined();
// No bundle for the flow: nothing to translate with.
expect(flowLabelTranslations(activeFlowsBundle(zhInstance(), 'zh-CN', 'other_flow'), 'other_flow', 'zh-CN')).toBeUndefined();
});

it('useFlowLabelTranslations reads the console provider’s active language', () => {
let seen: FlowLabelTranslations | undefined;
function Probe() {
seen = useFlowLabelTranslations('opportunity_approval');
return null;
}
render(
<I18nProvider instance={zhInstance()} persistLanguage={false}>
<Probe />
</I18nProvider>,
);
expect(seen?.flowLabel).toBe('大额商机审批');
expect(seen?.stepLabel({ id: 'manager_review', type: 'approval' })).toBe('经理审核');
});

it('draws the translated approval-step labels on the canvas, nested ones included; decision and branch-edge labels stay authored', () => {
const { container } = render(
<FlowLabelTranslationsProvider value={zh()}>
<FlowCanvas nodes={NODES} edges={EDGES} editable={false} designMode={false} selectedId={null} onSelect={() => {}} />
</FlowLabelTranslationsProvider>,
);
const card = (id: string) => container.querySelector(`[data-node-id="${id}"]`) as HTMLElement;
expect(card('manager_review').textContent).toContain('经理审核');
expect(card('manager_review').textContent).not.toContain('Manager review');
expect(card('big').textContent).toContain('Over 1M?');
// A decision branch's authored edge label has no package key, so it stays as written. (An approval's
// `approve` / `reject` outcome pills are the designer's own vocabulary, not the package bundle's.)
expect(screen.getByText('Yes, over 1M')).toBeInTheDocument();
fireEvent.click(screen.getByRole('button', { name: 'Expand nested regions' }));
expect(card('each').textContent).toContain('总监签批');
expect(card('each').textContent).not.toContain('Director sign-off');
});

it('with no translation the canvas draws the authored labels, as before', () => {
const { container } = render(
<FlowCanvas nodes={NODES} edges={EDGES} editable={false} designMode={false} selectedId={null} onSelect={() => {}} />,
);
expect((container.querySelector('[data-node-id="manager_review"]') as HTMLElement).textContent).toContain('Manager review');
});

it('FlowPreview hands the bundle in from the console provider', () => {
vi.stubGlobal('fetch', vi.fn(async () => new Response('not found', { status: 404 })));
const { container } = render(
<I18nProvider instance={zhInstance()} persistLanguage={false}>
<FlowPreview type="flow" name="opportunity_approval" draft={DRAFT} selection={null} onSelectionChange={() => {}} />
</I18nProvider>,
);
expect((container.querySelector('[data-node-id="manager_review"]') as HTMLElement).textContent).toContain('经理审核');
});

it('the inspector header titles an approval step by its translation and shows the authored label beside it', () => {
const t = zh();
const top = flowSelectionHeading(DRAFT, { kind: 'node', id: 'manager_review' }, undefined, t);
expect(top).toMatchObject({ title: '经理审核', source: 'Manager review' });
render(<FlowSelectionTitle heading={top} />);
expect(screen.getByText('经理审核')).toBeInTheDocument();
expect(screen.getByTestId('flow-selection-source').textContent).toContain('Manager review');
// A decision node has no key: authored title, no source line.
const decision = flowSelectionHeading(DRAFT, { kind: 'node', id: 'big' }, undefined, t);
expect(decision.title).toBe('Over 1M?');
expect(decision).not.toHaveProperty('source');
// No translations: exactly as before.
expect(flowSelectionHeading(DRAFT, { kind: 'node', id: 'manager_review' })).not.toHaveProperty('source');
expect(flowSelectionHeading(DRAFT, { kind: 'node', id: 'manager_review' }).title).toBe('Manager review');
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import { AlertCircle, AlertTriangle, Maximize2, Plus, ZoomIn, ZoomOut } from 'lu
import { cn } from '@object-ui/components';
import { uniqueId, appendArray, spliceArray } from '../inspectors/_shared.js';
import { t as tr, tFormat } from '../i18n.js';
import { useFlowLabelTranslationsContext } from './flow-label-translations.js';
import {
computeLayoutWithGeometry,
NODE_W,
Expand Down Expand Up @@ -218,6 +219,8 @@ export function FlowCanvas({
// has an index signature). Same reference when nothing needed migrating, so
// the memos keyed on `nodes` are unaffected.
const nodes = React.useMemo(() => withCanonicalGeometry(storedNodes), [storedNodes]);
// An approval step's label in the viewer's language, where the package translates it.
const labelTranslations = useFlowLabelTranslationsContext();

const viewportRef = React.useRef<HTMLDivElement>(null);
// objectui#11795 — the viewport width the diagram was last framed at (the
Expand Down Expand Up @@ -1294,7 +1297,7 @@ export function FlowCanvas({
id={node.id}
locale={locale}
type={node.type}
label={node.label || node.id}
label={labelTranslations?.stepLabel(node) ?? (node.label || node.id)}
summary={nodeSummary(node, locale, objectLabels)}
position={pos}
selected={selectedId === node.id}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,9 @@ import type { MetadataPreviewProps, MetadataSelection } from '../preview-registr
import { PreviewShell, PreviewMessage, PreviewErrorBoundary } from './PreviewShell.js';
import { appendArray } from '../inspectors/_shared.js';
import { useObjectCatalog, type ObjectCatalogEntry } from '../inspectors/ObjectPicker.js';
import { t as tr, translateFlowMeta } from '../i18n.js';
import { t as tr, tFormat, translateFlowMeta } from '../i18n.js';
import { FlowCanvas } from './FlowCanvas.js';
import { FlowLabelTranslationsProvider, useFlowLabelTranslations, type FlowLabelTranslations } from './flow-label-translations.js';
import { defaultNodeLabel, flowEdgeDisplayName, flowNodeDisplayName } from './flow-canvas-parts.js';
import { edgeKey, type FlowDesignerEdge, type FlowDesignerNode } from './flow-canvas-layout.js';
import { NESTED_NODE_KIND, parseNestedNodeId, encodeNestedNodeId, locateFlowNode } from '../inspectors/flow-nested-selection.js';
Expand Down Expand Up @@ -125,12 +126,18 @@ export function FlowPreviewChromeProvider({ value, children }: { value: FlowPrev
* node with its type's display name ("Node · Decision"), or a connection — and
* the title is the one the inspector's own header would show (the node's label
* or id; the connection's `source → target`, each end by its node's name).
*
* An `approval` node's title is its step label in the viewer's language when
* the package bundle carries one (`translations`, see
* `flow-label-translations.tsx`); `source` is then the authored label, which
* the header shows beside it, since Studio is where that label is authored.
*/
export function flowSelectionHeading(
draft: Record<string, unknown>,
selection: MetadataSelection,
locale?: string,
): { eyebrow: string; title: string } {
translations?: FlowLabelTranslations,
): { eyebrow: string; title: string; source?: string } {
if (selection.kind === 'edge') {
const edges = Array.isArray(draft.edges) ? (draft.edges as FlowDesignerEdge[]) : [];
const edge = edges.find((e, i) => edgeKey(e, i) === selection.id);
Expand All @@ -150,12 +157,32 @@ export function flowSelectionHeading(
return { eyebrow: kind, title: selection.label ?? nested?.nodeId ?? selection.id };
}
const type = typeof node.type === 'string' ? node.type : '';
const authored = (typeof node.label === 'string' && node.label) || node.id;
const translated = translations?.stepLabel(node);
return {
eyebrow: type ? `${kind} · ${defaultNodeLabel(type, locale)}` : kind,
title: (typeof node.label === 'string' && node.label) || node.id,
title: translated ?? authored,
...(translated !== undefined && translated !== authored ? { source: authored } : {}),
};
}

/**
* A {@link flowSelectionHeading}'s title as a host's inspector header draws
* it: the title, and under it the authored label when a translation stands in
* for it.
*/
export function FlowSelectionTitle({ heading, locale }: { heading: { title: string; source?: string }; locale?: string }) {
if (heading.source === undefined) return <>{heading.title}</>;
return (
<>
{heading.title}
<span data-testid="flow-selection-source" className="block truncate text-xs font-normal text-muted-foreground">
{tFormat('engine.inspector.flowNode.sourceLabel', locale, { label: heading.source })}
</span>
</>
);
}

/**
* The header's Trigger pill (objectui#11779): the trigger the engine arms this
* flow on, read from its Start node by the spec's own resolver
Expand Down Expand Up @@ -465,6 +492,7 @@ export function FlowPreview({ draft, editing, selection, onSelectionChange, onPa

// Run history needs the published flow name (the engine keys runs by it).
const flowName = typeof d.name === 'string' && d.name ? d.name : '';
const labelTranslations = useFlowLabelTranslations(flowName);

const empty = canEdit ? (
<div className="p-3">
Expand All @@ -490,6 +518,8 @@ export function FlowPreview({ draft, editing, selection, onSelectionChange, onPa
const canvasEl = nodes.length === 0 ? (
<div className="h-full">{empty}</div>
) : (
// The approval steps' labels in the viewer's language (flow-label-translations.tsx).
<FlowLabelTranslationsProvider value={labelTranslations}>
<FlowCanvas
nodes={nodes}
edges={edges}
Expand Down Expand Up @@ -530,6 +560,7 @@ export function FlowPreview({ draft, editing, selection, onSelectionChange, onPa
onPatch={onPatch}
mintNodeId={mintNodeId}
/>
</FlowLabelTranslationsProvider>
);

// The bar's four panel toggles, as the designer has them: one panel at a time beside the canvas.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license.

/**
* The flow designer's reading of a package's `flows` translations: the flow's
* own label (`flows.<flow>.label`) and an `approval` node's step label
* (`flows.<flow>.approvals.<node_id>.label`), in the viewer's language.
*
* The bundle is the one `FlowRunner` reads, `activeFlowsBundle` over the
* i18next resource tree the console already loaded, and the flow label goes
* through the spec's `translateFlow`, the runner's resolution path. So the
* designer and the runner cannot disagree about a flow's name, and there is no
* second loader.
*
* Only those two keys exist in the spec's `flows` group for a label the
* designer draws. A decision or other node's label and an edge label have no
* key, so they stay the authored text.
*
* Display only: the draft is never rewritten with a translation, so an edit,
* a rename or a save keeps working on the authored label. Studio is the
* authoring surface, so the inspector also shows the authored label beside a
* translation (`flowSelectionHeading`'s `source`).
*/

import * as React from 'react';
import { useObjectTranslation } from '@object-ui/react';
import { translateFlow, type TranslationBundle } from '@objectstack/spec/system';
import { activeFlowsBundle } from '../../../utils/flowsBundle.js';

/** A flow's translated labels in one language. Every member answers `undefined` where the bundle carries nothing. */
export interface FlowLabelTranslations {
/** `flows.<flow>.label`. */
flowLabel?: string;
/** `flows.<flow>.approvals.<node_id>.label`, for an `approval` node only. */
stepLabel: (node: { id?: unknown; type?: unknown }) => string | undefined;
}

/**
* The translated labels for `flowName` out of `bundle` in `language`, or
* `undefined` when there is no bundle (nothing to translate with).
*/
export function flowLabelTranslations(
bundle: TranslationBundle | undefined,
flowName: string,
language: string,
): FlowLabelTranslations | undefined {
if (!bundle || !flowName) return undefined;
const flowLabel = translateFlow<{ name: string; label?: string }>({ name: flowName }, bundle, { locale: language }).label;
const approvals = bundle[language]?.flows?.[flowName]?.approvals;
return {
flowLabel: typeof flowLabel === 'string' && flowLabel ? flowLabel : undefined,
stepLabel: (node) => {
if (node.type !== 'approval' || typeof node.id !== 'string' || !node.id) return undefined;
const label = approvals?.[node.id]?.label;
return typeof label === 'string' && label ? label : undefined;
},
};
}

/**
* {@link flowLabelTranslations} for `flowName` in the console's active
* language. Recomputed on every render: a handful of property reads that
* follow the live resource tree and a language switch.
*/
export function useFlowLabelTranslations(flowName: string): FlowLabelTranslations | undefined {
const { i18n, language } = useObjectTranslation();
return flowLabelTranslations(activeFlowsBundle(i18n, language, flowName), flowName, language);
}

const FlowLabelTranslationsContext = React.createContext<FlowLabelTranslations | undefined>(undefined);

/** Hands {@link FlowLabelTranslations} to the canvas below it. */
export const FlowLabelTranslationsProvider = FlowLabelTranslationsContext.Provider;

/** The translations the nearest provider hands in, or `undefined` outside one. */
export function useFlowLabelTranslationsContext(): FlowLabelTranslations | undefined {
return React.useContext(FlowLabelTranslationsContext);
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import { REGION_BLOCK_PAD, REGION_GAP, REGION_LABEL_H } from './flow-region-metr
// stable region `key`. It lives in its own module so the nested-node inspector
// breadcrumb reads the same rows (objectui#10696).
import { displayRegionLabel } from './flow-region-label.js';
import { useFlowLabelTranslationsContext } from './flow-label-translations.js';

/**
* Node box inside a region — a compact echo of `NodeCard`. Read-only by default
Expand All @@ -60,6 +61,8 @@ function RegionNode({
}) {
const tone = nodeTone(node.type);
const interactive = !!onSelect;
// An approval step's label in the viewer's language, where the package translates it.
const labelTranslations = useFlowLabelTranslationsContext();
return (
<div
data-region-node-id={node.id}
Expand Down Expand Up @@ -96,8 +99,8 @@ function RegionNode({
<NodeTypeIcon type={node.type} className={cn('h-4 w-4', tone.icon)} />
</div>
<div className="min-w-0 flex-1">
<div className="truncate text-[12px] font-semibold leading-tight text-foreground" title={node.label || node.id}>
{node.label || node.id}
<div className="truncate text-[12px] font-semibold leading-tight text-foreground" title={labelTranslations?.stepLabel(node) ?? (node.label || node.id)}>
{labelTranslations?.stepLabel(node) ?? (node.label || node.id)}
</div>
<div className={cn('text-[10px] font-semibold uppercase tracking-[0.08em]', tone.label)}>{node.type}</div>
</div>
Expand Down
Loading
Loading