Skip to content

Commit a690993

Browse files
Review
1 parent 62f3a27 commit a690993

File tree

4 files changed

+39
-33
lines changed

4 files changed

+39
-33
lines changed

.github/workflows/tests.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ jobs:
7171
pip install -r requirements.txt
7272
- name: Remove locale file for testing
7373
shell: bash
74-
run: rm -rf python_docs_theme/locales/pt_BR/
74+
run: rm -rf python_docs_theme/locale/pt_BR/
7575
- run: python babel_runner.py extract
7676
- run: python babel_runner.py init -l pt_BR
7777
- run: python babel_runner.py update
@@ -80,7 +80,7 @@ jobs:
8080
- run: python babel_runner.py compile -l pt_BR
8181
- name: Print .pot file
8282
shell: bash
83-
run: cat python_docs_theme/locales/messages.pot
83+
run: cat python_docs_theme/locale/messages.pot
8484
- name: Print .po file
8585
shell: bash
86-
run: cat python_docs_theme/locales/pt_BR/LC_MESSAGES/messages.po
86+
run: cat python_docs_theme/locale/pt_BR/LC_MESSAGES/messages.po

babel_runner.py

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
#!/usr/bin/env python3
21
"""Script for handling translations with Babel"""
2+
33
from __future__ import annotations
44

55
import argparse
@@ -8,18 +8,18 @@
88
import tomllib
99
from pathlib import Path
1010

11-
PROJECT_DIR = Path(__file__).resolve().parent
12-
PYPROJECT_TOML = PROJECT_DIR / "pyproject.toml"
13-
INIT_PY = PROJECT_DIR / "python_docs_theme" / "__init__.py"
14-
1511
# Global variables used by pybabel below (paths relative to PROJECT_DIR)
16-
DOMAIN = "messages"
12+
DOMAIN = "python-docs-theme"
1713
COPYRIGHT_HOLDER = "Python Software Foundation"
1814
SOURCE_DIR = "python_docs_theme"
19-
LOCALES_DIR = f"{SOURCE_DIR}/locales"
20-
POT_FILE = Path(LOCALES_DIR, f"{DOMAIN}.pot")
2115
MAPPING_FILE = ".babel.cfg"
2216

17+
PROJECT_DIR = Path(__file__).resolve().parent
18+
PYPROJECT_TOML = PROJECT_DIR / "pyproject.toml"
19+
INIT_PY = PROJECT_DIR / SOURCE_DIR / "__init__.py"
20+
LOCALES_DIR = f"{SOURCE_DIR}/locale"
21+
POT_FILE = Path(LOCALES_DIR, f"{DOMAIN}.pot")
22+
2323

2424
def get_project_info() -> dict:
2525
"""Retrieve project's info to populate the message catalog template"""
@@ -75,21 +75,32 @@ def init_locale(locale: str) -> None:
7575
if pofile.exists():
7676
print(f"There is already a message catalog for locale {locale}, skipping.")
7777
return
78-
cmd = ["pybabel", "init", "-i", POT_FILE, "-d", LOCALES_DIR, "-l", locale]
78+
cmd = [
79+
"pybabel",
80+
"init",
81+
"-i",
82+
POT_FILE,
83+
"-d",
84+
LOCALES_DIR,
85+
"-D",
86+
DOMAIN,
87+
"-l",
88+
locale,
89+
]
7990
subprocess.run(cmd, cwd=PROJECT_DIR, check=True)
8091

8192

8293
def update_catalogs(locale: str) -> None:
8394
"""Update translations from existing message catalogs"""
84-
cmd = ["pybabel", "update", "-i", POT_FILE, "-d", LOCALES_DIR]
95+
cmd = ["pybabel", "update", "-i", POT_FILE, "-d", LOCALES_DIR, "-D", DOMAIN]
8596
if locale:
8697
cmd.extend(["-l", locale])
8798
subprocess.run(cmd, cwd=PROJECT_DIR, check=True)
8899

89100

90101
def compile_catalogs(locale: str) -> None:
91102
"""Compile existing message catalogs"""
92-
cmd = ["pybabel", "compile", "-d", LOCALES_DIR]
103+
cmd = ["pybabel", "compile", "-d", LOCALES_DIR, "-D", DOMAIN]
93104
if locale:
94105
cmd.extend(["-l", locale])
95106
subprocess.run(cmd, cwd=PROJECT_DIR, check=True)

python_docs_theme/__init__.py

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,31 @@
11
from __future__ import annotations
22

3-
import gettext
4-
import os
53
from pathlib import Path
64

5+
from sphinx.locale import get_translation
6+
77
TYPE_CHECKING = False
88
if TYPE_CHECKING:
9-
from sphinx.application import Sphinx
10-
from sphinx.util.typing import ExtensionMetadata
9+
from sphinx.application import Sphinx # noqa: F401
10+
from sphinx.util.typing import ExtensionMetadata # noqa: F401
1111

1212
__version__ = "2025.5"
1313

1414
THEME_PATH = Path(__file__).resolve().parent
15+
LOCALE_DIR = THEME_PATH / "locale"
16+
MESSAGE_CATALOG_NAME = "python-docs-theme"
1517

1618

17-
def setup_translations(app):
18-
translation = gettext.translation(
19-
domain="messages",
20-
localedir=os.fspath(THEME_PATH / "locales"),
21-
languages=[app.config.language],
22-
fallback=True,
23-
)
24-
app.builder.templates.environment.install_gettext_translations(
25-
translation, newstyle=True
26-
)
27-
28-
29-
def setup(app: Sphinx) -> ExtensionMetadata:
19+
def setup(app):
3020
app.require_sphinx("7.3")
31-
32-
app.connect("builder-inited", setup_translations)
3321
app.add_html_theme("python_docs_theme", str(THEME_PATH))
22+
app.add_message_catalog(MESSAGE_CATALOG_NAME, LOCALE_DIR)
23+
24+
def add_translation_to_context(app, pagename, templatename, context, doctree):
25+
_ = get_translation(MESSAGE_CATALOG_NAME)
26+
context["_"] = context["gettext"] = context["ngettext"] = _
27+
28+
app.connect("html-page-context", add_translation_to_context)
3429

3530
return {
3631
"version": __version__,
2.18 KB
Binary file not shown.

0 commit comments

Comments
 (0)