diff --git a/web/src/padelnomics/app.py b/web/src/padelnomics/app.py index 49d9776..3906001 100644 --- a/web/src/padelnomics/app.py +++ b/web/src/padelnomics/app.py @@ -10,7 +10,7 @@ from .analytics import close_analytics_db, open_analytics_db from .core import close_db, config, get_csrf_token, init_db, is_flag_enabled, setup_logging, setup_request_id setup_logging() -from .i18n import LANG_BLUEPRINTS, SUPPORTED_LANGS, get_translations +from .i18n import LANG_BLUEPRINTS, SUPPORTED_LANGS, get_country_name, get_translations _ASSET_VERSION = str(int(time.time())) @@ -96,6 +96,7 @@ def create_app() -> Quart: app.jinja_env.filters["fmt_x"] = _fmt_x app.jinja_env.filters["fmt_n"] = _fmt_n app.jinja_env.filters["tformat"] = _tformat # translate with placeholders: {{ t.key | tformat(count=n) }} + app.jinja_env.filters["country_name"] = get_country_name # {{ article.country | country_name(lang) }} # Session config app.config["SESSION_COOKIE_SECURE"] = not config.DEBUG diff --git a/web/src/padelnomics/content/templates/partials/market_results.html b/web/src/padelnomics/content/templates/partials/market_results.html index eb65871..c515775 100644 --- a/web/src/padelnomics/content/templates/partials/market_results.html +++ b/web/src/padelnomics/content/templates/partials/market_results.html @@ -8,7 +8,7 @@ {% endif %}
{% if article.country %} - {{ article.country }} + {{ article.country | country_name(lang) }} {% endif %} {% if article.region %} {{ article.region }} diff --git a/web/src/padelnomics/directory/routes.py b/web/src/padelnomics/directory/routes.py index 00caa44..a7f46cd 100644 --- a/web/src/padelnomics/directory/routes.py +++ b/web/src/padelnomics/directory/routes.py @@ -7,7 +7,7 @@ from pathlib import Path from quart import Blueprint, g, make_response, redirect, render_template, request, url_for from ..core import csrf_protect, execute, fetch_all, fetch_one, utcnow_iso -from ..i18n import get_translations +from ..i18n import COUNTRY_LABELS, get_translations bp = Blueprint( "directory", @@ -16,41 +16,6 @@ bp = Blueprint( template_folder=str(Path(__file__).parent / "templates"), ) -COUNTRY_LABELS = { - "DE": "Germany", - "ES": "Spain", - "IT": "Italy", - "FR": "France", - "PT": "Portugal", - "GB": "United Kingdom", - "NL": "Netherlands", - "BE": "Belgium", - "SE": "Sweden", - "DK": "Denmark", - "FI": "Finland", - "NO": "Norway", - "AT": "Austria", - "SI": "Slovenia", - "IS": "Iceland", - "CH": "Switzerland", - "EE": "Estonia", - "US": "United States", - "CA": "Canada", - "MX": "Mexico", - "BR": "Brazil", - "AR": "Argentina", - "AE": "UAE", - "SA": "Saudi Arabia", - "TR": "Turkey", - "CN": "China", - "IN": "India", - "SG": "Singapore", - "ID": "Indonesia", - "TH": "Thailand", - "AU": "Australia", - "ZA": "South Africa", - "EG": "Egypt", -} CATEGORY_LABELS = { "manufacturer": "Manufacturer", diff --git a/web/src/padelnomics/i18n.py b/web/src/padelnomics/i18n.py index 59b2545..a94ff9b 100644 --- a/web/src/padelnomics/i18n.py +++ b/web/src/padelnomics/i18n.py @@ -13,6 +13,44 @@ from pathlib import Path SUPPORTED_LANGS = {"en", "de"} LANG_BLUEPRINTS = {"public", "planner", "directory", "content", "leads", "suppliers"} +# 2-letter ISO country code → English name. +# Used by the directory, article templates, and get_country_name(). +COUNTRY_LABELS: dict[str, str] = { + "DE": "Germany", + "ES": "Spain", + "IT": "Italy", + "FR": "France", + "PT": "Portugal", + "GB": "United Kingdom", + "NL": "Netherlands", + "BE": "Belgium", + "SE": "Sweden", + "DK": "Denmark", + "FI": "Finland", + "NO": "Norway", + "AT": "Austria", + "SI": "Slovenia", + "IS": "Iceland", + "CH": "Switzerland", + "EE": "Estonia", + "US": "United States", + "CA": "Canada", + "MX": "Mexico", + "BR": "Brazil", + "AR": "Argentina", + "AE": "UAE", + "SA": "Saudi Arabia", + "TR": "Turkey", + "CN": "China", + "IN": "India", + "SG": "Singapore", + "ID": "Indonesia", + "TH": "Thailand", + "AU": "Australia", + "ZA": "South Africa", + "EG": "Egypt", +} + _LOCALES_DIR = Path(__file__).parent / "locales" @@ -138,3 +176,24 @@ def get_calc_item_names(lang: str) -> dict[str, str]: """ assert lang in _CALC_ITEM_NAMES, f"Unknown lang: {lang!r}" return _CALC_ITEM_NAMES[lang] + + +# Reverse map: English country name → 2-letter code (e.g. "Germany" → "DE"). +# Built once at load time from COUNTRY_LABELS. +_COUNTRY_CODE_BY_EN_NAME: dict[str, str] = {v: k for k, v in COUNTRY_LABELS.items()} + + +def get_country_name(country_str: str, lang: str) -> str: + """Return the localised name for a country stored as an English name in the DB. + + Falls back to the original string if not found in translations. + Used as a Jinja filter: {{ article.country | country_name(lang) }} + """ + if not country_str: + return country_str + effective_lang = lang if lang in _TRANSLATIONS else "en" + code = _COUNTRY_CODE_BY_EN_NAME.get(country_str, "") + if not code: + return country_str + key = f"dir_country_{code}" + return _TRANSLATIONS[effective_lang].get(key, country_str)