fix(i18n): country_name filter now handles 2-letter ISO codes from DB
articles.country stores "CH"/"DE"/etc., not English names. Update get_country_name() to try the input as an uppercase code first, falling back to the reverse-name lookup for any English-name values. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -184,7 +184,11 @@ _COUNTRY_CODE_BY_EN_NAME: dict[str, str] = {v: k for k, v in COUNTRY_LABELS.item
|
|||||||
|
|
||||||
|
|
||||||
def get_country_name(country_str: str, lang: str) -> str:
|
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.
|
"""Return the localised name for a country stored as a 2-letter code or English name.
|
||||||
|
|
||||||
|
Handles both formats stored in the DB:
|
||||||
|
- 2-letter ISO code: "CH" → "Schweiz" (de) / "Switzerland" (en)
|
||||||
|
- English name: "Switzerland" → "Schweiz" (de)
|
||||||
|
|
||||||
Falls back to the original string if not found in translations.
|
Falls back to the original string if not found in translations.
|
||||||
Used as a Jinja filter: {{ article.country | country_name(lang) }}
|
Used as a Jinja filter: {{ article.country | country_name(lang) }}
|
||||||
@@ -192,7 +196,9 @@ def get_country_name(country_str: str, lang: str) -> str:
|
|||||||
if not country_str:
|
if not country_str:
|
||||||
return country_str
|
return country_str
|
||||||
effective_lang = lang if lang in _TRANSLATIONS else "en"
|
effective_lang = lang if lang in _TRANSLATIONS else "en"
|
||||||
code = _COUNTRY_CODE_BY_EN_NAME.get(country_str, "")
|
# Accept both 2-letter code ("CH") and English name ("Switzerland")
|
||||||
|
upper = country_str.upper()
|
||||||
|
code = upper if upper in COUNTRY_LABELS else _COUNTRY_CODE_BY_EN_NAME.get(country_str, "")
|
||||||
if not code:
|
if not code:
|
||||||
return country_str
|
return country_str
|
||||||
key = f"dir_country_{code}"
|
key = f"dir_country_{code}"
|
||||||
|
|||||||
Reference in New Issue
Block a user