Repository navigation
Description
Refs: #438
Depends on: #439
Can parallel with: B after A merges
Related: #372, #388, #390
Context
Printing needs a language policy (receipt vs KOT vs UI), direction metadata, and bilingual-label semantics that today exist nowhere. The central language registry (frontend/src/lib/i18n/languages.ts, carrying direction and selectable) is authoritative for languages — print must never hardcode 'en' | 'fa' | 'es' | 'pt'.
The repository has no neutral shared directory today: root tsconfig.json compiles only main/**/* (rootDir: "./main"), the frontend aliases one backend file via @countries, and tests compile tests + main. Introducing a neutral shared/print/ module is therefore an explicit, verified build-boundary change — this issue owns doing it safely.
Architectural Decisions Relevant to This Issue
- Neutral location: create
shared/print/at repo root. Do NOT park it undermain/print/*to dodge tsconfig work, and do not expand the@countries-style frontend-imports-backend pattern. - Purity: the kernel contains types + pure functions only. No Electron, DOM, React, DB, filesystem, network, or transport IO.
- Registry independence: the kernel does not import the frontend registry. It defines a structural
PrintLanguageCode = stringplus pure functions that receive registry-derived data as parameters; call sites inject registry facts. This keeps the dependency direction clean while guaranteeing "no hardcoded language unions". - Typed policy, no magic arrays:
{ primary: {mode:'inherit'} | {mode:'fixed', language}, additional: Language[] }for receipts; same shape single-primary for KOT."auto"/"inherit"is a mode, never stored where a language code belongs. - Direction is per-scope (
document/block/value); helpers expose base-direction resolution and LTR-island classification (IDs, phones, URLs, SKUs, tax IDs, invoice numbers, amounts). - Bilingual labels are semantic: a
BilingualLabel { primary, secondary? }value plus pure width-fit strategies (inline vs stacked) parameterized by column count — renderers choose presentation later.
Scope / What to do
- Create
shared/print/kernel.ts(split into small files as sensible): policy types, resolution functions (resolveReceiptLanguages(policy, storeLanguage)returning ordered list, max 2 enforced at type level for v1),DirectionSpec,BilingualLabel+ fit strategy selector, LTR-island classifier. - Build-boundary work, with verification for every step:
- root
tsconfig.json: includeshared/**/*; resolverootDirimplications for emitted output layout (checkdist/layout consumed by Electron packaging andscripts/copy-runtime-assets.cjs). - frontend
tsconfig.json: path alias (e.g.,@print/*→../shared/print/*) so Next bundles kernel sources directly (a bundled copy in the renderer is expected; what matters is no duplicate runtime copies inside the Electron main process). - tests
tsconfig.json: includeshared/**/*. - Check electron-builder packaging config includes whatever the compiled output requires.
- root
- Settings model + UX (v1):
- Persist structured policies as settings keys:
bill_language_policy,kot_language_policystoring the typed JSON (add toALLOWED_WILDCARD_KEYSwith validation: primary mode inherit-or-registered-selectable-language; additional entries registered+selectable, deduped, ≤1 entry in v1 UI). - Settings → Printing section: "Receipt language" (Same as store [default] / fixed list) and "Second receipt language" (None [default] / list) and "Kitchen ticket language" (Same as store / list). Dropdown options are generated from the central registry at runtime (filter by
selectable), so future languages appear automatically. - Mirror policies in
pos-settingsZustand store for renderer-side reads.
- Persist structured policies as settings keys:
- Unit tests for policy resolution, dedupe/validation, bilingual fit strategies at 32/36/42/48 columns, direction classification.
Compatibility / Migration
- New settings keys default to inherit/none — zero behavior change for existing tenants until renderers start consuming them (refactor(printing): introduce renderer-independent PrintDocument v1 #442, refactor(printing): migrate backend thermal receipts, KOT, and preview to semantic print model #443, refactor(printing): migrate browser and WebUSB printing to semantic print model #444).
- If legacy flat keys existed (they do not — nothing ships yet), document mapping here instead.
What NOT to do
- Do NOT modify any renderer to consume the policy yet (D/E/F own adoption).
- Do NOT add translation keys or touch label generation (refactor(printing): centralize receipt and KOT labels on canonical i18n messages #440).
- Do NOT implement bilingual rendering inside real renderers, global RTL flipping, or per-station/customer language features (listed as epic future work).
- Do NOT place kernel code under
main/or import it fromfrontendvia a backend path alias. - Do NOT introduce an npm workspace/package.json project for the kernel.
Acceptance Criteria
-
shared/print/exists, pure (no IO imports — enforce via lint rule or import audit note), consumed by main, frontend, and tests. - Root
npm run buildand frontend production build both succeed with the new boundary; packaging output verified to include compiled kernel for the main process. - No duplicate runtime copies of kernel code inside the Electron main bundle (verify emitted output manually and record evidence in PR).
- Policy settings persist/validate correctly; invalid payloads rejected; defaults preserve current behavior.
- Settings dropdowns populate dynamically from the language registry (test with a temporary registry entry added in test fixture).
- Kernel unit tests cover resolution, direction classification, bilingual fit at 32/36/42/48 columns.
Verification
npm run build
npm run lint
cd frontend && npx tsc --noEmit && npm run build && cd ..
npx ts-node --transpile-only -P tests/tsconfig.json tests/<new-kernel-tests>.test.ts
npm run i18n:checkPackaging spot-check per repository release scripts (document exact command used in PR).
Documentation
Kernel README section (or docs/ placeholder) describing purity rules and the registry-injection pattern; full contributor docs remain #449's.
Activity
chore: update pull request (#453)
refactor(printing): add shared print kernel, language policy, and rec…
refactor(printing): introduce renderer-independent PrintDocument v1 (#…
feat(print): add shared print language kernel and settings (FreeOpenS…