"""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": {"": "…"}}} 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")}