GeezerVison is showing github.com — feat(catalog): the parameter tables follow the app language · u8array/ZPLab@a7ca5c6
File treeExpand file treeCollapse file tree Open diff view settings packages/core/src/catalog
Some content is hiddenLarge Commits have some content hidden by default. Use the searchbox below for content that may be hidden. Dismiss bannerExpand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: packages/core/src/catalog/commands.json +2-2Lines changed: 2 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 1692 | 1692 | { | 1693 | 1693 | "name": "m", | 1694 | 1694 | "meaning": "mode", | 1695 | | - "values": "N none, U UCC case, A automatic, D UCC/EAN", | | 1695 | + "values": "N none, U UCC Case Mode, A automatic, D UCC/EAN", | 1696 | 1696 | "default": "N" | 1697 | 1697 | } | 1698 | 1698 | ], | | 3087 | 3087 | { | 3088 | 3088 | "name": "r", | 3089 | 3089 | "meaning": "corner rounding", | 3090 | | - "values": "0 none to 8 heaviest", | | 3090 | + "values": "0 none to 8 strongest rounding", | 3091 | 3091 | "default": "0" | 3092 | 3092 | } | 3093 | 3093 | ], | |
Collapse file Copy file name to clipboardExpand all lines: packages/core/src/catalog/schema.ts +1-1Lines changed: 1 addition & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -13,7 +13,7 @@ export type ImportLossCause = (typeof IMPORT_LOSS_CAUSES)[number]; | 13 | 13 | const supportSchema = z.object({ web: z.enum(SUPPORT_LEVELS), desktop: z.enum(SUPPORT_LEVELS), docker: z.enum(SUPPORT_LEVELS).optional() }).strict(); | 14 | 14 | export type CommandSupport = Required<z.infer<typeof supportSchema>>; | 15 | 15 |
| 16 | | -/** Slot letters of the format line in order. English only like `title`, so no translated channel. */ | | 16 | +/** Slot letters of the format line in order. `name` stays English like `title`; the other cells are translated per locale, keyed by the English block. */ | 17 | 17 | const paramSchema = z.object({ | 18 | 18 | name: z.string().min(1), | 19 | 19 | meaning: z.string().min(1), | |
Collapse file Copy file name to clipboard +7Lines changed: 7 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +// Types for the node-only hash helper, so the translation test can import it under the app tsconfig. | | 2 | +import type { CommandReference } from "../packages/core/src/catalog/schema"; | | 3 | + | | 4 | +export declare function textHash(text: string): string; | | 5 | +export declare function referenceParamsText(reference: CommandReference): string; | | 6 | +export declare function referenceHash(reference: CommandReference): string; | | 7 | +export declare function referenceTokens(text: string): Set<string>; |
Collapse file Copy file name to clipboard +20Lines changed: 20 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +import { createHash } from "node:crypto"; | | 2 | + | | 3 | +/** The one hash scheme every catalog translation is pinned with. */ | | 4 | +export function textHash(text) { | | 5 | + return createHash("sha256").update(text).digest("hex").slice(0, 8); | | 6 | +} | | 7 | + | | 8 | +/** One text per reference block, so the test and the merge script pin the same English. */ | | 9 | +export function referenceParamsText(reference) { | | 10 | + return JSON.stringify(reference.params.map((p) => [p.meaning, p.values, p.default ?? ""])); | | 11 | +} | | 12 | + | | 13 | +export function referenceHash(reference) { | | 14 | + return textHash(referenceParamsText(reference)); | | 15 | +} | | 16 | + | | 17 | +/** The codes and numbers the user types, so a translation may not drop them. Two letter runs stay out: words like ID get translated. */ | | 18 | +export function referenceTokens(text) { | | 19 | + return new Set(text.match(/-?\d+(?:\.\d+)?|[\^~][A-Z@][A-Z0-9]|\b[A-Z](?=[\s,.]|$)|\b[A-Z][A-Z0-9/-]{2,}\b/g) ?? []); | | 20 | +} |
Collapse file Copy file name to clipboard +52Lines changed: 52 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +#!/usr/bin/env node | | 2 | +// The only writer that may stamp a reference hash. | | 3 | +// Usage: node scripts/catalog-reference-merge.mjs todo <locale> prints the blocks still to translate | | 4 | +// node scripts/catalog-reference-merge.mjs merge <locale> <in.json> writes them into src/locales/catalog/<locale>.json | | 5 | +import { readFileSync, writeFileSync } from "node:fs"; | | 6 | +import { join } from "node:path"; | | 7 | +import { ROOT, commandId, readCatalog } from "./catalog.mjs"; | | 8 | +import { referenceHash } from "./catalog-reference-hash.mjs"; | | 9 | + | | 10 | +const [mode, locale, input] = process.argv.slice(2); | | 11 | +if (!["todo", "merge"].includes(mode) || !locale || (mode === "merge" && !input)) throw new Error("usage: todo <locale> | merge <locale> <in.json>"); | | 12 | +const catalog = readCatalog().commands.filter((c) => c.reference?.params.length); | | 13 | +const file = join(ROOT, "src", "locales", "catalog", `${locale}.json`); | | 14 | +const rows = JSON.parse(readFileSync(file, "utf8")); | | 15 | +const current = (c) => rows[commandId(c)]?.reference?.hash === referenceHash(c.reference); | | 16 | + | | 17 | +if (mode === "todo") { | | 18 | + const todo = Object.fromEntries( | | 19 | + catalog.filter((c) => !current(c)).map((c) => [commandId(c), c.reference.params.map((p) => ({ name: p.name, meaning: p.meaning, values: p.values, ...(p.default ? { default: p.default } : {}) }))]), | | 20 | + ); | | 21 | + process.stdout.write(JSON.stringify(todo, null, 1) + "\n"); | | 22 | +} else { | | 23 | + const translated = JSON.parse(readFileSync(input, "utf8")); | | 24 | + const known = new Set(catalog.map(commandId)); | | 25 | + const unknown = Object.keys(translated).filter((id) => !known.has(id)); | | 26 | + if (unknown.length > 0) throw new Error(`no table to translate for ${unknown.join(", ")}`); | | 27 | + for (const c of catalog) { | | 28 | + const id = commandId(c); | | 29 | + const params = translated[id]; | | 30 | + if (!params) continue; | | 31 | + if (!rows[id]) throw new Error(`${id}: no summary row to attach the table to`); | | 32 | + checkParams(id, params, c.reference.params); | | 33 | + rows[id] = { | | 34 | + ...rows[id], | | 35 | + reference: { hash: referenceHash(c.reference), params: params.map((p) => ({ meaning: p.meaning, values: p.values, ...(p.default !== undefined ? { default: p.default } : {}) })) }, | | 36 | + }; | | 37 | + } | | 38 | + // The catalog order keeps the diff readable. A row whose command left the catalog drops out. | | 39 | + const ordered = Object.fromEntries(readCatalog().commands.map((c) => [commandId(c), rows[commandId(c)]]).filter(([, row]) => row)); | | 40 | + writeFileSync(file, JSON.stringify(ordered, null, 2) + "\n"); | | 41 | + console.log(`${locale}: ${Object.keys(translated).length} reference blocks written`); | | 42 | +} | | 43 | + | | 44 | +function checkParams(id, params, english) { | | 45 | + if (params.length !== english.length) throw new Error(`${id}: ${params.length} params, catalog has ${english.length}`); | | 46 | + // Siblings often share values and default, so only the slot name tells a reordered table apart. | | 47 | + params.forEach((p, i) => { | | 48 | + if (p.name !== undefined && p.name !== english[i].name) throw new Error(`${id}: slot ${i} is ${p.name}, catalog has ${english[i].name}`); | | 49 | + if (!p.meaning?.trim() || !p.values?.trim()) throw new Error(`${id} ${english[i].name}: empty meaning or values`); | | 50 | + if ((p.default === undefined) !== (english[i].default === undefined)) throw new Error(`${id} ${english[i].name}: default presence differs from the catalog`); | | 51 | + }); | | 52 | +} |
Collapse file Copy file name to clipboardExpand all lines: scripts/catalog.d.mts +1Lines changed: 1 addition & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 1 | 1 | // Types for the node-only catalog loader, so the parity test can import it under the app tsconfig. | 2 | 2 | import type { Catalog, ZplCommandEntry } from "../packages/core/src/catalog/schema"; | 3 | 3 |
| | 4 | +export declare const ROOT: string; | 4 | 5 | export declare const CAPABILITIES: readonly ["web", "desktop", "docker"]; | 5 | 6 | export declare const commandId: (entry: ZplCommandEntry) => string; | 6 | 7 | export declare const commandIds: (entry: ZplCommandEntry) => string[]; | |
Collapse file Copy file name to clipboardExpand all lines: scripts/catalog.mjs +1-1Lines changed: 1 addition & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -4,7 +4,7 @@ import { readFileSync } from 'node:fs'; | 4 | 4 | import { join } from 'node:path'; | 5 | 5 | import { fileURLToPath } from 'node:url'; | 6 | 6 |
| 7 | | -const ROOT = fileURLToPath(new URL('..', import.meta.url)); | | 7 | +export const ROOT = fileURLToPath(new URL('..', import.meta.url)); | 8 | 8 | const CATALOG = join(ROOT, 'packages', 'core', 'src', 'catalog', 'commands.json'); | 9 | 9 | export const CAPABILITIES = ['web', 'desktop', 'docker']; | 10 | 10 | const LEVELS = new Set(['yes', 'planned', 'no']); | |
Collapse file Copy file name to clipboardExpand all lines: src/components/Output/ZplCatalog.test.tsx +24Lines changed: 24 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -5,9 +5,13 @@ import { ZplCatalogList } from "./ZplCatalogList"; | 5 | 5 | import { ZplCatalogDetail } from "./ZplCatalogDetail"; | 6 | 6 | import { useCatalogSelection } from "../../hooks/useCatalogSelection"; | 7 | 7 | import type { CursorCommand } from "../../lib/zplLanguage"; | | 8 | +import { useCatalogSummaries } from "../../hooks/useCatalogSummaries"; | | 9 | + | | 10 | +vi.mock("../../hooks/useCatalogSummaries", () => ({ useCatalogSummaries: vi.fn(() => ({})) })); | 8 | 11 |
| 9 | 12 | afterEach(() => { | 10 | 13 | cleanup(); | | 14 | + vi.mocked(useCatalogSummaries).mockReturnValue({}); | 11 | 15 | localStorage.removeItem("zpl:section:catalog-list"); | 12 | 16 | localStorage.removeItem("zpl:section:catalog-detail"); | 13 | 17 | }); | @@ -57,6 +61,26 @@ describe("catalog panes", () => { | 57 | 61 | expect(second.queryByRole("list")).toBeNull(); | 58 | 62 | }); | 59 | 63 |
| | 64 | + it("shows the parameter table in the loaded language and falls back to English row by row", () => { | | 65 | + vi.mocked(useCatalogSummaries).mockReturnValue({ | | 66 | + "^FO": { hash: "x", summary: "Setzt die Feldposition.", reference: { hash: "y", params: [ | | 67 | + { meaning: "horizontale Position in Punkten", values: "0 bis 32000", default: "0" }, | | 68 | + { meaning: "vertikale Position in Punkten", values: "0 bis 32000", default: "0" }, | | 69 | + { meaning: "Ausrichtung", values: "0 links, 1 rechts, 2 automatisch", default: "letztes ^FW" }, | | 70 | + ] } }, | | 71 | + }); | | 72 | + const r = render(<CatalogPanes cursor={{ id: "^FO", from: 0, pointed: true }} onInsert={vi.fn()} />); | | 73 | + expect(detail(r.getByTestId).getByText("horizontale Position in Punkten")).toBeTruthy(); | | 74 | + expect(detail(r.getByTestId).getByText(/0 links, 1 rechts, 2 automatisch/)).toBeTruthy(); | | 75 | + r.unmount(); | | 76 | + vi.mocked(useCatalogSummaries).mockReturnValue({ "^FO": { hash: "x", summary: "Setzt die Feldposition.", reference: { hash: "y", params: [ | | 77 | + { meaning: "horizontale Position in Punkten", values: "0 bis 32000", default: "0" }, | | 78 | + ] } } }); | | 79 | + const mixed = render(<CatalogPanes cursor={{ id: "^FO", from: 0, pointed: true }} onInsert={vi.fn()} />); | | 80 | + expect(detail(mixed.getByTestId).getByText("horizontale Position in Punkten")).toBeTruthy(); | | 81 | + expect(detail(mixed.getByTestId).getByText("vertical position in dots")).toBeTruthy(); | | 82 | + }); | | 83 | + | 60 | 84 | it("folds the command list to a rail, keeps the description and remembers the fold", () => { | 61 | 85 | const first = render(<CatalogPanes cursor={{ id: "^LL", from: 0, pointed: true }} onInsert={vi.fn()} />); | 62 | 86 | fireEvent.click(first.getByRole("button", { name: "Command list" })); | |
Collapse file Copy file name to clipboardExpand all lines: src/components/Output/ZplCatalogDetail.tsx +9-8Lines changed: 9 additions & 8 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -3,7 +3,7 @@ import { commandId, commandLabel, type CommandSupport } from "@zplab/core/catalo | 3 | 3 | import { useT } from "../../hooks/useT"; | 4 | 4 | import { useCatalogSummaries } from "../../hooks/useCatalogSummaries"; | 5 | 5 | import type { CatalogSelection } from "../../hooks/useCatalogSelection"; | 6 | | -import { catalogEmptyText, syntaxSegments } from "../../lib/catalogText"; | | 6 | +import { catalogEmptyText, referenceRows, syntaxSegments } from "../../lib/catalogText"; | 7 | 7 | import { useCollapsibleState } from "../ui/useCollapsibleState"; | 8 | 8 | import { PaneToggle } from "../ui/PaneToggle"; | 9 | 9 | import { sectionHeadingCls } from "../ui/formStyles"; | @@ -22,6 +22,7 @@ export function ZplCatalogDetail({ selection, editorHasFocus }: { selection: Cat | 22 | 22 | const bodyId = useId(); | 23 | 23 | const [open, setOpen] = useCollapsibleState("catalog-detail", true); | 24 | 24 | const { entry, emptyReason, stepBack } = selection; | | 25 | + const translated = entry ? summaries[commandId(entry)] : undefined; | 25 | 26 | const emptyLine = emptyReason && <p className="text-muted leading-relaxed">{catalogEmptyText(emptyReason, t)}</p>; | 26 | 27 | // The no-cursor line would be read on every step through plain text. | 27 | 28 | const announced = emptyReason?.kind !== "noCursor"; | @@ -56,7 +57,7 @@ export function ZplCatalogDetail({ selection, editorHasFocus }: { selection: Cat | 56 | 57 | <span className="font-mono text-accent font-semibold shrink-0">{commandLabel(entry)}</span> | 57 | 58 | <span className="text-text font-medium">{entry.title}</span> | 58 | 59 | </div> | 59 | | - <p className="mt-1.5 text-text leading-relaxed">{summaries[commandId(entry)]?.summary ?? entry.summary}</p> | | 60 | + <p className="mt-1.5 text-text leading-relaxed">{translated?.summary ?? entry.summary}</p> | 60 | 61 | </> | 61 | 62 | )} | 62 | 63 | {announced && emptyLine} | @@ -73,14 +74,14 @@ export function ZplCatalogDetail({ selection, editorHasFocus }: { selection: Cat | 73 | 74 | </code> | 74 | 75 | {entry.reference.params.length > 0 && ( | 75 | 76 | <ul className="mt-2.5 space-y-2"> | 76 | | - {entry.reference.params.map((p, i) => ( | 77 | | - <li key={p.name} className="grid grid-cols-[0.875rem_1fr] gap-x-2 items-baseline"> | 78 | | - <span className={`font-mono font-semibold ${slotClass(i)}`}>{p.name}</span> | 79 | | - <span className="text-text leading-snug">{p.meaning}</span> | | 77 | + {referenceRows(entry.reference.params, translated).map((row, i) => ( | | 78 | + <li key={row.name} className="grid grid-cols-[0.875rem_1fr] gap-x-2 items-baseline"> | | 79 | + <span className={`font-mono font-semibold ${slotClass(i)}`}>{row.name}</span> | | 80 | + <span className="text-text leading-snug">{row.meaning}</span> | 80 | 81 | <span className="col-start-2 mt-0.5 text-[11px] text-muted leading-snug"> | 81 | | - {p.values} | | 82 | + {row.values} | 82 | 83 | {/* Without nowrap the separator can end a line and orphan the default. */} | 83 | | - {p.default && <span className="whitespace-nowrap">{` \u00b7 ${t.output.catalogDefault} ${p.default}`}</span>} | | 84 | + {row.default && <span className="whitespace-nowrap">{` \u00b7 ${t.output.catalogDefault} ${row.default}`}</span>} | 84 | 85 | </span> | 85 | 86 | </li> | 86 | 87 | ))} | |
Collapse file Copy file name to clipboard +32Lines changed: 32 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +// @vitest-environment jsdom | | 2 | +import { describe, it, expect, afterEach, vi } from "vitest"; | | 3 | +import { renderHook, act, cleanup, waitFor } from "@testing-library/react"; | | 4 | +import { useLabelStore } from "../store/labelStore"; | | 5 | +import { useCatalogSummaries } from "./useCatalogSummaries"; | | 6 | +import type * as CatalogSummariesModule from "../locales/catalogSummaries"; | | 7 | + | | 8 | +const { pending } = vi.hoisted(() => ({ pending: new Map<string, (rows: Record<string, unknown>) => void>() })); | | 9 | +vi.mock("../locales/catalogSummaries", async (importOriginal) => ({ | | 10 | + ...(await importOriginal<typeof CatalogSummariesModule>()), | | 11 | + loadCatalogSummaries: (locale: string) => new Promise((resolve) => pending.set(locale, resolve)), | | 12 | +})); | | 13 | + | | 14 | +afterEach(() => { | | 15 | + cleanup(); | | 16 | + pending.clear(); | | 17 | + act(() => useLabelStore.setState({ loadedLocale: "en" })); | | 18 | +}); | | 19 | + | | 20 | +describe("useCatalogSummaries", () => { | | 21 | + it("shows nothing of the previous language while the next chunk loads", async () => { | | 22 | + act(() => useLabelStore.setState({ loadedLocale: "de" })); | | 23 | + const { result } = renderHook(() => useCatalogSummaries()); | | 24 | + await act(async () => pending.get("de")?.({ "^FO": { hash: "x", summary: "Setzt die Feldposition." } })); | | 25 | + await waitFor(() => expect(result.current["^FO"]?.summary).toBe("Setzt die Feldposition.")); | | 26 | + | | 27 | + act(() => useLabelStore.setState({ loadedLocale: "fr" })); | | 28 | + expect(result.current).toEqual({}); | | 29 | + await act(async () => pending.get("fr")?.({ "^FO": { hash: "x", summary: "Place le champ." } })); | | 30 | + await waitFor(() => expect(result.current["^FO"]?.summary).toBe("Place le champ.")); | | 31 | + }); | | 32 | +}); |
|