Skip to content

refactor(printing): centralize receipt and KOT labels on canonical i18n messages #440

Closed

Description

@khaira777

Refs: #438
Depends on: #439 (parity harness)
Can parallel with: C (kernel) after A merges
Related: #372, #377, #382, #196

Context

Backend thermal renderers (main/printers/thermal.ts) hardcode ~45 English strings ("Subtotal", "TOTAL", "Invoice #", "KITCHEN ORDER TICKET", payment names, points labels, test-page text), and frontend ESC-POS encoders add ~30 more. Only web-print.ts resolves labels through canonical i18n. An audit identified 58 translatable concepts: 19 reusable existing keys, 9 reuse-with-rename, 30 new keys — the mapping table is embedded below and is part of this issue's contract.

FloCafe's Electron main process cannot import the frontend i18n loaders, but locale JSON files must remain the only translation source (epic principle). The chosen mechanism is a generated derived view: a build script extracts the needed label subtree from canonical messages into a committed TypeScript module under main/print/, with CI drift validation. Ordinary builds consume it; they never regenerate tracked files silently (epic principle on build determinism).

Architectural Decisions Relevant to This Issue

  • Locale JSONs are the single translation source; the generated file is explicitly a derived view, regenerated only via an explicit npm command and verified by CI.
  • Languages are never hardcoded as unions in print code: the generated dictionary is keyed by language code strings validated against the generated key set; typing tightens when the shared kernel (refactor(printing): add shared print kernel, language policy, and receipt language settings #441) lands.
  • Test-page policy (decided): human-facing test-page sentences are translated like any other print surface; technical literals stay verbatim — protocol names (ESC/POS), encodings/codepages (UTF-8, CP1256), raw byte/hex output, IP/port values, printer model identifiers, capability IDs.
  • Branding constants (Powered by FloPOS, URL) are never translated.
  • Currency symbol ASCII fallback and Persian-digit suppression remain formatting concerns (existing helpers), not translations.
  • Payment-method labels: port web-print.ts's pos.methodCash/methodCard/methodWallet mapping into backend resolution; unknown methods keep current capitalize-fallback.
  • KOT order types resolve via existing orders.dineIn/takeaway/delivery family instead of uppercasing raw enums.

Scope / What to do

  1. Add the print.* message namespace to all four committed locale files (en, fa, es, pt) for genuinely-new concepts (~30 keys): invoice/bill title variants, time, short customer/number prefixes, address/call/note prefixes, TOTAL casing, thank-you variants, points earned/balance/redeemed, KOT title/banner/station/type/no-pending/end, HSN, subtotal-excl-tax, rates-inclusive note, human-facing test-page lines. Reuse existing keys for the other concepts by listing them in the generator's borrowed-keys manifest rather than duplicating them.
  2. Generator scripts/generate-print-labels.cjs: reads canonical messages, extracts print.* + borrowed keys per language, emits committed main/print/print-labels.generated.ts exporting a typed record plus a resolver printLabel(lang, conceptId) with safe English fallback. Include a header "GENERATED FILE — edit messages, then run npm run generate:print-labels".
  3. npm wiring: generate:print-labels script; extend the i18n:check pipeline with a drift step (regenerate in temp, diff against committed file, fail on drift). npm run build consumes the committed file without rewriting it.
  4. Thread an optional language?: string parameter through prepareReceipt, formatReceipt, formatClassicReceipt, formatCompactReceipt, formatKOT, buildTestPage (default 'en'). Routes (main/routes/printers.ts) pass the tenant's existing language setting where available so backend prints become localized immediately without new settings UI (full policy arrives with refactor(printing): add shared print kernel, language policy, and receipt language settings #441).
  5. Replace every audited hardcoded occurrence in thermal.ts (classic, compact, KOT, plugin built-in fallbacks such as TAX INVOICE/TOTAL/footer defaults, test page human-facing lines) with resolver calls. Keep author-supplied plugin payload fields (e.g., totals.grandTotalLabel) authoritative over defaults.

Audit mapping (contract — implement exactly this split)

Concept Existing key to reuse
Bill#/Date/Table/Customer/CustomerNo/Ph receipt.billNumber, receipt.date, pos.tableLabel, pos.customer, receipt.customerNo, receipt.phone
Item/Qty/Rate/Amount/Amt receipt.item, receipt.qty, receipt.rate, receipt.amount, printTest.amt
Subtotal/Discount/Tax/Delivery pos.subtotal, pos.discount, pos.tax, pos.delivery
Total Tax / Service Charge / Tax Details / Payments receipt.totalTax, receipt.serviceCharge, receipt.taxDetails, receipt.payments
Thank you (long) / tax included receipt.thankYou, receipt.taxIncluded
REPRINT receipt.reprint (render adds \*\* … \*\*)

New print.* keys (complete list this issue owns): taxInvoiceTitle, invoiceTitle, invoiceNumber, time, customerShort, numberShort, address, call, note, phoneLong, subtotalExclTax, serviceChargeShort, grandTotal, thankYouShort, thankYouVisitAgain, pleaseComeAgain, ratesInclusiveNote, pointsEarned, pointsBalance, pointsRedeemed, kot.title, kot.banner, kot.station, kot.type, kot.noPendingItems, kot.end, hsn, test.title, test.networkUsb, test.columns, test.wrapHint, test.success.

Compatibility / Migration

  • Generated file is additive; no persisted settings change; API request/response shapes unchanged except optional language input.
  • English output must be semantically identical (parity harness from #A stays green); minor whitespace normalization at call sites is allowed where the audit flagged drift (e.g., Bill #123 vs Bill #: 123).

What NOT to do

Acceptance Criteria

  • All 58 audited concepts resolve via printLabel; zero user-visible hardcoded English remains in migrated backend paths except documented exemptions (branding, technical literals).
  • main/print/print-labels.generated.ts committed; regeneration produces byte-identical output (idempotency test).
  • Drift check fails CI when messages and generated view diverge; ordinary npm run build leaves git status clean.
  • Backend receipt/KOT/test-page honor tenant language setting for label selection with English fallback.
  • Payment methods localize via existing method keys with capitalize fallback; KOT order type uses translated order-type keys.
  • Parity harness (#A) green in English across widths 32/42/48; new tests assert fa/es label selection unit-level.

Verification

npm run generate:print-labels && git status --short   # clean after regen
npm run i18n:check
npx ts-node --transpile-only -P tests/tsconfig.json tests/printer.test.ts
npx ts-node --transpile-only -P tests/tsconfig.json tests/print-parity.test.ts
npx ts-node --transpile-only -P tests/tsconfig.json tests/bills-print-api.test.ts
npm run build

Documentation

Document the derived-view rule (edit messages → run generator → commit) in docs/i18n.md or a new docs/printing-labels.md section; full architecture docs belong to #449.

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