"""Every menu text per language (owner 2026-11-21, core/menu_languages.py). - Every choice gets its own `translations` {language: text}: choice groups, quick options, extras, ingredients, preference sets and their choices (sub-choices and a set's shared sub-set keep theirs inside their JSON). - The texts typed so far had no language: they become the text **in their language**: Greek letters → "el", else the venue's language (`venue.language`, default "en"). A language that already had a translation keeps it. - The product's old `"quick": {"": "…"}` translations move onto the quick options. Revision ID: 0042_menu_texts_per_language Revises: 0041_floor_item_color Create Date: 2026-11-21 12:00:00 """ import json import re from alembic import op import sqlalchemy as sa revision = '0042_menu_texts_per_language' down_revision = '0041_floor_item_color' branch_labels = None depends_on = None CHOICE_TABLES = ('product_modifier_groups', 'product_quick_options', 'product_options', 'product_ingredients', 'product_preference_sets', 'product_preference_choices') GREEK = re.compile('[Ͱ-Ͽἀ-῿]') LANGS = ('el', 'en') def _load(v): if not v: return {} try: d = json.loads(v) except ValueError: return {} return d if isinstance(d, (dict, list)) else {} def _dump(d): return json.dumps(d, ensure_ascii=False) if d else None def upgrade() -> None: for table in CHOICE_TABLES: with op.batch_alter_table(table, schema=None) as batch_op: batch_op.add_column(sa.Column('translations', sa.Text(), nullable=True)) bind = op.get_bind() row = bind.execute(sa.text("SELECT value FROM pos_settings WHERE key = 'venue.language'")).first() venue = (row[0] if row else None) or 'en' venue = venue if venue in LANGS else 'en' def lang_of(text): return 'el' if GREEK.search(text or '') else venue def names(text, current=None): out = dict(current or {}) if (text or '').strip(): out.setdefault(lang_of(text), text.strip()) return out def subs(raw): """Sub-choices (a JSON list of dicts) with their names per language.""" data = _load(raw) if not isinstance(data, list) or not data: return raw for s in data: if isinstance(s, dict): s['translations'] = names(s.get('name'), s.get('translations') if isinstance(s.get('translations'), dict) else None) return json.dumps(data, ensure_ascii=False) # products: name / description in their language; the quick options' translations move out quick_moved = {} for pid, name, desc, tr in bind.execute(sa.text("SELECT id, name, description, translations FROM products")).fetchall(): data = _load(tr) data = data if isinstance(data, dict) else {} for lang, fields in list(data.items()): if not isinstance(fields, dict): data.pop(lang) continue for base, text in (fields.pop('quick', None) or {}).items(): quick_moved.setdefault(pid, {}).setdefault(base, {})[lang] = text for field, text in (('name', name), ('description', desc)): if (text or '').strip(): data.setdefault(lang_of(text), {}).setdefault(field, text.strip()) data = {k: v for k, v in data.items() if v} bind.execute(sa.text("UPDATE products SET translations = :t WHERE id = :id"), {'t': _dump(data), 'id': pid}) for cid, name, tr in bind.execute(sa.text("SELECT id, name, translations FROM categories")).fetchall(): data = _load(tr) data = data if isinstance(data, dict) else {} if (name or '').strip(): data.setdefault(lang_of(name), {}).setdefault('name', name.strip()) bind.execute(sa.text("UPDATE categories SET translations = :t WHERE id = :id"), {'t': _dump(data), 'id': cid}) for qid, pid, name in bind.execute(sa.text("SELECT id, product_id, name FROM product_quick_options")).fetchall(): bind.execute(sa.text("UPDATE product_quick_options SET translations = :t WHERE id = :id"), {'t': _dump(names(name, quick_moved.get(pid, {}).get(name))), 'id': qid}) for table in ('product_modifier_groups', 'product_ingredients'): for rid, name in bind.execute(sa.text(f"SELECT id, name FROM {table}")).fetchall(): bind.execute(sa.text(f"UPDATE {table} SET translations = :t WHERE id = :id"), {'t': _dump(names(name)), 'id': rid}) for table in ('product_options', 'product_preference_choices'): for rid, name, sub in bind.execute(sa.text(f"SELECT id, name, sub_choices FROM {table}")).fetchall(): bind.execute(sa.text(f"UPDATE {table} SET translations = :t, sub_choices = :s WHERE id = :id"), {'t': _dump(names(name)), 's': subs(sub), 'id': rid}) for rid, name, shared in bind.execute(sa.text("SELECT id, name, shared_subset FROM product_preference_sets")).fetchall(): data = _load(shared) if isinstance(data, dict) and data: data['translations'] = names(data.get('name')) data['choices'] = json.loads(subs(json.dumps(data.get('choices') or [])) or '[]') shared = json.dumps(data, ensure_ascii=False) bind.execute(sa.text("UPDATE product_preference_sets SET translations = :t, shared_subset = :s WHERE id = :id"), {'t': _dump(names(name)), 's': shared, 'id': rid}) def downgrade() -> None: for table in CHOICE_TABLES: with op.batch_alter_table(table, schema=None) as batch_op: batch_op.drop_column('translations')