"""The menu in other languages (plan step 6.4, owner decisions 2026-11-18, D-29; every text per language, owner 2026-11-21). Since 2026-11-21 (Alembic 0042) every text of the menu is kept **per language**, the venue's own language included: a product's name and description, a category's name, and every choice's name (quick options, ingredients, extras and their sub-choices, preference sets, their choices, sub-choices and shared sub-set, choice groups). The editor's "Default" is the venue's language (`venue.language`). The row's own `name` / `description` is the text shown when a language has none: the venue language's, else the first language that has one. So when the venue changes its language, the new language starts empty ("Default" is empty) but the item keeps showing its only text everywhere; languages are never mixed. products.translations {"el": {"name": "…", "description": "…"}, "en": {…}} categories.translations {"el": {"name": "…"}, …} a choice's translations {"el": "…", "en": "…"} (the rows' column; sub-choices inside their JSON) Before 0042 the base text had no language and quick options were translated on the product (`"quick": {"": "…"}`); 0042 moved those onto the quick options. 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, own: dict | None = None) -> dict: """A quick option's texts: its own translations (since 0042), else the product's old `quick` map, else the base text.""" own = own or {} return {lang: own.get(lang) or ((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")} def normalize_names(value) -> dict: """A choice's translations {language: text}: API input → stored; empty texts dropped.""" data = _load(value) out = {} for lang, text in data.items(): if text is not None and not isinstance(text, str): raise _bad(str(lang)) if not (text or "").strip(): continue # empty: nothing to keep, whatever the language if lang not in MENU_LANGUAGES: raise _bad(str(lang)) out[lang] = text.strip() return out def names_of(row) -> dict: """A choice row's translations {language: text}, read safely.""" return {k: v for k, v in _load(getattr(row, "translations", None)).items() if isinstance(v, str)} def name_texts(base: str, names: dict) -> dict: """{language: text} of a choice for every menu language: its own, else the base text.""" return {lang: names.get(lang) or base for lang in MENU_LANGUAGES}