Skip to content

Commit 17889e6

Browse files
committed
test+changeset: pin the reserved locale channel at the console runner owners
Co-authored-by: Claude <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01XbF1LGGBRTWwTfLay1d51s
1 parent 7f9627d commit 17889e6

2 files changed

Lines changed: 143 additions & 0 deletions

File tree

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
---
2+
'@objectstack/formula': patch
3+
'@objectstack/service-automation': patch
4+
'@object-ui/core': patch
5+
'@object-ui/react': patch
6+
---
7+
8+
fix(formula,core,react): a template's formatter locale comes from a reserved context channel, so a root named `locale` is just a value, and the console passes the viewer's display locale
9+
10+
Clause-②: yes (narrowing)
11+
Metadata-protocol: none
12+
13+
<!-- adr-0087: not-required (no-migration-prescription) No metadata moves: no spec key, authorable spelling, object definition, stored row or stored shape is added, removed, renamed or re-shaped. What changes is which input sets the template dialect's formatter locale: the evaluate context's own `locale` field instead of a root named `locale`. A template that names a root `locale` keeps compiling and rendering; its hole still prints that value, only the number and date formatting no longer follows it. There is nothing for `objectstack migrate meta` to rewrite. The other categories are closed on facts: every package publishes (not unpublished); no ADR-0087 id is named or touched (not registered or already-registered); and no runtime interface, type or export is removed or narrowed: `EvalContext` gains an optional member and `fillTemplateHoles` an optional trailing argument (not runtime-interface-only, type-surface-only or runtime-export-retired). -->
14+
15+
The `template` dialect's formatters (`number`, `currency`, `percent`, `date`, `datetime`) used to take their locale from `extra.locale`, the same bag a caller passes the template's roots in. A template's roots are author-visible values, so a bulk action param or a flow variable named `locale` silently set the locale of every formatted value in the sentence, and no caller ever passed the viewer's locale.
16+
17+
- **`@objectstack/formula`**: `EvalContext` gains `locale?: string`, a reserved channel like `timezone`. It is not bound as a variable, and the template engine reads its formatter locale from it alone, defaulting to `en-US` when unset or empty. `extra.locale` is no longer read.
18+
- **`@objectstack/service-automation`**: a flow text slot's formatters render in `en-US`, and a flow variable named `locale` is a value like any other. The renderer's doc said the variable was also the formatters' locale; that line is gone.
19+
- **`@object-ui/core`**: `fillTemplateHoles(source, roots, locale?)` passes `locale` on the engine's reserved channel, apart from the roots. `ActionRunner.setDisplayLocale(read)` installs where the runner reads the viewer's display locale; `confirmText` and `successMessage` templates format in it, and a `callContext` dispatch keeps it.
20+
- **`@object-ui/react`**: `<ActionProvider>` and `useActionRunner` install `useDisplayLocale()` on the runner they build, read when a template is filled, so a locale switch reaches a runner built before it.
21+
22+
**BREAKING** (a narrowing) for a caller that set the formatter locale through a root: a template evaluated with `extra: { locale }` (or a flow variable named `locale`) now formats in `en-US` and prints the value only where a hole names it. None was found in this repository: the one test that did so now passes `locale` on the context. What to do: pass the locale as the evaluate context's `locale` (`templateEngine.evaluate(expr, { extra: roots, locale })`), or as `fillTemplateHoles`'s third argument.
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
/**
2+
* ObjectUI
3+
* Copyright (c) 2024-present ObjectStack Inc.
4+
*
5+
* This source code is licensed under the MIT license found in the
6+
* LICENSE file in the root directory of this source tree.
7+
*/
8+
9+
/**
10+
* objectstack#26232 — both React owners of an `ActionRunner` install the
11+
* viewer's display locale on it (`useActionRunnerDisplayLocale`), so a copy
12+
* template's formatters render in `useDisplayLocale()` instead of `en-US`.
13+
*
14+
* Measured by running a real action through a real provider under a real
15+
* `I18nProvider` and `LocalizationProvider`, and reading what the host's
16+
* confirm handler and `onToast` receive. Expected strings come from `Intl`
17+
* itself, so a newer ICU cannot drift them.
18+
*/
19+
import { describe, it, expect, vi, afterEach } from 'vitest';
20+
import { act, cleanup, renderHook } from '@testing-library/react';
21+
import React from 'react';
22+
import { I18nProvider, LocalizationProvider, useObjectTranslation } from '@object-ui/i18n';
23+
import type { ActionResult, ToastHandler } from '@object-ui/core';
24+
import { ActionProvider, useAction } from '../ActionContext';
25+
import { useActionRunner } from '../../hooks/useActionRunner';
26+
27+
afterEach(() => cleanup());
28+
29+
const AMOUNT = 1234567.5;
30+
const money = (tag: string) => new Intl.NumberFormat(tag, { style: 'currency', currency: 'EUR' }).format(AMOUNT);
31+
const CONFIG = { defaultLanguage: 'en', detectBrowserLanguage: false } as const;
32+
const SUCCEEDS = async (): Promise<ActionResult> => ({ success: true, data: { total: AMOUNT } });
33+
const SUCCESS_COPY = { dialect: 'template' as const, source: 'Total {{ result.total | currency:EUR }}' };
34+
35+
function providerHarness(locale: string | undefined, onToast: ToastHandler, onConfirm?: (m: string) => Promise<boolean>) {
36+
const wrapper = ({ children }: { children: React.ReactNode }) => (
37+
<I18nProvider config={CONFIG} persistLanguage={false}>
38+
<LocalizationProvider value={{ locale }}>
39+
<ActionProvider
40+
context={{ record: { amount: AMOUNT } }}
41+
onToast={onToast}
42+
onConfirm={onConfirm}
43+
handlers={{ script: SUCCEEDS }}
44+
>
45+
{children}
46+
</ActionProvider>
47+
</LocalizationProvider>
48+
</I18nProvider>
49+
);
50+
return renderHook(() => ({ action: useAction(), i18n: useObjectTranslation() }), { wrapper });
51+
}
52+
53+
describe('<ActionProvider> — copy templates format in the display locale (objectstack#26232)', () => {
54+
it('successMessage formats in the tenant display locale', async () => {
55+
expect(money('de-DE')).not.toBe(money('en-US'));
56+
const onToast = vi.fn<ToastHandler>();
57+
const { result } = providerHarness('de-DE', onToast);
58+
59+
await act(async () => {
60+
await result.current.action.execute({ type: 'script', name: 'reprice', successMessage: SUCCESS_COPY });
61+
});
62+
63+
expect(onToast).toHaveBeenCalledWith(`Total ${money('de-DE')}`, expect.objectContaining({ type: 'success' }));
64+
});
65+
66+
it('confirmText formats in the display locale; a `locale` param is a value it prints', async () => {
67+
const onConfirm = vi.fn(async (_message: string) => false);
68+
const { result } = providerHarness('de-DE', vi.fn<ToastHandler>(), onConfirm);
69+
70+
await act(async () => {
71+
await result.current.action.execute({
72+
type: 'script',
73+
name: 'reprice',
74+
confirmText: 'Set {{ record.amount | currency:EUR }} ({{ params.locale }})?',
75+
params: { locale: 'fr-FR' },
76+
});
77+
});
78+
79+
expect(onConfirm.mock.calls[0][0]).toBe(`Set ${money('de-DE')} (fr-FR)?`);
80+
});
81+
82+
it('with no tenant locale, the UI language is the display locale, and a switch reaches the built runner', async () => {
83+
const onToast = vi.fn<ToastHandler>();
84+
const { result } = providerHarness(undefined, onToast);
85+
const runnerBefore = result.current.action.runner;
86+
87+
await act(async () => {
88+
await result.current.action.execute({ type: 'script', name: 'reprice', successMessage: SUCCESS_COPY });
89+
});
90+
expect(onToast).toHaveBeenLastCalledWith(`Total ${money('en')}`, expect.anything());
91+
92+
await act(async () => {
93+
await result.current.i18n.changeLanguage('ja');
94+
});
95+
expect(result.current.action.runner).toBe(runnerBefore);
96+
97+
await act(async () => {
98+
await result.current.action.execute({ type: 'script', name: 'reprice', successMessage: SUCCESS_COPY });
99+
});
100+
expect(onToast).toHaveBeenLastCalledWith(`Total ${money('ja')}`, expect.anything());
101+
});
102+
});
103+
104+
describe('useActionRunner — copy templates format in the display locale (objectstack#26232)', () => {
105+
it('successMessage formats in the tenant display locale', async () => {
106+
const onToast = vi.fn<ToastHandler>();
107+
const wrapper = ({ children }: { children: React.ReactNode }) => (
108+
<I18nProvider config={CONFIG} persistLanguage={false}>
109+
<LocalizationProvider value={{ locale: 'de-DE' }}>{children}</LocalizationProvider>
110+
</I18nProvider>
111+
);
112+
const { result } = renderHook(() => useActionRunner({ context: {}, onToast }), { wrapper });
113+
result.current.runner.registerHandler('script', SUCCEEDS);
114+
115+
await act(async () => {
116+
await result.current.execute({ type: 'script', name: 'reprice', successMessage: SUCCESS_COPY });
117+
});
118+
119+
expect(onToast).toHaveBeenCalledWith(`Total ${money('de-DE')}`, expect.objectContaining({ type: 'success' }));
120+
});
121+
});

0 commit comments

Comments
 (0)