@marimo-team/frontend 0.23.16-dev2 → 0.23.16-dev4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/assets/JsonOutput-CkCD-FgM.js +53 -0
- package/dist/assets/{add-connection-dialog-CEp3SJR2.js → add-connection-dialog-B-jCRNTN.js} +1 -1
- package/dist/assets/{agent-panel-By4A-Cqr.js → agent-panel-CHB6t7Zy.js} +1 -1
- package/dist/assets/{cell-editor-DdofA9oQ.js → cell-editor-B5JVq7_q.js} +1 -1
- package/dist/assets/{column-preview-BgZLUEOj.js → column-preview-Dne1dIt0.js} +1 -1
- package/dist/assets/{command-palette-DrI24FoL.js → command-palette-iOCqUIs1.js} +1 -1
- package/dist/assets/{edit-page-C37w54dQ.js → edit-page-7X3HYey7.js} +5 -5
- package/dist/assets/file-explorer-panel-Ntq1-VbF.js +26 -0
- package/dist/assets/{form-CgWNb44_.js → form-B6BkSCJb.js} +1 -1
- package/dist/assets/{index-Cl0lNhQU.js → index-CMZzy-dF.js} +14 -14
- package/dist/assets/{layout-BOwE7yok.js → layout-CUlZijwG.js} +3 -3
- package/dist/assets/{panels-CW-PUlv2.js → panels-Dj45fW-v.js} +1 -1
- package/dist/assets/{reveal-component-DqMRxmUr.js → reveal-component-BLMTmd8T.js} +1 -1
- package/dist/assets/{run-page-s6Mf8p78.js → run-page-B6pgDYEJ.js} +1 -1
- package/dist/assets/{scratchpad-panel-CXsxdMB-.js → scratchpad-panel-B0mEqBNk.js} +1 -1
- package/dist/assets/{session-panel-CmHetqAb.js → session-panel-Y0Oon0sD.js} +1 -1
- package/dist/assets/{skeleton-B_4CZVEm.js → skeleton-D3QPxqWC.js} +1 -1
- package/dist/assets/{state-BXbiRXZk.js → state-BZQc7xxL.js} +1 -1
- package/dist/assets/{useNotebookActions-DJKDd0gM.js → useNotebookActions-DTXAoukG.js} +1 -1
- package/dist/index.html +5 -5
- package/package.json +1 -1
- package/src/components/data-table/column-formatting/feature.ts +2 -1
- package/src/components/editor/file-tree/__tests__/file-render-mode.test.ts +27 -0
- package/src/components/editor/file-tree/__tests__/file-viewer.test.tsx +150 -0
- package/src/components/editor/file-tree/file-preview-metadata.tsx +51 -0
- package/src/components/editor/file-tree/file-render-mode.ts +32 -0
- package/src/components/editor/file-tree/file-viewer.tsx +76 -50
- package/src/core/i18n/__tests__/locale-provider.test.tsx +54 -118
- package/src/core/i18n/__tests__/locale.test.ts +113 -0
- package/src/core/i18n/locale-provider.tsx +1 -19
- package/src/core/i18n/locale.ts +71 -0
- package/src/core/network/types.ts +3 -1
- package/dist/assets/JsonOutput-DnGSkLeP.js +0 -53
- package/dist/assets/file-explorer-panel-DbdoJbbF.js +0 -26
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
/* Copyright 2026 Marimo. All rights reserved. */
|
|
2
|
+
|
|
3
|
+
import { DownloadIcon } from "lucide-react";
|
|
4
|
+
import { useLocale } from "react-aria";
|
|
5
|
+
import { Button } from "@/components/ui/button";
|
|
6
|
+
import type { FileInfo } from "@/core/network/types";
|
|
7
|
+
import { formatBytes } from "@/utils/formatting";
|
|
8
|
+
|
|
9
|
+
interface Props {
|
|
10
|
+
file: FileInfo;
|
|
11
|
+
mimeType: string;
|
|
12
|
+
message: string;
|
|
13
|
+
onDownload?: () => void;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export const FilePreviewMetadata = ({
|
|
17
|
+
file,
|
|
18
|
+
mimeType,
|
|
19
|
+
message,
|
|
20
|
+
onDownload,
|
|
21
|
+
}: Props) => {
|
|
22
|
+
const { locale } = useLocale();
|
|
23
|
+
return (
|
|
24
|
+
<div className="p-4 text-sm">
|
|
25
|
+
<div className="grid grid-cols-[auto_1fr] gap-x-4 gap-y-2">
|
|
26
|
+
<span className="text-muted-foreground font-medium">Path</span>
|
|
27
|
+
<span className="font-mono text-xs break-all">{file.path}</span>
|
|
28
|
+
<span className="text-muted-foreground font-medium">Type</span>
|
|
29
|
+
<span>{mimeType}</span>
|
|
30
|
+
{file.size != null && (
|
|
31
|
+
<>
|
|
32
|
+
<span className="text-muted-foreground font-medium">Size</span>
|
|
33
|
+
<span>{formatBytes(file.size, locale)}</span>
|
|
34
|
+
</>
|
|
35
|
+
)}
|
|
36
|
+
</div>
|
|
37
|
+
<div className="mt-4 text-muted-foreground italic">{message}</div>
|
|
38
|
+
{onDownload && (
|
|
39
|
+
<Button
|
|
40
|
+
variant="outline"
|
|
41
|
+
size="sm"
|
|
42
|
+
className="mt-4"
|
|
43
|
+
onClick={onDownload}
|
|
44
|
+
>
|
|
45
|
+
<DownloadIcon className="h-3.5 w-3.5 mr-2" />
|
|
46
|
+
Download
|
|
47
|
+
</Button>
|
|
48
|
+
)}
|
|
49
|
+
</div>
|
|
50
|
+
);
|
|
51
|
+
};
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
/* Copyright 2026 Marimo. All rights reserved. */
|
|
2
|
+
|
|
3
|
+
import { isMediaMime, MIME_TO_LANGUAGE } from "./renderers";
|
|
4
|
+
|
|
5
|
+
export type FileRenderMode = "text" | "csv" | "media" | "unsupported";
|
|
6
|
+
|
|
7
|
+
export function getFileRenderMode(
|
|
8
|
+
mimeType: string | null | undefined,
|
|
9
|
+
isBase64: boolean | undefined,
|
|
10
|
+
): FileRenderMode {
|
|
11
|
+
const mime = mimeType || "text/plain";
|
|
12
|
+
|
|
13
|
+
if (isMediaMime(mime)) {
|
|
14
|
+
return "media";
|
|
15
|
+
}
|
|
16
|
+
if (isBase64) {
|
|
17
|
+
return "unsupported";
|
|
18
|
+
}
|
|
19
|
+
if (mime === "text/csv") {
|
|
20
|
+
return "csv";
|
|
21
|
+
}
|
|
22
|
+
if (
|
|
23
|
+
mime.startsWith("text/") ||
|
|
24
|
+
(mime !== "default" && mime in MIME_TO_LANGUAGE)
|
|
25
|
+
) {
|
|
26
|
+
return "text";
|
|
27
|
+
}
|
|
28
|
+
if (mimeType == null) {
|
|
29
|
+
return "text";
|
|
30
|
+
}
|
|
31
|
+
return "unsupported";
|
|
32
|
+
}
|
|
@@ -25,11 +25,11 @@ import { copyToClipboard } from "@/utils/copy";
|
|
|
25
25
|
import type { Base64String } from "@/utils/json/base64";
|
|
26
26
|
import { downloadFile } from "./download";
|
|
27
27
|
import { FilePreviewHeader } from "./file-header";
|
|
28
|
-
import {
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
28
|
+
import { FilePreviewMetadata } from "./file-preview-metadata";
|
|
29
|
+
import { getFileRenderMode } from "./file-render-mode";
|
|
30
|
+
import { FileContentRenderer } from "./renderers";
|
|
31
|
+
|
|
32
|
+
export const MAX_FILE_PREVIEW_BYTES = 10 * 1024 * 1024;
|
|
33
33
|
|
|
34
34
|
interface Props {
|
|
35
35
|
file: FileInfo;
|
|
@@ -50,9 +50,15 @@ export const FileViewer: React.FC<Props> = ({ file, onOpenNotebook }) => {
|
|
|
50
50
|
|
|
51
51
|
const { data, isPending, error, setData, refetch } =
|
|
52
52
|
useAsyncData(async () => {
|
|
53
|
-
const details = await sendFileDetails({
|
|
54
|
-
|
|
55
|
-
|
|
53
|
+
const details = await sendFileDetails({
|
|
54
|
+
path: file.path,
|
|
55
|
+
maxBytes: MAX_FILE_PREVIEW_BYTES,
|
|
56
|
+
});
|
|
57
|
+
const mode = getFileRenderMode(details.mimeType, details.isBase64);
|
|
58
|
+
if (!details.isTooLarge && mode === "text") {
|
|
59
|
+
const contents = details.contents || "";
|
|
60
|
+
setInternalValue(unsavedContentsForFile.get(file.path) ?? contents);
|
|
61
|
+
}
|
|
56
62
|
return details;
|
|
57
63
|
}, [file.path]);
|
|
58
64
|
|
|
@@ -72,13 +78,19 @@ export const FileViewer: React.FC<Props> = ({ file, onOpenNotebook }) => {
|
|
|
72
78
|
);
|
|
73
79
|
};
|
|
74
80
|
|
|
75
|
-
// On file change or unmount, save the unsaved contents
|
|
81
|
+
// On file change or unmount, save the unsaved contents. Only editable text
|
|
82
|
+
// previews participate in editor state; oversized and non-text responses
|
|
83
|
+
// carry no draft to preserve.
|
|
76
84
|
// We use a ref for internalValue so we don't call this effect on each keystroke
|
|
77
85
|
const internalValueRef = useRef<string>(internalValue);
|
|
78
86
|
internalValueRef.current = internalValue;
|
|
79
87
|
useEffect(() => {
|
|
80
88
|
return () => {
|
|
81
|
-
if (
|
|
89
|
+
if (
|
|
90
|
+
data?.contents == null ||
|
|
91
|
+
data.isTooLarge ||
|
|
92
|
+
getFileRenderMode(data.mimeType, data.isBase64) !== "text"
|
|
93
|
+
) {
|
|
82
94
|
return;
|
|
83
95
|
}
|
|
84
96
|
const draft = internalValueRef.current;
|
|
@@ -88,7 +100,13 @@ export const FileViewer: React.FC<Props> = ({ file, onOpenNotebook }) => {
|
|
|
88
100
|
unsavedContentsForFile.set(file.path, draft);
|
|
89
101
|
}
|
|
90
102
|
};
|
|
91
|
-
}, [
|
|
103
|
+
}, [
|
|
104
|
+
file.path,
|
|
105
|
+
data?.contents,
|
|
106
|
+
data?.isTooLarge,
|
|
107
|
+
data?.mimeType,
|
|
108
|
+
data?.isBase64,
|
|
109
|
+
]);
|
|
92
110
|
|
|
93
111
|
if (error) {
|
|
94
112
|
return <ErrorBanner error={error} />;
|
|
@@ -99,7 +117,10 @@ export const FileViewer: React.FC<Props> = ({ file, onOpenNotebook }) => {
|
|
|
99
117
|
}
|
|
100
118
|
|
|
101
119
|
const mimeType = data.mimeType || "text/plain";
|
|
102
|
-
const
|
|
120
|
+
const renderMode = getFileRenderMode(data.mimeType, data.isBase64);
|
|
121
|
+
const isText = renderMode === "text";
|
|
122
|
+
const isCsv = renderMode === "csv";
|
|
123
|
+
const isMedia = renderMode === "media";
|
|
103
124
|
const isActiveNotebook =
|
|
104
125
|
currentNotebookFilename &&
|
|
105
126
|
data.file.isMarimoFile &&
|
|
@@ -108,22 +129,27 @@ export const FileViewer: React.FC<Props> = ({ file, onOpenNotebook }) => {
|
|
|
108
129
|
// but this is an okay heuristic for now.
|
|
109
130
|
file.path.endsWith(`/${currentNotebookFilename}`));
|
|
110
131
|
|
|
111
|
-
if (!data.contents && !isEditable) {
|
|
112
|
-
// Show details instead of contents
|
|
113
|
-
return (
|
|
114
|
-
<div className="grid grid-cols-2 gap-2 p-6">
|
|
115
|
-
<div className="font-bold text-muted-foreground">Name</div>
|
|
116
|
-
<div>{data.file.name}</div>
|
|
117
|
-
<div className="font-bold text-muted-foreground">Type</div>
|
|
118
|
-
<div>{mimeType}</div>
|
|
119
|
-
</div>
|
|
120
|
-
);
|
|
121
|
-
}
|
|
122
|
-
|
|
123
132
|
const handleDownload = async () => {
|
|
124
133
|
await downloadFile(file.path, data.file.name);
|
|
125
134
|
};
|
|
126
135
|
|
|
136
|
+
const saveKeymapExtension = keymap.of([
|
|
137
|
+
{
|
|
138
|
+
key: hotkeys.getHotkey("global.save").key,
|
|
139
|
+
stopPropagation: true,
|
|
140
|
+
run: () => {
|
|
141
|
+
if (internalValue !== data.contents) {
|
|
142
|
+
void handleSaveFile();
|
|
143
|
+
return true;
|
|
144
|
+
}
|
|
145
|
+
return false;
|
|
146
|
+
},
|
|
147
|
+
},
|
|
148
|
+
]);
|
|
149
|
+
|
|
150
|
+
// Defined before the metadata-only returns so refresh, download, and
|
|
151
|
+
// open-notebook stay available while edit-only copy/save are withheld from
|
|
152
|
+
// non-text previews.
|
|
127
153
|
const header = (
|
|
128
154
|
<FilePreviewHeader
|
|
129
155
|
filename={data.file.name}
|
|
@@ -142,7 +168,7 @@ export const FileViewer: React.FC<Props> = ({ file, onOpenNotebook }) => {
|
|
|
142
168
|
</Button>
|
|
143
169
|
</Tooltip>
|
|
144
170
|
)}
|
|
145
|
-
{
|
|
171
|
+
{isText && (
|
|
146
172
|
<>
|
|
147
173
|
<Tooltip content="Copy contents to clipboard">
|
|
148
174
|
<Button
|
|
@@ -172,8 +198,23 @@ export const FileViewer: React.FC<Props> = ({ file, onOpenNotebook }) => {
|
|
|
172
198
|
/>
|
|
173
199
|
);
|
|
174
200
|
|
|
175
|
-
|
|
176
|
-
|
|
201
|
+
if (data.isTooLarge || renderMode === "unsupported") {
|
|
202
|
+
return (
|
|
203
|
+
<>
|
|
204
|
+
{header}
|
|
205
|
+
<FilePreviewMetadata
|
|
206
|
+
file={data.file}
|
|
207
|
+
mimeType={mimeType}
|
|
208
|
+
message={
|
|
209
|
+
data.isTooLarge
|
|
210
|
+
? "File is too large to preview."
|
|
211
|
+
: "This file type cannot be previewed."
|
|
212
|
+
}
|
|
213
|
+
onDownload={disableFileDownloads ? undefined : handleDownload}
|
|
214
|
+
/>
|
|
215
|
+
</>
|
|
216
|
+
);
|
|
217
|
+
}
|
|
177
218
|
|
|
178
219
|
const warningBanner = isText && isActiveNotebook && (
|
|
179
220
|
<Alert variant="warning" className="rounded-none">
|
|
@@ -192,35 +233,20 @@ export const FileViewer: React.FC<Props> = ({ file, onOpenNotebook }) => {
|
|
|
192
233
|
<FileContentRenderer
|
|
193
234
|
mimeType={mimeType}
|
|
194
235
|
contents={
|
|
195
|
-
|
|
196
|
-
?
|
|
197
|
-
:
|
|
198
|
-
?
|
|
199
|
-
:
|
|
236
|
+
isText
|
|
237
|
+
? internalValue
|
|
238
|
+
: isCsv
|
|
239
|
+
? (data.contents ?? undefined)
|
|
240
|
+
: undefined
|
|
200
241
|
}
|
|
201
242
|
mediaSource={
|
|
202
|
-
isMedia
|
|
243
|
+
isMedia && data.contents
|
|
203
244
|
? { base64: data.contents as Base64String, mime: mimeType }
|
|
204
245
|
: undefined
|
|
205
246
|
}
|
|
206
247
|
readOnly={!isText}
|
|
207
|
-
onChange={setInternalValue}
|
|
208
|
-
extensions={[
|
|
209
|
-
// Command S for save
|
|
210
|
-
keymap.of([
|
|
211
|
-
{
|
|
212
|
-
key: hotkeys.getHotkey("global.save").key,
|
|
213
|
-
stopPropagation: true,
|
|
214
|
-
run: () => {
|
|
215
|
-
if (internalValue !== data.contents) {
|
|
216
|
-
handleSaveFile();
|
|
217
|
-
return true;
|
|
218
|
-
}
|
|
219
|
-
return false;
|
|
220
|
-
},
|
|
221
|
-
},
|
|
222
|
-
]),
|
|
223
|
-
]}
|
|
248
|
+
onChange={isText ? setInternalValue : undefined}
|
|
249
|
+
extensions={isText ? [saveKeymapExtension] : []}
|
|
224
250
|
/>
|
|
225
251
|
</>
|
|
226
252
|
);
|
|
@@ -8,20 +8,9 @@ import {
|
|
|
8
8
|
defaultUserConfig,
|
|
9
9
|
parseUserConfig,
|
|
10
10
|
} from "@/core/config/config-schema";
|
|
11
|
+
import { FALLBACK_LOCALE } from "../locale";
|
|
11
12
|
import { LocaleProvider } from "../locale-provider";
|
|
12
13
|
|
|
13
|
-
// Mock navigator.language with a getter
|
|
14
|
-
let mockNavigatorLanguage: string | undefined;
|
|
15
|
-
|
|
16
|
-
Object.defineProperty(window, "navigator", {
|
|
17
|
-
value: {
|
|
18
|
-
get language() {
|
|
19
|
-
return mockNavigatorLanguage;
|
|
20
|
-
},
|
|
21
|
-
},
|
|
22
|
-
writable: true,
|
|
23
|
-
});
|
|
24
|
-
|
|
25
14
|
// Mock react-aria-components I18nProvider
|
|
26
15
|
vi.mock("react-aria-components", () => ({
|
|
27
16
|
I18nProvider: ({
|
|
@@ -37,143 +26,90 @@ vi.mock("react-aria-components", () => ({
|
|
|
37
26
|
),
|
|
38
27
|
}));
|
|
39
28
|
|
|
29
|
+
function renderWithLocale(configLocale: string | null | undefined) {
|
|
30
|
+
const store = createStore();
|
|
31
|
+
store.set(
|
|
32
|
+
userConfigAtom,
|
|
33
|
+
parseUserConfig({ display: { locale: configLocale } }),
|
|
34
|
+
);
|
|
35
|
+
return render(
|
|
36
|
+
<Provider store={store}>
|
|
37
|
+
<LocaleProvider>
|
|
38
|
+
<div>Test content</div>
|
|
39
|
+
</LocaleProvider>
|
|
40
|
+
</Provider>,
|
|
41
|
+
);
|
|
42
|
+
}
|
|
43
|
+
|
|
40
44
|
describe("LocaleProvider", () => {
|
|
41
45
|
beforeEach(() => {
|
|
42
|
-
|
|
43
|
-
mockNavigatorLanguage = undefined;
|
|
46
|
+
vi.stubGlobal("navigator", { language: undefined as string | undefined });
|
|
44
47
|
});
|
|
45
48
|
|
|
46
49
|
afterEach(() => {
|
|
47
50
|
cleanup();
|
|
48
|
-
|
|
49
|
-
mockNavigatorLanguage = undefined;
|
|
51
|
+
vi.unstubAllGlobals();
|
|
50
52
|
vi.clearAllMocks();
|
|
51
53
|
});
|
|
52
54
|
|
|
53
|
-
it("
|
|
54
|
-
const
|
|
55
|
-
|
|
56
|
-
store.set(userConfigAtom, config);
|
|
57
|
-
|
|
58
|
-
const { getByTestId } = render(
|
|
59
|
-
<Provider store={store}>
|
|
60
|
-
<LocaleProvider>
|
|
61
|
-
<div>Test content</div>
|
|
62
|
-
</LocaleProvider>
|
|
63
|
-
</Provider>,
|
|
64
|
-
);
|
|
65
|
-
|
|
66
|
-
const i18nProvider = getByTestId("i18n-provider");
|
|
67
|
-
expect(i18nProvider).toBeInTheDocument();
|
|
68
|
-
expect(i18nProvider.dataset.locale).toBe(undefined);
|
|
69
|
-
expect(i18nProvider).toHaveTextContent("Test content");
|
|
55
|
+
it("passes a valid configured locale through unchanged", () => {
|
|
56
|
+
const { getByTestId } = renderWithLocale("es-ES");
|
|
57
|
+
expect(getByTestId("i18n-provider").dataset.locale).toBe("es-ES");
|
|
70
58
|
});
|
|
71
59
|
|
|
72
|
-
it("
|
|
73
|
-
|
|
74
|
-
const
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
const { getByTestId } = render(
|
|
78
|
-
<Provider store={store}>
|
|
79
|
-
<LocaleProvider>
|
|
80
|
-
<div>Test content</div>
|
|
81
|
-
</LocaleProvider>
|
|
82
|
-
</Provider>,
|
|
83
|
-
);
|
|
84
|
-
|
|
85
|
-
const i18nProvider = getByTestId("i18n-provider");
|
|
86
|
-
expect(i18nProvider).toBeInTheDocument();
|
|
87
|
-
expect(i18nProvider.dataset.locale).toBe(undefined);
|
|
88
|
-
expect(i18nProvider).toHaveTextContent("Test content");
|
|
60
|
+
it("prefers the browser locale when config is unset", () => {
|
|
61
|
+
vi.stubGlobal("navigator", { language: "de-DE" });
|
|
62
|
+
const { getByTestId } = renderWithLocale(null);
|
|
63
|
+
expect(getByTestId("i18n-provider").dataset.locale).toBe("de-DE");
|
|
89
64
|
});
|
|
90
65
|
|
|
91
|
-
it(
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
<Provider store={store}>
|
|
99
|
-
<LocaleProvider>
|
|
100
|
-
<div>Test content</div>
|
|
101
|
-
</LocaleProvider>
|
|
102
|
-
</Provider>,
|
|
103
|
-
);
|
|
66
|
+
it.each([null, undefined])(
|
|
67
|
+
"falls back to %s config + unusable browser locale",
|
|
68
|
+
(configLocale) => {
|
|
69
|
+
const { getByTestId } = renderWithLocale(configLocale);
|
|
70
|
+
expect(getByTestId("i18n-provider").dataset.locale).toBe(FALLBACK_LOCALE);
|
|
71
|
+
},
|
|
72
|
+
);
|
|
104
73
|
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
expect(
|
|
74
|
+
it("sanitizes a POSIX-tagged browser locale (issue #9938)", () => {
|
|
75
|
+
vi.stubGlobal("navigator", { language: "en-US@posix" });
|
|
76
|
+
const { getByTestId } = renderWithLocale(null);
|
|
77
|
+
expect(getByTestId("i18n-provider").dataset.locale).toBe("en-US");
|
|
109
78
|
});
|
|
110
79
|
|
|
111
|
-
it("
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
const store = createStore();
|
|
116
|
-
const config = parseUserConfig({ display: { locale } });
|
|
117
|
-
store.set(userConfigAtom, config);
|
|
118
|
-
|
|
119
|
-
const { getByTestId } = render(
|
|
120
|
-
<Provider store={store}>
|
|
121
|
-
<LocaleProvider>
|
|
122
|
-
<div>Test content for {locale}</div>
|
|
123
|
-
</LocaleProvider>
|
|
124
|
-
</Provider>,
|
|
125
|
-
);
|
|
126
|
-
|
|
127
|
-
const i18nProvider = getByTestId("i18n-provider");
|
|
128
|
-
expect(i18nProvider.dataset.locale).toBe(locale);
|
|
129
|
-
expect(i18nProvider).toHaveTextContent(`Test content for ${locale}`);
|
|
130
|
-
|
|
131
|
-
// Clean up after each iteration
|
|
132
|
-
cleanup();
|
|
133
|
-
});
|
|
80
|
+
it("falls back to the browser locale when the configured locale is unusable", () => {
|
|
81
|
+
vi.stubGlobal("navigator", { language: "de-DE" });
|
|
82
|
+
const { getByTestId } = renderWithLocale("@@@");
|
|
83
|
+
expect(getByTestId("i18n-provider").dataset.locale).toBe("de-DE");
|
|
134
84
|
});
|
|
135
85
|
|
|
136
|
-
it("
|
|
86
|
+
it("resolves a concrete locale for the default config", () => {
|
|
87
|
+
vi.stubGlobal("navigator", { language: "fr-FR" });
|
|
137
88
|
const store = createStore();
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
const { getByText, getByRole } = render(
|
|
89
|
+
store.set(userConfigAtom, defaultUserConfig());
|
|
90
|
+
const { getByTestId } = render(
|
|
142
91
|
<Provider store={store}>
|
|
143
92
|
<LocaleProvider>
|
|
144
|
-
<div>
|
|
145
|
-
<h1>Test Heading</h1>
|
|
146
|
-
<p>Test paragraph</p>
|
|
147
|
-
<button type="button">Test Button</button>
|
|
148
|
-
</div>
|
|
93
|
+
<div>Default config test</div>
|
|
149
94
|
</LocaleProvider>
|
|
150
95
|
</Provider>,
|
|
151
96
|
);
|
|
152
|
-
|
|
153
|
-
expect(getByText("Test Heading")).toBeInTheDocument();
|
|
154
|
-
expect(getByText("Test paragraph")).toBeInTheDocument();
|
|
155
|
-
expect(getByRole("button", { name: "Test Button" })).toBeInTheDocument();
|
|
97
|
+
expect(getByTestId("i18n-provider").dataset.locale).toBe("fr-FR");
|
|
156
98
|
});
|
|
157
99
|
|
|
158
|
-
it("
|
|
159
|
-
mockNavigatorLanguage = "de-DE";
|
|
160
|
-
|
|
100
|
+
it("renders children", () => {
|
|
161
101
|
const store = createStore();
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
102
|
+
store.set(
|
|
103
|
+
userConfigAtom,
|
|
104
|
+
parseUserConfig({ display: { locale: "en-US" } }),
|
|
105
|
+
);
|
|
106
|
+
const { getByRole } = render(
|
|
166
107
|
<Provider store={store}>
|
|
167
108
|
<LocaleProvider>
|
|
168
|
-
<
|
|
109
|
+
<button type="button">Test Button</button>
|
|
169
110
|
</LocaleProvider>
|
|
170
111
|
</Provider>,
|
|
171
112
|
);
|
|
172
|
-
|
|
173
|
-
const i18nProvider = getByTestId("i18n-provider");
|
|
174
|
-
expect(i18nProvider).toBeInTheDocument();
|
|
175
|
-
// When no locale is specified in config, it should use navigator.language
|
|
176
|
-
expect(i18nProvider.dataset.locale).toBe("de-DE");
|
|
177
|
-
expect(i18nProvider).toHaveTextContent("Test content");
|
|
113
|
+
expect(getByRole("button", { name: "Test Button" })).toBeInTheDocument();
|
|
178
114
|
});
|
|
179
115
|
});
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
/* Copyright 2026 Marimo. All rights reserved. */
|
|
2
|
+
|
|
3
|
+
import { afterEach, describe, expect, it, vi } from "vitest";
|
|
4
|
+
import {
|
|
5
|
+
browserLocale,
|
|
6
|
+
FALLBACK_LOCALE,
|
|
7
|
+
isValidLocale,
|
|
8
|
+
normalizeLocale,
|
|
9
|
+
safeLocale,
|
|
10
|
+
} from "../locale";
|
|
11
|
+
|
|
12
|
+
describe("isValidLocale", () => {
|
|
13
|
+
it("accepts valid BCP 47 tags", () => {
|
|
14
|
+
expect(isValidLocale("en-US")).toBe(true);
|
|
15
|
+
expect(isValidLocale("de-DE")).toBe(true);
|
|
16
|
+
expect(isValidLocale("en")).toBe(true);
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
it("rejects empty and modifier-tainted tags", () => {
|
|
20
|
+
expect(isValidLocale("")).toBe(false);
|
|
21
|
+
expect(isValidLocale("en-US@posix")).toBe(false);
|
|
22
|
+
expect(isValidLocale("en_US.UTF-8")).toBe(false);
|
|
23
|
+
});
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
describe("normalizeLocale", () => {
|
|
27
|
+
it("keeps valid tags unchanged, including script + region", () => {
|
|
28
|
+
expect(normalizeLocale("de-DE")).toBe("de-DE");
|
|
29
|
+
expect(normalizeLocale("zh-Hant-TW")).toBe("zh-Hant-TW");
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
it("strips POSIX @modifiers (issue #9938)", () => {
|
|
33
|
+
expect(normalizeLocale("en-US@posix")).toBe("en-US");
|
|
34
|
+
expect(normalizeLocale("de-DE@euro")).toBe("de-DE");
|
|
35
|
+
// ICU-style unicode extension keywords are also dropped.
|
|
36
|
+
expect(normalizeLocale("de@collation=phonebook")).toBe("de");
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
it("strips charset suffixes and maps underscores", () => {
|
|
40
|
+
expect(normalizeLocale("en_US.UTF-8")).toBe("en-US");
|
|
41
|
+
expect(normalizeLocale("en_US")).toBe("en-US");
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
it("falls back to the language subtag when the full tag is invalid", () => {
|
|
45
|
+
// `-toolongsubtag` / `-oed` are structurally invalid, so `Intl.Locale`
|
|
46
|
+
// rejects the whole tag but accepts the leading `en`.
|
|
47
|
+
expect(normalizeLocale("en-toolongsubtag")).toBe("en");
|
|
48
|
+
expect(normalizeLocale("en-GB-oed")).toBe("en");
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
it("returns null for empty or unrecoverable tags", () => {
|
|
52
|
+
expect(normalizeLocale(null)).toBeNull();
|
|
53
|
+
expect(normalizeLocale(undefined)).toBeNull();
|
|
54
|
+
expect(normalizeLocale("")).toBeNull();
|
|
55
|
+
expect(normalizeLocale("@@@")).toBeNull();
|
|
56
|
+
// POSIX `C` locale: single-letter tag is not a valid language subtag.
|
|
57
|
+
expect(normalizeLocale("C")).toBeNull();
|
|
58
|
+
});
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
describe("browserLocale", () => {
|
|
62
|
+
afterEach(() => {
|
|
63
|
+
vi.unstubAllGlobals();
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
it("normalizes navigator.language", () => {
|
|
67
|
+
vi.stubGlobal("navigator", { language: "en-US@posix" });
|
|
68
|
+
expect(browserLocale()).toBe("en-US");
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
it("falls back when navigator.language is unusable", () => {
|
|
72
|
+
vi.stubGlobal("navigator", { language: "@@@" });
|
|
73
|
+
expect(browserLocale()).toBe(FALLBACK_LOCALE);
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
it("falls back when navigator is unavailable", () => {
|
|
77
|
+
vi.stubGlobal("navigator", undefined);
|
|
78
|
+
expect(browserLocale()).toBe(FALLBACK_LOCALE);
|
|
79
|
+
});
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
describe("safeLocale", () => {
|
|
83
|
+
afterEach(() => {
|
|
84
|
+
vi.unstubAllGlobals();
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
it("prefers a valid configured locale", () => {
|
|
88
|
+
vi.stubGlobal("navigator", { language: "de-DE" });
|
|
89
|
+
expect(safeLocale("fr-FR")).toBe("fr-FR");
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
it("normalizes configured locales before use", () => {
|
|
93
|
+
vi.stubGlobal("navigator", { language: "de-DE" });
|
|
94
|
+
expect(safeLocale("en_US")).toBe("en-US");
|
|
95
|
+
expect(safeLocale("en-US@posix")).toBe("en-US");
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
it("falls back to the browser locale when config is unusable", () => {
|
|
99
|
+
vi.stubGlobal("navigator", { language: "de-DE" });
|
|
100
|
+
expect(safeLocale(null)).toBe("de-DE");
|
|
101
|
+
expect(safeLocale("@@@")).toBe("de-DE");
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
it("normalizes the browser locale on the fallback path", () => {
|
|
105
|
+
vi.stubGlobal("navigator", { language: "en-US@posix" });
|
|
106
|
+
expect(safeLocale(null)).toBe("en-US");
|
|
107
|
+
});
|
|
108
|
+
|
|
109
|
+
it("falls back to FALLBACK_LOCALE when nothing is usable", () => {
|
|
110
|
+
vi.stubGlobal("navigator", { language: "@@@" });
|
|
111
|
+
expect(safeLocale(null)).toBe(FALLBACK_LOCALE);
|
|
112
|
+
});
|
|
113
|
+
});
|
|
@@ -4,6 +4,7 @@ import { useAtomValue } from "jotai";
|
|
|
4
4
|
import type { ReactNode } from "react";
|
|
5
5
|
import { I18nProvider } from "react-aria-components";
|
|
6
6
|
import { localeAtom } from "@/core/config/config";
|
|
7
|
+
import { safeLocale } from "./locale";
|
|
7
8
|
|
|
8
9
|
interface LocaleProviderProps {
|
|
9
10
|
children: ReactNode;
|
|
@@ -14,22 +15,3 @@ export const LocaleProvider = ({ children }: LocaleProviderProps) => {
|
|
|
14
15
|
|
|
15
16
|
return <I18nProvider locale={safeLocale(locale)}>{children}</I18nProvider>;
|
|
16
17
|
};
|
|
17
|
-
|
|
18
|
-
function safeLocale(locale: string | null | undefined) {
|
|
19
|
-
if (!locale) {
|
|
20
|
-
return navigator.language;
|
|
21
|
-
}
|
|
22
|
-
if (isValidLocale(locale)) {
|
|
23
|
-
return locale;
|
|
24
|
-
}
|
|
25
|
-
return navigator.language;
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
function isValidLocale(locale: string) {
|
|
29
|
-
try {
|
|
30
|
-
new Intl.NumberFormat(locale);
|
|
31
|
-
return true;
|
|
32
|
-
} catch {
|
|
33
|
-
return false;
|
|
34
|
-
}
|
|
35
|
-
}
|