From f9112bb7c3979c64e61874a922ed0d197a9f65e6 Mon Sep 17 00:00:00 2001 From: Nell Date: Sun, 18 Oct 2026 16:21:24 +1100 Subject: [PATCH 1/2] Make the sidebar toggle tooltips translatable The "Toggle primary sidebar" and "Toggle secondary sidebar" tooltips in components/toggle-primary-sidebar.html and components/toggle-secondary-sidebar.html were hard-coded in English, while the rest of the theme's strings go through the theme's message catalog. Wrap both strings in the theme's translate() helper and add them to the translation JSONs, with the Hungarian translations supplied in the issue. Other languages fall back to the English string, as before. Closes #998 --- .../assets/translations/jsons/Toggle primary sidebar.json | 8 ++++++++ .../translations/jsons/Toggle secondary sidebar.json | 8 ++++++++ .../components/toggle-primary-sidebar.html | 2 +- .../components/toggle-secondary-sidebar.html | 2 +- 4 files changed, 18 insertions(+), 2 deletions(-) create mode 100644 src/sphinx_book_theme/assets/translations/jsons/Toggle primary sidebar.json create mode 100644 src/sphinx_book_theme/assets/translations/jsons/Toggle secondary sidebar.json diff --git a/src/sphinx_book_theme/assets/translations/jsons/Toggle primary sidebar.json b/src/sphinx_book_theme/assets/translations/jsons/Toggle primary sidebar.json new file mode 100644 index 000000000..40def68a9 --- /dev/null +++ b/src/sphinx_book_theme/assets/translations/jsons/Toggle primary sidebar.json @@ -0,0 +1,8 @@ +[ + { "language": "English", "symbol": "en", "text": "Toggle primary sidebar" }, + { + "language": "Hungarian", + "symbol": "hu", + "text": "Elsődleges oldalsáv be- vagy kikapcsolása" + } +] diff --git a/src/sphinx_book_theme/assets/translations/jsons/Toggle secondary sidebar.json b/src/sphinx_book_theme/assets/translations/jsons/Toggle secondary sidebar.json new file mode 100644 index 000000000..95d261f00 --- /dev/null +++ b/src/sphinx_book_theme/assets/translations/jsons/Toggle secondary sidebar.json @@ -0,0 +1,8 @@ +[ + { "language": "English", "symbol": "en", "text": "Toggle secondary sidebar" }, + { + "language": "Hungarian", + "symbol": "hu", + "text": "Másodlagos oldalsáv be- vagy kikapcsolása" + } +] diff --git a/src/sphinx_book_theme/theme/sphinx_book_theme/components/toggle-primary-sidebar.html b/src/sphinx_book_theme/theme/sphinx_book_theme/components/toggle-primary-sidebar.html index 6b25347a8..849c7784d 100644 --- a/src/sphinx_book_theme/theme/sphinx_book_theme/components/toggle-primary-sidebar.html +++ b/src/sphinx_book_theme/theme/sphinx_book_theme/components/toggle-primary-sidebar.html @@ -1,3 +1,3 @@ - diff --git a/src/sphinx_book_theme/theme/sphinx_book_theme/components/toggle-secondary-sidebar.html b/src/sphinx_book_theme/theme/sphinx_book_theme/components/toggle-secondary-sidebar.html index cfbf0978a..3a60d3d1e 100644 --- a/src/sphinx_book_theme/theme/sphinx_book_theme/components/toggle-secondary-sidebar.html +++ b/src/sphinx_book_theme/theme/sphinx_book_theme/components/toggle-secondary-sidebar.html @@ -1,7 +1,7 @@ {# The in-page Table of Contents -#} {% set page_toc = generate_toc_html() -%} {% if page_toc -%} - {%- endif %} From e098ab0ef2c99d9b27e9782bdd873cd15d95b297 Mon Sep 17 00:00:00 2001 From: Nell Date: Sun, 18 Oct 2026 16:21:24 +1100 Subject: [PATCH 2/2] Add a regression test for the sidebar toggle tooltips Build the test site with language = "hu" and check that both sidebar toggle buttons carry the Hungarian tooltip. The compiled booktheme.mo catalogs come from `stb compile` and are not present in every test environment, so the test compiles a catalog for the two strings from the translation JSONs with Babel (a Sphinx dependency) and registers it for the "booktheme" domain before building. --- tests/test_build.py | 47 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/tests/test_build.py b/tests/test_build.py index 5fb1c9f0f..2a3d3fa6d 100644 --- a/tests/test_build.py +++ b/tests/test_build.py @@ -1,14 +1,20 @@ +import json import os from pathlib import Path from shutil import copytree, rmtree from importlib.metadata import version from packaging.version import parse +from babel.messages.catalog import Catalog +from babel.messages.mofile import write_mo from bs4 import BeautifulSoup import pytest import sphinx +import sphinx.locale from sphinx.testing.util import SphinxTestApp +import sphinx_book_theme + sphinx_version = parse(version("sphinx")) path_tests = Path(__file__).parent @@ -510,3 +516,44 @@ def test_marginnote(sphinx_build_factory, file_regression): encoding="utf8", basename=regression_file, ) + + +def test_sidebar_toggle_tooltips_translated(sphinx_build_factory, tmp_path): + """The tooltips of the two sidebar toggle buttons go through the catalog. + + Regression test for #998, where "Toggle primary sidebar" and + "Toggle secondary sidebar" were hard-coded in English. + + The theme's compiled ``booktheme.mo`` catalogs are produced by ``stb compile`` + and are not present in every test environment, so the Hungarian strings are + compiled from the translation JSONs here and registered for the ``booktheme`` + domain next to whatever catalog the theme itself registered. + """ + path_jsons = ( + Path(sphinx_book_theme.__file__).parent / "assets" / "translations" / "jsons" + ) + catalog = Catalog(locale="hu") + expected = {} + for msgid in ("Toggle primary sidebar", "Toggle secondary sidebar"): + entries = json.loads((path_jsons / f"{msgid}.json").read_text("utf8")) + assert entries[0]["symbol"] == "en" and entries[0]["text"] == msgid + expected[msgid] = next(e["text"] for e in entries if e["symbol"] == "hu") + catalog.add(msgid, expected[msgid]) + path_mo = tmp_path / "locale" / "hu" / "LC_MESSAGES" / "booktheme.mo" + path_mo.parent.mkdir(parents=True) + with path_mo.open("wb") as f: + write_mo(f, catalog) + + # docutils has no Hungarian localisation and warns about it; the warning is + # unrelated to the theme, so it is suppressed here. + confoverrides = {"language": "hu", "suppress_warnings": ["docutils"]} + sphinx_build = sphinx_build_factory("base", confoverrides=confoverrides) + # The theme registered its own catalog in setup(); add ours as well. + sphinx.locale.init([tmp_path / "locale"], "hu", "booktheme") + sphinx_build.build(assert_pass=True) + + page = sphinx_build.html_tree("page1.html") + primary = page.select_one("button.primary-toggle") + secondary = page.select_one("button.secondary-toggle") + assert primary["title"] == expected["Toggle primary sidebar"] + assert secondary["title"] == expected["Toggle secondary sidebar"]