Skip to content

Commit 4bc70ab

Browse files
hugovkm-aciek
andauthored
Indicate languages in the docs.python.org switcher (python-docs-translations#24)
* Indicate languages in the docs.python.org switcher * Update get_languages to return generator of language codes in switcher Co-authored-by: Maciej Olko <[email protected]> * Update following refactor * Include build column with either 'in switcher' (linked to docs) or ✗ * Specifically refer to data-label instead of counting relative child --------- Co-authored-by: Maciej Olko <[email protected]>
1 parent ea33d13 commit 4bc70ab

File tree

3 files changed

+62
-5
lines changed

3 files changed

+62
-5
lines changed

generate.py

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# /// script
2+
# requires-python = ">=3.11"
23
# dependencies = [
34
# "gitpython",
45
# "potodo",
@@ -16,6 +17,7 @@
1617
from jinja2 import Template
1718

1819
import repositories
20+
import switcher
1921
import visitors
2022
from completion import branches_from_devguide, get_completion
2123

@@ -30,13 +32,23 @@
3032
)
3133
subprocess.run(['make', '-C', Path(clones_dir, 'cpython/Doc'), 'venv'], check=True)
3234
subprocess.run(['make', '-C', Path(clones_dir, 'cpython/Doc'), 'gettext'], check=True)
35+
switcher_languages = list(switcher.get_languages())
3336
for language, repo in repositories.get_languages_and_repos(devguide_dir):
3437
if repo:
3538
completion_number, translators_number = get_completion(clones_dir, repo)
3639
visitors_number = visitors.get_number_of_visitors(language)
3740
else:
3841
completion_number, visitors_number = 0.0, 0
39-
completion_progress.append((language, repo, completion_number, translators_number, visitors_number))
42+
completion_progress.append(
43+
(
44+
language,
45+
repo,
46+
completion_number,
47+
translators_number,
48+
visitors_number,
49+
language in switcher_languages,
50+
)
51+
)
4052
print(completion_progress[-1])
4153

4254
template = Template(
@@ -52,19 +64,27 @@
5264
<thead>
5365
<tr>
5466
<th>language</th>
67+
<th>build</th>
5568
<th><a href="https://plausible.io/data-policy#how-we-count-unique-users-without-cookies">visitors<a/></th>
5669
<th>translators</th>
5770
<th>completion</th>
5871
</tr>
5972
</thead>
6073
<tbody>
61-
{% for language, repo, completion, translators, visitors in completion_progress | sort(attribute=2) | reverse %}
74+
{% for language, repo, completion, translators, visitors, in_switcher in completion_progress | sort(attribute=2) | reverse %}
6275
<tr>
6376
{% if repo %}
6477
<td data-label="language">
6578
<a href="https://github.com/{{ repo }}" target="_blank">
6679
{{ language }}
6780
</a>
81+
</td>
82+
<td data-label="build">
83+
{% if in_switcher %}
84+
<a href="https://docs.python.org/{{ language }}/">in switcher</a>
85+
{% else %}
86+
87+
{% endif %}
6888
</td>
6989
<td data-label="visitors">
7090
<a href="https://plausible.io/docs.python.org?filters=((contains,page,(/{{ language }}/)))" target="_blank">

style.css

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,10 @@ th {
2525
min-width: 50px;
2626
box-sizing: border-box;
2727
}
28-
td:nth-child(2) {
28+
td[data-label="visitors"] {
2929
text-align: right;
3030
}
31-
td:last-child {
31+
td[data-label="completion"] {
3232
width: 100%;
3333
}
3434
@media screen and (max-width: 600px) {
@@ -56,7 +56,7 @@ td:last-child {
5656
left: 10px;
5757
position: absolute;
5858
}
59-
td:last-child {
59+
td[data-label="completion"] {
6060
width: inherit;
6161
}
6262
.progress-bar {

switcher.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
"""
2+
Fetch languages in the https://docs.python.org language switcher.
3+
4+
Return a defaultdict mapping language codes to a Boolean indicating
5+
whether it is in the language switcher.
6+
"""
7+
8+
import tomllib
9+
from collections import defaultdict
10+
from typing import Generator
11+
12+
import requests
13+
14+
15+
def get_languages() -> Generator[str, None, None]:
16+
data = requests.get(
17+
"https://raw.githubusercontent.com/"
18+
"python/docsbuild-scripts/refs/heads/main/config.toml",
19+
timeout=10,
20+
).text
21+
config = tomllib.loads(data)
22+
languages = config["languages"]
23+
defaults = config["defaults"]
24+
for code, language in languages.items():
25+
if language.get("in_prod", defaults["in_prod"]):
26+
yield code.lower().replace("_", "-")
27+
28+
29+
def main() -> None:
30+
languages = list(get_languages())
31+
print(languages)
32+
for code in ("en", "pl", "ar", "zh-cn"):
33+
print(f"{code}: {code in languages}")
34+
35+
36+
if __name__ == "__main__":
37+
main()

0 commit comments

Comments
 (0)