feat(i18n): add tformat Jinja2 filter for parameterized translations

Adds _tformat(s, **kwargs) filter registered as app.jinja_env.filters["tformat"].
Uses str.format_map() with named placeholders.

Usage: {{ t.key | tformat(count=total_suppliers, name=supplier.name) }}
JSON:  "Browse {count}+ suppliers from {name}"

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
Deeman
2026-02-20 23:22:49 +01:00
parent 9957b27a77
commit 8732f5a5e0

View File

@@ -54,6 +54,15 @@ def _fmt_n(n) -> str:
return f"{round(float(n)):,}".replace(",", ".") return f"{round(float(n)):,}".replace(",", ".")
def _tformat(s: str, **kwargs) -> str:
"""Format a translation string with named placeholders.
Usage: {{ t.some_key | tformat(count=total, name=supplier.name) }}
JSON value: "Browse {count}+ suppliers from {name}"
"""
return s.format_map(kwargs)
def create_app() -> Quart: def create_app() -> Quart:
"""Create and configure the Quart application.""" """Create and configure the Quart application."""
@@ -67,12 +76,13 @@ def create_app() -> Quart:
app.secret_key = config.SECRET_KEY app.secret_key = config.SECRET_KEY
# Jinja2 filters for financial formatting (used in planner templates) # Jinja2 filters
app.jinja_env.filters["fmt_currency"] = _fmt_currency app.jinja_env.filters["fmt_currency"] = _fmt_currency
app.jinja_env.filters["fmt_k"] = _fmt_k app.jinja_env.filters["fmt_k"] = _fmt_k
app.jinja_env.filters["fmt_pct"] = _fmt_pct app.jinja_env.filters["fmt_pct"] = _fmt_pct
app.jinja_env.filters["fmt_x"] = _fmt_x app.jinja_env.filters["fmt_x"] = _fmt_x
app.jinja_env.filters["fmt_n"] = _fmt_n app.jinja_env.filters["fmt_n"] = _fmt_n
app.jinja_env.filters["tformat"] = _tformat # translate with placeholders: {{ t.key | tformat(count=n) }}
# Session config # Session config
app.config["SESSION_COOKIE_SECURE"] = not config.DEBUG app.config["SESSION_COOKIE_SECURE"] = not config.DEBUG