feat(i18n): add country name translations for article country badges
Move COUNTRY_LABELS to i18n.py (shared). Add get_country_name(country_str, lang) that maps English DB values (e.g. "Germany") to localised names via existing dir_country_* translation keys. Register as Jinja filter country_name. Apply to market_results.html country badge. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -8,7 +8,7 @@
|
||||
{% endif %}
|
||||
<div class="flex items-center gap-2">
|
||||
{% if article.country %}
|
||||
<span class="badge">{{ article.country }}</span>
|
||||
<span class="badge">{{ article.country | country_name(lang) }}</span>
|
||||
{% endif %}
|
||||
{% if article.region %}
|
||||
<span class="text-xs text-slate">{{ article.region }}</span>
|
||||
|
||||
@@ -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",
|
||||
|
||||
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user