log: serve translated content when available; English fallback

Adds module-level _resolve_log_content(session, log_id, lang) helper
to app/routers/pages.py: looks up StrategicLogTranslation by (log_id,
lang) when lang != 'en'; falls back silently to the English original
when no translation row exists yet (the expected case for the first
hour after a new language activates, or when translation fails for a
specific log).

log_page / log_page_day pull cu.user.lang and thread it through
_log_page_context so the template renders the right variant.

Two tests cover both branches.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
Giorgio Gilestro 2026-05-27 17:13:57 +02:00
parent 924f37548b
commit 1ea71bc160
2 changed files with 92 additions and 4 deletions

View file

@ -332,3 +332,66 @@ def test_digest_pick_variant_uses_user_lang():
assert ed._pick_variant(table, tone="NOVICE", lang="de") == "novice en"
# Missing tone → fallback to INTERMEDIATE/en (the safe default).
assert ed._pick_variant(table, tone="UNKNOWN", lang="en") == "intermediate en"
@pytest.mark.asyncio
async def test_log_endpoint_serves_italian_when_user_is_italian(tmp_path):
"""When a user with lang='it' opens /log, the served content is the
Italian translation, not the English original."""
from app.db import utcnow
from app.models import StrategicLog, StrategicLogTranslation, User
_, factory, setup = _build_session_factory(tmp_path)
await setup()
async with factory() as session:
session.add(User(id=10, email="it@x", tier="paid", lang="it"))
slog = StrategicLog(
generated_at=utcnow(), content="# Open\n\nDown 0.4%.",
model="test-model",
tone="INTERMEDIATE", analysis="NORMAL",
)
session.add(slog)
await session.commit()
session.add(StrategicLogTranslation(
log_id=slog.id, lang="it",
content_md="# Apertura\n\nIn calo 0,4%.",
generated_at=utcnow(), llm_model="m", llm_cost_usd=0.0,
))
await session.commit()
log_id = slog.id
# Test the resolver directly.
from app.routers.pages import _resolve_log_content
async with factory() as session:
user = await session.get(User, 10)
content = await _resolve_log_content(session, log_id, user.lang)
assert "Apertura" in content
assert "Open" not in content
@pytest.mark.asyncio
async def test_log_endpoint_falls_back_to_english_when_no_translation(tmp_path):
"""User lang='it' but no IT translation exists → English fallback."""
from app.db import utcnow
from app.models import StrategicLog, User
_, factory, setup = _build_session_factory(tmp_path)
await setup()
async with factory() as session:
session.add(User(id=11, email="it2@x", tier="paid", lang="it"))
slog = StrategicLog(
generated_at=utcnow(), content="# Open\n\nDown 0.4%.",
model="test-model",
tone="INTERMEDIATE", analysis="NORMAL",
)
session.add(slog)
await session.commit()
log_id = slog.id
from app.routers.pages import _resolve_log_content
async with factory() as session:
user = await session.get(User, 11)
content = await _resolve_log_content(session, log_id, user.lang)
assert "Open" in content