Skip to content

refactor(printing): add shared print kernel, language policy, and receipt language settings #441

Closed

Description

@khaira777

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 under main/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 = string plus 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

  1. 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.
  2. Build-boundary work, with verification for every step:
    • root tsconfig.json: include shared/**/*; resolve rootDir implications for emitted output layout (check dist/ layout consumed by Electron packaging and scripts/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: include shared/**/*.
    • Check electron-builder packaging config includes whatever the compiled output requires.
  3. Settings model + UX (v1):
    • Persist structured policies as settings keys: bill_language_policy, kot_language_policy storing the typed JSON (add to ALLOWED_WILDCARD_KEYS with 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-settings Zustand store for renderer-side reads.
  4. Unit tests for policy resolution, dedupe/validation, bilingual fit strategies at 32/36/42/48 columns, direction classification.

Compatibility / Migration

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 from frontend via 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 build and 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:check

Packaging 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.

Metadata

Metadata

Assignees

Labels

area:backendarea:frontend

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions