Skip to content

Commit 2922201

Browse files
make button text translatable
1 parent bbaf94a commit 2922201

8 files changed

Lines changed: 84 additions & 19 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ cover/
5757
# Translations
5858
*.mo
5959
*.pot
60+
!tests/test_snippets/*.pot
6061

6162
# Django stuff:
6263
*.log

docs/snippets/myst/button-link.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@
55
Button text
66
```
77

8+
```{button-link} https://example.com
9+
:color: primary
10+
11+
Button text with options
12+
```
13+
814
```{button-link} https://example.com
915
:color: primary
1016
:shadow:

sphinx_design/badges_buttons.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -178,8 +178,13 @@ def run_with_defaults(self) -> list[nodes.Node]:
178178
textnodes, _ = self.state.inline_text(
179179
"\n".join(self.content), self.lineno + self.content_offset
180180
)
181-
content = nodes.inline("", "")
181+
content = nodes.inline("", "", translatable=True)
182182
content.extend(textnodes)
183+
184+
# make link text translatable -
185+
# target gettext to the content lines, not the outer directive
186+
self.set_source_info(content)
187+
content.line += self.content_offset
183188
else:
184189
content = nodes.inline(target, target)
185190
node.append(content)
@@ -191,7 +196,9 @@ def run_with_defaults(self) -> list[nodes.Node]:
191196
node = grid_container
192197

193198
# `visit_reference` requires that a reference be inside a `TextElement` parent
194-
container = nodes.paragraph(classes=self.options.get("align", []))
199+
container = nodes.paragraph(
200+
classes=self.options.get("align", []), translatable=False
201+
)
195202
self.set_source_info(container)
196203
container += node
197204

tests/conftest.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,12 @@ def get_doctree(
6868
return doctree
6969

7070

71-
@pytest.fixture()
72-
def sphinx_builder(tmp_path: Path, make_app, monkeypatch):
71+
@pytest.fixture(params=[pytest.param("html", id="html")])
72+
def sphinx_builder(
73+
tmp_path: Path, make_app, monkeypatch, request: pytest.FixtureRequest
74+
):
7375
def _create_project(
74-
buildername: str = "html", conf_kwargs: dict[str, Any] | None = None
76+
buildername: str = request.param, conf_kwargs: dict[str, Any] | None = None
7577
):
7678
src_path = tmp_path / "srcdir"
7779
src_path.mkdir()

tests/test_snippets.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,12 @@
1717
SNIPPETS_PATH = Path(__file__).parent.parent / "docs" / "snippets"
1818
SNIPPETS_GLOB_RST = list((SNIPPETS_PATH / "rst").glob("[!_]*"))
1919
SNIPPETS_GLOB_MYST = list((SNIPPETS_PATH / "myst").glob("[!_]*"))
20+
EXPECTED_PATH = Path(__file__).parent / "test_snippets"
21+
I18N_GLOB_MYST = [
22+
p
23+
for p in SNIPPETS_GLOB_MYST
24+
if (EXPECTED_PATH / f"snippet_i18n_{p.stem}.pot").exists()
25+
]
2026

2127

2228
def write_assets(src_path: Path):
@@ -205,3 +211,35 @@ def test_sd_custom_directives(
205211
extension=".xml",
206212
encoding="utf8",
207213
)
214+
215+
216+
@pytest.mark.parametrize("sphinx_builder", ["gettext"], indirect=True)
217+
@pytest.mark.parametrize(
218+
"path",
219+
I18N_GLOB_MYST,
220+
ids=[path.stem for path in I18N_GLOB_MYST],
221+
)
222+
@pytest.mark.skipif(not MYST_INSTALLED, reason="myst-parser not installed")
223+
def test_i18n_myst(
224+
sphinx_builder: Callable[..., SphinxBuilder],
225+
path: Path,
226+
normalize_doctree_xml,
227+
file_regression,
228+
):
229+
builder = sphinx_builder()
230+
content = path.read_text(encoding="utf8")
231+
builder.src_path.joinpath("index.md").write_text(content, encoding="utf8")
232+
write_assets(builder.src_path)
233+
builder.build()
234+
235+
# strip metadata (that includes a constantly-varying timestamp)
236+
out_path = builder.out_path / "index.pot"
237+
out = out_path.read_text()
238+
out = out[out.find("#: ../../index") :]
239+
240+
file_regression.check(
241+
out,
242+
basename=f"snippet_i18n_{path.stem}",
243+
extension=".pot",
244+
encoding="utf8",
245+
)
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#: ../../index.md:4
2+
msgid "Button text"
3+
msgstr ""
4+
5+
#: ../../index.md:10
6+
msgid "Button text with options"
7+
msgstr ""

tests/test_snippets/snippet_post_button-link.xml

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,25 @@
22
<section ids="heading" names="heading">
33
<title>
44
Heading
5-
<paragraph>
5+
<paragraph translatable="0">
66
<reference classes="sd-sphinx-override sd-btn sd-text-wrap" refuri="https://example.com">
77
<inline>
88
https://example.com
9-
<paragraph>
9+
<paragraph translatable="0">
1010
<reference classes="sd-sphinx-override sd-btn sd-text-wrap" refuri="https://example.com">
11-
<inline>
12-
Button text
13-
<paragraph>
11+
Button text
12+
<paragraph translatable="0">
13+
<reference classes="sd-sphinx-override sd-btn sd-text-wrap sd-btn-primary" refuri="https://example.com">
14+
Button text with options
15+
<paragraph translatable="0">
1416
<reference classes="sd-sphinx-override sd-btn sd-text-wrap sd-btn-primary sd-shadow-sm" refuri="https://example.com">
1517
<inline>
1618
https://example.com
17-
<paragraph>
19+
<paragraph translatable="0">
1820
<reference classes="sd-sphinx-override sd-btn sd-text-wrap sd-btn-outline-primary" refuri="https://example.com">
1921
<inline>
2022
https://example.com
21-
<paragraph>
23+
<paragraph translatable="0">
2224
<inline classes="sd-d-grid">
2325
<reference classes="sd-sphinx-override sd-btn sd-text-wrap sd-btn-secondary" refuri="https://example.com">
2426
<inline>

tests/test_snippets/snippet_pre_button-link.xml

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,25 @@
22
<section ids="heading" names="heading">
33
<title>
44
Heading
5-
<paragraph>
5+
<paragraph translatable="0">
66
<reference classes="sd-sphinx-override sd-btn sd-text-wrap" refuri="https://example.com">
77
<inline>
88
https://example.com
9-
<paragraph>
9+
<paragraph translatable="0">
1010
<reference classes="sd-sphinx-override sd-btn sd-text-wrap" refuri="https://example.com">
11-
<inline>
12-
Button text
13-
<paragraph>
11+
Button text
12+
<paragraph translatable="0">
13+
<reference classes="sd-sphinx-override sd-btn sd-text-wrap sd-btn-primary" refuri="https://example.com">
14+
Button text with options
15+
<paragraph translatable="0">
1416
<reference classes="sd-sphinx-override sd-btn sd-text-wrap sd-btn-primary sd-shadow-sm" refuri="https://example.com">
1517
<inline>
1618
https://example.com
17-
<paragraph>
19+
<paragraph translatable="0">
1820
<reference classes="sd-sphinx-override sd-btn sd-text-wrap sd-btn-outline-primary" refuri="https://example.com">
1921
<inline>
2022
https://example.com
21-
<paragraph>
23+
<paragraph translatable="0">
2224
<inline classes="sd-d-grid">
2325
<reference classes="sd-sphinx-override sd-btn sd-text-wrap sd-btn-secondary" refuri="https://example.com">
2426
<inline>

0 commit comments

Comments
 (0)