Skip to content

Commit e0ae1fe

Browse files
language selector draft :)
1 parent 1b8cf82 commit e0ae1fe

File tree

5 files changed

+77
-12
lines changed

5 files changed

+77
-12
lines changed

_static/language_select.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
document.addEventListener("DOMContentLoaded", () => {
2+
let selectors = document.querySelectorAll("#language-selector");
3+
selectors.forEach((selector) => {
4+
selector.addEventListener("change", (event) => {
5+
let target = event.target.value;
6+
if (target.startsWith("https")) {
7+
window.location.href = target;
8+
} else {
9+
window.location.pathname = target;
10+
}
11+
});
12+
});
13+
});

_static/pyos.css

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,10 @@ body {
4646
margin-right: auto !important;
4747
}
4848

49+
.navbar-persistent--mobile {
50+
margin-left: unset !important;
51+
}
52+
4953
/* custom fonts */
5054

5155
html,
@@ -375,3 +379,7 @@ th {
375379
border: 1px solid #ccc; /* Light gray border */
376380
padding: 8px; /* Add some padding for better readability */
377381
}
382+
383+
/* ----------------- */
384+
/* Language Selector */
385+
/* ----------------- */

_templates/language-selector.html

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
{%- macro langlink(lang, selected=False) -%} {%- if lang == "en" %}
2+
<option
3+
value="{{ baseurl }}{{ pagename }}{{ file_suffix }}"
4+
{%
5+
if
6+
selected
7+
%}selected{%
8+
endif
9+
%}
10+
>
11+
{{ lang }}
12+
</option>
13+
{%- else %}
14+
<option
15+
value="{{ baseurl }}{{ lang }}/{{ pagename }}{{ file_suffix }}"
16+
{%
17+
if
18+
selected
19+
%}selected{%
20+
endif
21+
%}
22+
>
23+
{{ lang }}
24+
</option>
25+
{%- endif -%} {%- endmacro -%}
26+
<select class="dropdown" id="language-selector" aria-label="Choose a language">
27+
{{ langlink(language, selected=True) }} {%- for a_language in languages -%}
28+
{%- if a_language != language -%} {{ langlink(a_language) }} {%- endif -%} {%-
29+
endfor %}
30+
</select>

conf.py

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
# sys.path.insert(0, os.path.abspath('.'))
1616
from datetime import datetime
1717
import subprocess
18+
import os
1819

1920
current_year = datetime.now().year
2021
organization_name = "pyOpenSci"
@@ -26,10 +27,10 @@
2627
copyright = f"{current_year}, {organization_name}"
2728
author = "pyOpenSci Community"
2829

29-
language = "en"
30-
languages = ["es", "jp"]
31-
# languages excluding english
32-
# (english is built without a subdirectory to not break already-existing inbound links)
30+
# language can later be overridden (eg with the -D flag)
31+
# but we need it set here so it can make it into the html_context
32+
language = os.environ.get("SPHINX_LANG", "en")
33+
languages = ["en", "es", "jp"]
3334

3435
# Get the latest Git tag - there might be a prettier way to do this but...
3536
try:
@@ -76,7 +77,11 @@
7677
{"href": "https://www.pyopensci.org/images/favicon.ico"},
7778
]
7879

79-
# Link to our repo for easy PR/ editing
80+
html_baseurl = "https://www.pyopensci.org/python-package-guide/"
81+
if os.environ.get("SPHINX_DEV", False):
82+
# for links in language selector when developing locally
83+
html_baseurl = "/"
84+
8085
html_theme_options = {
8186
"announcement": "<p><a href='https://www.pyopensci.org/about-peer-review/index.html'>We run peer review of scientific Python software. Learn more.</a></p>",
8287
# "navbar_center": ["nav"], this can be a way to override the default navigation structure
@@ -116,14 +121,16 @@
116121
"github_url": "https://github.com/pyopensci/python-package-guide",
117122
"footer_start": ["code_of_conduct", "copyright"],
118123
"footer_end": [],
124+
"navbar_persistent": ["language-selector", "search-button"]
119125
}
120126

121127
html_context = {
122128
"github_user": "pyopensci",
123129
"github_repo": "python-package-guide",
124130
"github_version": "main",
125-
'language': language,
126-
'languages': languages
131+
"language": language,
132+
"languages": languages,
133+
"base_url": html_baseurl,
127134
}
128135

129136
# Add any paths that contain templates here, relative to this directory.
@@ -147,7 +154,7 @@
147154
]
148155

149156
# For sitemap generation
150-
html_baseurl = "https://www.pyopensci.org/python-package-guide/"
157+
151158
sitemap_url_scheme = "{link}"
152159

153160
# -- Options for HTML output -------------------------------------------------
@@ -159,7 +166,7 @@
159166
html_static_path = ["_static"]
160167
html_css_files = ["pyos.css"]
161168
html_title = "Python Packaging Guide"
162-
html_js_files = ["matomo.js"]
169+
html_js_files = ["matomo.js", "language_select.js"]
163170

164171

165172
# Social cards

noxfile.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@
5252
LANGUAGES = conf.languages
5353

5454
# List of languages that should be built when releasing the guide (docs or docs-test sessions)
55-
RELEASE_LANGUAGES = []
55+
RELEASE_LANGUAGES = [lang for lang in conf.languages if lang != "en"]
5656

5757

5858
@nox.session
@@ -78,7 +78,7 @@ def docs_test(session):
7878
session.notify("build-translations", ['release-build', *TEST_PARAMETERS])
7979

8080
def _autobuild_cmd(posargs: list[str], output_dir = OUTPUT_DIR) -> list[str]:
81-
cmd = [SPHINX_AUTO_BUILD, *BUILD_PARAMETERS, str(SOURCE_DIR), str(output_dir), *posargs]
81+
cmd = ["SPHINX_DEV=true", SPHINX_AUTO_BUILD, *BUILD_PARAMETERS, str(SOURCE_DIR), str(output_dir), *posargs]
8282
for folder in AUTOBUILD_IGNORE:
8383
cmd.extend(["--ignore", f"*/{folder}/*"])
8484
return cmd
@@ -158,8 +158,11 @@ def docs_live_langs(session):
158158

159159
cmds = ['"' + " ".join(_autobuild_cmd(session.posargs) + ['--open-browser']) + '"']
160160
for language in LANGUAGES:
161+
if language == "en":
162+
continue
161163
cmds.append(
162164
'"' + " ".join(
165+
[f"SPHINX_LANG={language}"] +
163166
_autobuild_cmd(
164167
session.posargs + ["-D", f"language={language}"],
165168
output_dir=OUTPUT_DIR / language
@@ -217,7 +220,11 @@ def build_languages(session):
217220
session.warn(f"Language [{lang}] is not available for translation")
218221
continue
219222
session.log(f"Building [{lang}] guide")
220-
session.run(SPHINX_BUILD, *BUILD_PARAMETERS, "-D", f"language={lang}", ".", OUTPUT_DIR / lang, *session.posargs)
223+
if lang == 'en':
224+
out_dir = OUTPUT_DIR
225+
else:
226+
out_dir = OUTPUT_DIR / lang
227+
session.run(f"SPHINX_LANG={lang}", SPHINX_BUILD, *BUILD_PARAMETERS, "-D", f"language={lang}", ".", out_dir, *session.posargs)
221228

222229

223230
@nox.session(name="build-translations")

0 commit comments

Comments
 (0)