| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
from pathlib import Path |
| 4 |
|
| 5 |
from sphinx.locale import get_translation |
| 6 |
|
| 7 |
TYPE_CHECKING = False |
| 8 |
if TYPE_CHECKING: |
| 9 |
from typing import Any |
| 10 |
|
| 11 |
from sphinx.application import Sphinx |
| 12 |
from sphinx.util.typing import ExtensionMetadata |
| 13 |
|
| 14 |
__version__ = "2026.7" |
| 15 |
|
| 16 |
THEME_PATH = Path(__file__).resolve().parent |
| 17 |
LOCALE_DIR = THEME_PATH / "locale" |
| 18 |
MESSAGE_CATALOG_NAME = "python-docs-theme" |
| 19 |
|
| 20 |
|
| 21 |
def add_translation_to_context( |
| 22 |
app: Sphinx, |
| 23 |
pagename: str, |
| 24 |
templatename: str, |
| 25 |
context: dict[str, Any], |
| 26 |
doctree: None, |
| 27 |
) -> None: |
| 28 |
theme_gettext = get_translation(MESSAGE_CATALOG_NAME) |
| 29 |
sphinx_gettext = get_translation("sphinx") |
| 30 |
|
| 31 |
def combined(message: str) -> str: |
| 32 |
translation = theme_gettext(message) |
| 33 |
if translation == message: |
| 34 |
return sphinx_gettext(message) |
| 35 |
return translation |
| 36 |
|
| 37 |
context["_"] = context["gettext"] = context["ngettext"] = combined |
| 38 |
|
| 39 |
|
| 40 |
def setup(app: Sphinx) -> ExtensionMetadata: |
| 41 |
app.require_sphinx("7.3") |
| 42 |
|
| 43 |
app.add_html_theme("python_docs_theme", str(THEME_PATH)) |
| 44 |
app.add_message_catalog(MESSAGE_CATALOG_NAME, LOCALE_DIR) |
| 45 |
app.connect("html-page-context", add_translation_to_context) |
| 46 |
|
| 47 |
return { |
| 48 |
"version": __version__, |
| 49 |
"parallel_read_safe": True, |
| 50 |
"parallel_write_safe": True, |
| 51 |
} |