Repository navigation
Expand file tree
/
Copy path__init__.py
More file actions
77 lines (59 loc) · 2.11 KB
/
Copy path__init__.py
File metadata and controls
77 lines (59 loc) · 2.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
from __future__ import annotations
from gettext import find
from pathlib import Path
from shutil import copyfile
from sphinx.locale import get_translation
TYPE_CHECKING = False
if TYPE_CHECKING:
from typing import Any
from sphinx.application import Sphinx
from sphinx.util.typing import ExtensionMetadata
__version__ = "2026.9.1"
THEME_PATH = Path(__file__).resolve().parent
LOCALE_DIR = THEME_PATH / "locale"
MESSAGE_CATALOG_NAME = "python-docs-theme"
def add_translation_to_context(
app: Sphinx,
pagename: str,
templatename: str,
context: dict[str, Any],
doctree: None,
) -> None:
theme_gettext = get_translation(MESSAGE_CATALOG_NAME)
sphinx_gettext = get_translation("sphinx")
def combined(message: str) -> str:
translation = theme_gettext(message)
if translation == message:
return sphinx_gettext(message)
return translation
context["_"] = context["gettext"] = context["ngettext"] = combined
def get_js_catalog(app: Sphinx) -> Path | None:
catalog = find(
MESSAGE_CATALOG_NAME,
localedir=LOCALE_DIR,
languages=[app.config.language or "en"],
)
if catalog is not None:
js_catalog = Path(catalog).with_suffix(".js")
if js_catalog.is_file():
return js_catalog
return None
def add_js_translations(app: Sphinx) -> None:
if app.builder.format != "html":
return
if catalog := get_js_catalog(app):
static_dir = Path(app.outdir) / "_static"
static_dir.mkdir(parents=True, exist_ok=True)
copyfile(catalog, static_dir / "python-docs-theme-translations.js")
app.add_js_file("python-docs-theme-translations.js")
def setup(app: Sphinx) -> ExtensionMetadata:
app.require_sphinx("7.3")
app.add_html_theme("python_docs_theme", str(THEME_PATH))
app.add_message_catalog(MESSAGE_CATALOG_NAME, LOCALE_DIR)
app.connect("html-page-context", add_translation_to_context)
app.connect("builder-inited", add_js_translations)
return {
"version": __version__,
"parallel_read_safe": True,
"parallel_write_safe": True,
}