Files
xenia-pos-local/local_backend/core/menu_languages.py
T
bonaminandClaude Opus 5.5 e395d99704 feat(menu): the menu in English and Greek (platform-v2 step 6.4, D-29)
- products.translations / categories.translations (Alembic 0033, core/menu_languages.py: el + en,
  name / description / quick options by their base name; catalog.translation). Empty = the text
  as typed; no machine translation (owner).
- In the manager's product editor ("In English" / "In Greek", quick extras), the category dialog,
  and a menu board's Language (signage MenuContent.language).
- Customer screens only: cloud snapshot names / descriptions per language (C-2), the menu tablet's
  EL / EN switch the names (localize), signage boards; tickets, KDS, counter, phones keep the base.
- Catalogue bundles carry translations; @xenia/access menuLanguages.js (a backend test compares).
- e2e: manager (the other language saved), menu tablet (EN / EL names), QR table (English on
  the menu, the base name on the kitchen's order, Greek page as typed).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-18 10:47:03 +02:00

103 lines
3.9 KiB
Python

"""The menu in other languages (plan step 6.4, owner decisions 2026-11-18, D-29).
The menu's languages are English and Greek (fixed for now). A product's own name and
description are its base text, in whatever language the venue typed them (usually the venue's
language). Translations add the other language:
products.translations {"en": {"name": "…", "description": "…", "quick": {"<quick option's name>": "…"}}}
categories.translations {"en": {"name": "…"}}
Quick options are kept by their base name (they are recreated on every product save, so ids
change). A missing translation shows the base text, whatever its language — never Latin letters,
never a machine translation (owner). Who sees translations: the customer-facing screens (QR menu,
order app, menu tablet, signage menu boards). Tickets, the kitchen display, the counter and the
waiters' phones keep the base text (the staff's).
"""
from __future__ import annotations
import json
MENU_LANGUAGES: tuple[str, ...] = ("el", "en")
PRODUCT_FIELDS = ("name", "description")
def _load(value):
if value in (None, "", {}):
return {}
if isinstance(value, str):
try:
value = json.loads(value)
except ValueError:
return {}
return value if isinstance(value, dict) else {}
def _bad(what):
from core.errors import XeniaError
return XeniaError("catalog.translation", what=what)
def normalize_product(value) -> dict:
"""API input → the stored shape; empty texts dropped; unknown languages or fields refused."""
data = _load(value)
out = {}
for lang, fields in data.items():
if lang not in MENU_LANGUAGES or not isinstance(fields, dict):
raise _bad(lang)
clean = {}
for k, v in fields.items():
if k in PRODUCT_FIELDS:
if v is not None and not isinstance(v, str):
raise _bad(f"{lang}.{k}")
if (v or "").strip():
clean[k] = v.strip()
elif k == "quick":
if not isinstance(v, dict):
raise _bad(f"{lang}.quick")
quick = {str(base): str(text).strip() for base, text in v.items() if str(text or "").strip()}
if quick:
clean["quick"] = quick
else:
raise _bad(f"{lang}.{k}")
if clean:
out[lang] = clean
return out
def normalize_category(value) -> dict:
data = _load(value)
out = {}
for lang, fields in data.items():
if lang not in MENU_LANGUAGES or not isinstance(fields, dict) or set(fields) - {"name"}:
raise _bad(lang)
if (fields.get("name") or "").strip():
out[lang] = {"name": fields["name"].strip()}
return out
def dump(data: dict) -> str | None:
return json.dumps(data, ensure_ascii=False) if data else None
def of(row) -> dict:
"""A product's or category's stored translations, read safely."""
return _load(getattr(row, "translations", None))
def texts(base: str | None, translations: dict, field: str) -> dict:
"""{language: text} for every menu language: the translation, else the base text."""
return {lang: (translations.get(lang) or {}).get(field) or base for lang in MENU_LANGUAGES}
def quick_texts(base: str, translations: dict) -> dict:
return {lang: ((translations.get(lang) or {}).get("quick") or {}).get(base) or base for lang in MENU_LANGUAGES}
def product_texts(product, customer: bool = True) -> dict:
"""{"names": {lang: …}, "descriptions": {lang: …}} for a customer-facing screen. `customer`:
the customer apps' own name / description (digital_*) are the base when set."""
tr = of(product)
name = (product.digital_name if customer else None) or product.name
desc = (product.digital_description if customer else None) or product.description
return {"names": texts(name, tr, "name"), "descriptions": texts(desc or None, tr, "description")}