Skip to content

Make the sidebar toggle tooltips translatable - #1013

Open
chanellebingham wants to merge 2 commits into
executablebooks:mainfrom
chanellebingham:fix-998-translate-sidebar-toggle-tooltips
Open

chanellebingham wants to merge 2 commits into
executablebooks:mainfrom
chanellebingham:fix-998-translate-sidebar-toggle-tooltips

Conversation

@chanellebingham

Copy link
Copy Markdown

Fixes #998

The "Toggle primary sidebar" and "Toggle secondary sidebar" tooltips were hard-coded in English in components/toggle-primary-sidebar.html and components/toggle-secondary-sidebar.html. In a site built with language = "hu" they stayed English while the neighbouring header buttons were translated ("Forrásfájl letöltése", "Nyomtatás PDF-be", "Teljes képernyős mód").

Changes:

  • Wrap both strings in the theme's translate() helper, which the other templates already use for the booktheme catalog.
  • Add Toggle primary sidebar.json and Toggle secondary sidebar.json under src/sphinx_book_theme/assets/translations/jsons/, with the English source string and the Hungarian translations from the issue. Other languages fall back to the English string, as they do today. The files are formatted with prettier as described in the translations README.
  • Add test_sidebar_toggle_tooltips_translated, which builds the test site with language = "hu" and checks the title attribute of both toggle buttons. 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 JSONs with Babel (already a Sphinx dependency) and registers it for the booktheme domain via sphinx.locale.init before building. docutils has no Hungarian localisation and warns about that, so the test sets suppress_warnings = ["docutils"].

Testing:

Rendered title attributes of a small site built with -D language=hu, before and after:

before: title="Toggle primary sidebar"            title="Toggle secondary sidebar"
after:  title="Elsődleges oldalsáv be- vagy kikapcsolása"   title="Másodlagos oldalsáv be- vagy kikapcsolása"

The default-language (English) build is unchanged, so the build__header-article.html regression fixture did not need updating.

The new test fails on main and passes with the change:

# on main (templates unchanged)
FAILED tests/test_build.py::test_sidebar_toggle_tooltips_translated
  AssertionError: assert 'Toggle primary sidebar' == 'Elsődleges oldalsáv be- vagy kikapcsolása'

# with this branch
1 passed

It also passes with no compiled static/locales/ directory present at all.

Full suite: pytest -> 30 passed, 1 skipped on Sphinx 9.1.0 and on Sphinx 8.2.3 (main is 29 passed, 1 skipped). ruff check, ruff format --check (ruff 0.15.20), codespell (2.4.2) and prettier --check (3.9.4) are clean on the changed files.

Noticed while testing, not changed here: _compile_translations.py writes the .po files with the platform default encoding, so on Windows it fails on the first non-Latin-1 string unless run with python -X utf8. Happy to open a separate small PR for that if useful.

Open questions:

  1. The new JSON files only contain en and hu. The translations README describes generating the other languages with the smodin.io service; would you like that done in this PR, or left for native speakers (as for the other strings)?
  2. Open PR Conditionally add toggle sidebar button #925 ("Conditionally add toggle sidebar button") edits the same line of toggle-primary-sidebar.html. Whichever lands second needs a trivial rebase; happy to do that on this side.
  3. Keep the test? It is the only test that builds in a non-English language, and the Babel/sphinx.locale.init step exists only because test environments have no compiled catalogs. It can be dropped if you would rather keep the suite simpler.

I used an AI assistant (Claude) to help investigate and write this change; I reviewed the diff and ran the tests myself before opening.

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 executablebooks#998
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.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

"Toggle primary sidebar" missing in language file

1 participant