From 313a83d59b26409ca78d858a625def791134c612 Mon Sep 17 00:00:00 2001 From: Stan Ulbrych Date: Tue, 9 Sep 2025 15:27:55 +0100 Subject: [PATCH 1/3] Commit --- babel_runner.py | 2 +- python_docs_theme/__init__.py | 18 +++++++++++++----- 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/babel_runner.py b/babel_runner.py index 677f6da..0a254b4 100755 --- a/babel_runner.py +++ b/babel_runner.py @@ -100,7 +100,7 @@ def update_catalogs(locale: str) -> None: def compile_catalogs(locale: str) -> None: """Compile existing message catalogs""" - cmd = ["pybabel", "compile", "-d", LOCALES_DIR, "-D", DOMAIN] + cmd = ["pybabel", "compile", "-d", LOCALES_DIR, "-D", DOMAIN, "--use-fuzzy"] if locale: cmd.extend(["-l", locale]) subprocess.run(cmd, cwd=PROJECT_DIR, check=True) diff --git a/python_docs_theme/__init__.py b/python_docs_theme/__init__.py index d9281ff..b63baf3 100644 --- a/python_docs_theme/__init__.py +++ b/python_docs_theme/__init__.py @@ -16,16 +16,24 @@ MESSAGE_CATALOG_NAME = "python-docs-theme" +def add_translation_to_context(app, pagename, templatename, context, doctree): + theme_gettext = get_translation(MESSAGE_CATALOG_NAME) + sphinx_gettext = get_translation("sphinx") + + def combined(msg): + trans = theme_gettext(msg) + if trans == msg: + return sphinx_gettext(msg) + return trans + + context["_"] = context["gettext"] = context["ngettext"] = combined + + 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) - - def add_translation_to_context(app, pagename, templatename, context, doctree): - _ = get_translation(MESSAGE_CATALOG_NAME) - context["_"] = context["gettext"] = context["ngettext"] = _ - app.connect("html-page-context", add_translation_to_context) return { From e670cdf327db3eb2ec6342685e11fd3d8fdfaa1b Mon Sep 17 00:00:00 2001 From: Stan Ulbrych Date: Tue, 9 Sep 2025 18:09:25 +0100 Subject: [PATCH 2/3] Switch approach --- babel_runner.py | 2 +- python_docs_theme/locale/es/LC_MESSAGES/python-docs-theme.po | 1 - python_docs_theme/locale/ja/LC_MESSAGES/python-docs-theme.po | 1 - python_docs_theme/locale/pl/LC_MESSAGES/python-docs-theme.po | 1 - python_docs_theme/locale/pt_BR/LC_MESSAGES/python-docs-theme.po | 1 - python_docs_theme/locale/tr/LC_MESSAGES/python-docs-theme.po | 1 - python_docs_theme/locale/zh_CN/LC_MESSAGES/python-docs-theme.po | 1 - python_docs_theme/locale/zh_TW/LC_MESSAGES/python-docs-theme.po | 1 - 8 files changed, 1 insertion(+), 8 deletions(-) diff --git a/babel_runner.py b/babel_runner.py index 0a254b4..677f6da 100755 --- a/babel_runner.py +++ b/babel_runner.py @@ -100,7 +100,7 @@ def update_catalogs(locale: str) -> None: def compile_catalogs(locale: str) -> None: """Compile existing message catalogs""" - cmd = ["pybabel", "compile", "-d", LOCALES_DIR, "-D", DOMAIN, "--use-fuzzy"] + cmd = ["pybabel", "compile", "-d", LOCALES_DIR, "-D", DOMAIN] if locale: cmd.extend(["-l", locale]) subprocess.run(cmd, cwd=PROJECT_DIR, check=True) diff --git a/python_docs_theme/locale/es/LC_MESSAGES/python-docs-theme.po b/python_docs_theme/locale/es/LC_MESSAGES/python-docs-theme.po index 34fc30e..5ac9ab3 100644 --- a/python_docs_theme/locale/es/LC_MESSAGES/python-docs-theme.po +++ b/python_docs_theme/locale/es/LC_MESSAGES/python-docs-theme.po @@ -7,7 +7,6 @@ # Translators: # Cristián Maureira-Fredes, 2025 # -#, fuzzy msgid "" msgstr "" "Project-Id-Version: python-docs-theme 2025.5\n" diff --git a/python_docs_theme/locale/ja/LC_MESSAGES/python-docs-theme.po b/python_docs_theme/locale/ja/LC_MESSAGES/python-docs-theme.po index dfea857..f0683c2 100644 --- a/python_docs_theme/locale/ja/LC_MESSAGES/python-docs-theme.po +++ b/python_docs_theme/locale/ja/LC_MESSAGES/python-docs-theme.po @@ -7,7 +7,6 @@ # Translators: # Inada Naoki , 2025 # -#, fuzzy msgid "" msgstr "" "Project-Id-Version: python-docs-theme 2025.5\n" diff --git a/python_docs_theme/locale/pl/LC_MESSAGES/python-docs-theme.po b/python_docs_theme/locale/pl/LC_MESSAGES/python-docs-theme.po index 461c4c9..5249a26 100644 --- a/python_docs_theme/locale/pl/LC_MESSAGES/python-docs-theme.po +++ b/python_docs_theme/locale/pl/LC_MESSAGES/python-docs-theme.po @@ -7,7 +7,6 @@ # Translators: # Stan Ulbrych, 2025 # -#, fuzzy msgid "" msgstr "" "Project-Id-Version: python-docs-theme 2025.5\n" diff --git a/python_docs_theme/locale/pt_BR/LC_MESSAGES/python-docs-theme.po b/python_docs_theme/locale/pt_BR/LC_MESSAGES/python-docs-theme.po index 2ce0936..1006ae9 100644 --- a/python_docs_theme/locale/pt_BR/LC_MESSAGES/python-docs-theme.po +++ b/python_docs_theme/locale/pt_BR/LC_MESSAGES/python-docs-theme.po @@ -8,7 +8,6 @@ # Alexsey Batista da Silva, 2025 # Rafael Fontenelle , 2025 # -#, fuzzy msgid "" msgstr "" "Project-Id-Version: python-docs-theme 2025.5\n" diff --git a/python_docs_theme/locale/tr/LC_MESSAGES/python-docs-theme.po b/python_docs_theme/locale/tr/LC_MESSAGES/python-docs-theme.po index 90afb86..f79742f 100644 --- a/python_docs_theme/locale/tr/LC_MESSAGES/python-docs-theme.po +++ b/python_docs_theme/locale/tr/LC_MESSAGES/python-docs-theme.po @@ -7,7 +7,6 @@ # Translators: # Ege Akman, 2025 # -#, fuzzy msgid "" msgstr "" "Project-Id-Version: python-docs-theme 2025.5\n" diff --git a/python_docs_theme/locale/zh_CN/LC_MESSAGES/python-docs-theme.po b/python_docs_theme/locale/zh_CN/LC_MESSAGES/python-docs-theme.po index 8fb52a4..73cc293 100644 --- a/python_docs_theme/locale/zh_CN/LC_MESSAGES/python-docs-theme.po +++ b/python_docs_theme/locale/zh_CN/LC_MESSAGES/python-docs-theme.po @@ -7,7 +7,6 @@ # Translators: # 汇民 王 , 2025 # -#, fuzzy msgid "" msgstr "" "Project-Id-Version: python-docs-theme 2025.5\n" diff --git a/python_docs_theme/locale/zh_TW/LC_MESSAGES/python-docs-theme.po b/python_docs_theme/locale/zh_TW/LC_MESSAGES/python-docs-theme.po index 719074c..c65766b 100644 --- a/python_docs_theme/locale/zh_TW/LC_MESSAGES/python-docs-theme.po +++ b/python_docs_theme/locale/zh_TW/LC_MESSAGES/python-docs-theme.po @@ -7,7 +7,6 @@ # Translators: # W. H. Wang , 2025 # -#, fuzzy msgid "" msgstr "" "Project-Id-Version: python-docs-theme 2025.5\n" From c527a8150a288812ca66756e8ae02b8853cadef5 Mon Sep 17 00:00:00 2001 From: Stan Ulbrych Date: Wed, 10 Sep 2025 16:58:33 +0100 Subject: [PATCH 3/3] Add type hints --- python_docs_theme/__init__.py | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/python_docs_theme/__init__.py b/python_docs_theme/__init__.py index b63baf3..875206b 100644 --- a/python_docs_theme/__init__.py +++ b/python_docs_theme/__init__.py @@ -6,6 +6,8 @@ TYPE_CHECKING = False if TYPE_CHECKING: + from typing import Any + from sphinx.application import Sphinx from sphinx.util.typing import ExtensionMetadata @@ -16,15 +18,21 @@ MESSAGE_CATALOG_NAME = "python-docs-theme" -def add_translation_to_context(app, pagename, templatename, context, doctree): +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(msg): - trans = theme_gettext(msg) - if trans == msg: - return sphinx_gettext(msg) - return trans + def combined(message: str) -> str: + translation = theme_gettext(message) + if translation == message: + return sphinx_gettext(message) + return translation context["_"] = context["gettext"] = context["ngettext"] = combined