Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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"
}
]
Original file line number Diff line number Diff line change
@@ -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"
}
]
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
<button class="sidebar-toggle primary-toggle btn btn-sm" title="Toggle primary sidebar" data-bs-placement="bottom" data-bs-toggle="tooltip">
<button class="sidebar-toggle primary-toggle btn btn-sm" title="{{ translate('Toggle primary sidebar') }}" data-bs-placement="bottom" data-bs-toggle="tooltip">
<span class="fa-solid fa-bars"></span>
</button>
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{# The in-page Table of Contents -#}
{% set page_toc = generate_toc_html() -%}
{% if page_toc -%}
<button class="sidebar-toggle secondary-toggle btn btn-sm pst-navbar-icon" title="Toggle secondary sidebar" data-bs-placement="bottom" data-bs-toggle="tooltip">
<button class="sidebar-toggle secondary-toggle btn btn-sm pst-navbar-icon" title="{{ translate('Toggle secondary sidebar') }}" data-bs-placement="bottom" data-bs-toggle="tooltip">
<span class="fa-solid fa-list"></span>
</button>
{%- endif %}
47 changes: 47 additions & 0 deletions tests/test_build.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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"]