Skip to content

Commit 1474317

Browse files
committed
fix(theme): tolerate hyphenated locale keys for Sphinx 9
Sphinx 9 normalizes the `language` config to hyphenated BCP-47 form (e.g. `de-DE`) before passing it into the Jinja template context, but `load_languages()` keyed the `languages` dict on the underscored form derived from Crowdin's locale strings. `versions.html` rendering then raised `KeyError('de-DE')` on every per-locale build. `get_language_code` and `get_language_display_name` now accept either form and fall back to the underscored variant, keeping the theme compatible with Sphinx 7, 8, and 9 without touching `.po`/`.mo` naming. Bumps version to 1.2.4. Signed-off-by: Gabriel Harris-Rouquette <gabizou@me.com>
1 parent 07aa1b4 commit 1474317

3 files changed

Lines changed: 26 additions & 4 deletions

File tree

CHANGELOG.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,15 @@
11
# Changelog
22

3+
## 1.2.4
4+
5+
- Fix `KeyError` during per-locale builds on Sphinx 9. Sphinx 9 normalizes the
6+
`language` config to the hyphenated BCP-47 form (e.g. `de-DE`) before passing
7+
it to the Jinja template context, but the theme's `languages` dict is keyed
8+
in the underscored form that Crowdin's locale strings are rewritten to at
9+
load time. `get_language_code` / `get_language_display_name` now accept
10+
either form and fall back to the underscored variant, so the `versions.html`
11+
template renders cleanly under Sphinx 7, 8, and 9.
12+
313
## 1.2.3
414

515
- Register `translations.js` via `builder.add_js_file()` instead of appending to

src/theme/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
VERSION = (1, 2, 3)
1+
VERSION = (1, 2, 4)
22
__version__ = '.'.join(str(v) for v in VERSION)
33

44

src/theme/languages.py

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,24 @@
1212
languages = None
1313

1414

15+
def _lookup_language(locale_code):
16+
# Sphinx <=8 passes the language config through untouched (e.g. ``de_DE``);
17+
# Sphinx 9 normalizes it to the hyphenated BCP-47 form (``de-DE``) before it
18+
# reaches template context. Our ``languages`` dict is keyed in underscored
19+
# form, so accept either and fall back to the underscored variant.
20+
if locale_code in languages:
21+
return locale_code, languages[locale_code]
22+
normalized = locale_code.replace('-', '_')
23+
return normalized, languages[normalized]
24+
25+
1526
def get_language_code(locale_code):
16-
return languages[locale_code]['code']
27+
_, lang = _lookup_language(locale_code)
28+
return lang['code']
1729

1830

1931
def get_language_display_name(locale_code):
20-
lang = languages[locale_code]
32+
resolved_code, lang = _lookup_language(locale_code)
2133

2234
# Compute display name if not already cached
2335
if 'display_name' not in lang:
@@ -33,7 +45,7 @@ def get_language_display_name(locale_code):
3345
locale.territory = None
3446

3547
try:
36-
lang['display_name'] = locale.languages[locale_code].title()
48+
lang['display_name'] = locale.languages[resolved_code].title()
3749
except KeyError:
3850
lang['display_name'] = locale.language_name.title()
3951
except babel.UnknownLocaleError:

0 commit comments

Comments
 (0)