Skip to content

Commit 737b24d

Browse files
Improve language details page
1 parent 1616734 commit 737b24d

File tree

5 files changed

+87
-25
lines changed

5 files changed

+87
-25
lines changed

build_status.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
from urllib3 import PoolManager
1212

1313

14-
def get_languages(http: PoolManager) -> Iterator[tuple[str, bool]]:
14+
def get_languages(http: PoolManager) -> Iterator[tuple[str, str, bool]]:
1515
data = http.request(
1616
'GET',
1717
'https://raw.githubusercontent.com/python/docsbuild-scripts/refs/heads/main/config.toml',
@@ -20,12 +20,14 @@ def get_languages(http: PoolManager) -> Iterator[tuple[str, bool]]:
2020
for code, language in config['languages'].items():
2121
language_code = code.lower().replace('_', '-')
2222
in_switcher = language.get('in_prod', config['defaults']['in_prod'])
23-
yield language_code, in_switcher
23+
translated_name = language.get('translated_name')
24+
yield language_code, translated_name, in_switcher
2425

2526

2627
def main() -> None:
2728
languages = {
28-
language: in_switcher for language, in_switcher in get_languages(PoolManager())
29+
language: in_switcher
30+
for language, translated_name, in_switcher in get_languages(PoolManager())
2931
}
3032
print(languages)
3133
for code in ('en', 'pl', 'ar', 'zh-cn', 'id'):

completion.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,15 @@ def get_completion(
3636
)
3737
except git.GitCommandError:
3838
print(f'failed to clone {repo} {branch}')
39-
translators_data = TranslatorsData(0, False)
39+
translators_data = TranslatorsData(0, set(), False)
4040
branch = ''
4141
continue
4242
else:
43-
translators_number = translators.get_number(clone_path)
43+
translators_list, translators_number = translators.get_number(clone_path)
4444
translators_link = translators.get_link(clone_path, repo, branch)
45-
translators_data = TranslatorsData(translators_number, translators_link)
45+
translators_data = TranslatorsData(
46+
translators_number, translators_list, translators_link
47+
)
4648
break
4749
path_for_merge = Path(clones_dir, 'rebased_translations', repo)
4850
completion = potodo.merge_and_scan_path(
@@ -83,4 +85,5 @@ def get_completion(
8385
@dataclass(frozen=True)
8486
class TranslatorsData:
8587
number: int
88+
list: set
8689
link: str | Literal[False]

generate.py

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from collections.abc import Iterator
77
from dataclasses import dataclass, asdict
88
from datetime import datetime, timezone
9+
from typing import Any, Dict
910
from pathlib import Path
1011

1112
from git import Repo
@@ -37,7 +38,13 @@ def get_completion_progress() -> Iterator['LanguageProjectData']:
3738
)
3839
subprocess.run(['make', '-C', cpython_dir / 'Doc', 'venv'], check=True)
3940
subprocess.run(['make', '-C', cpython_dir / 'Doc', 'gettext'], check=True)
40-
languages_built = dict(build_status.get_languages(PoolManager()))
41+
42+
languages_built: Dict[str, Dict[str, Any]] = {
43+
code: {'in_switcher': in_switcher, 'translated_name': translated_name}
44+
for code, translated_name, in_switcher in build_status.get_languages(
45+
PoolManager()
46+
)
47+
}
4148

4249
with concurrent.futures.ThreadPoolExecutor() as executor:
4350
return executor.map(
@@ -51,17 +58,22 @@ def get_completion_progress() -> Iterator['LanguageProjectData']:
5158
def get_project_data(
5259
language: Language,
5360
repo: str | None,
54-
languages_built: dict[str, bool],
61+
languages_built: Dict[str, Dict[str, Any]],
5562
clones_dir: str,
5663
) -> 'LanguageProjectData':
5764
built = language.code in languages_built
5865
if repo:
5966
completion, translators_data, branch, change = get_completion(clones_dir, repo)
6067
else:
6168
completion = 0.0
62-
translators_data = TranslatorsData(0, False)
69+
translators_data = TranslatorsData(0, set(), False)
6370
change = 0.0
6471
branch = ''
72+
73+
language_data = languages_built.get(language.code, {})
74+
translated_name = language_data.get('translated_name', '')
75+
in_switcher = language_data.get('in_switcher', False)
76+
6577
return LanguageProjectData(
6678
language,
6779
repo,
@@ -70,7 +82,8 @@ def get_project_data(
7082
change,
7183
translators_data,
7284
built,
73-
in_switcher=languages_built.get(language.code),
85+
translated_name=translated_name,
86+
in_switcher=in_switcher,
7487
uses_platform=language.code in contribute.pulling_from_transifex,
7588
contribution_link=contribute.get_contrib_link(language.code, repo),
7689
)
@@ -85,6 +98,7 @@ class LanguageProjectData:
8598
change: float
8699
translators: TranslatorsData
87100
built: bool
101+
translated_name: str
88102
in_switcher: bool | None
89103
uses_platform: bool
90104
contribution_link: str | None

templates/language.html.jinja

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,44 @@
11
{% extends "base.html.jinja" %}
22

33
{% block main %}
4+
<div style="margin-left: 3rem; margin-right: 3rem;">
45
{% if project.built %}
5-
<h1><a href="https://docs.python.org/{{ project.language.code }}/3">{{ project.language.name }}</a></h1>
6+
<h2 style="text-align: center;"><a href="https://docs.python.org/{{ project.language.code }}/3">{{ project.language.name }}</a></h2>
7+
<h1 style="text-align: center;"><a href="https://docs.python.org/{{ project.language.code }}/3">{{ project.translated_name }}</a></h1>
68
{% else %}
7-
<h1>{{ project.language.name }}</h1>
9+
<h1 style="text-align: center;">{{ project.language.name }}</h1>
810
{% endif %}
11+
12+
{% if project.built and project.in_switcher %}
13+
<h6 style="text-align: center;">This project is built and is in the switcher.</h6>
14+
{% elif project.built %}
15+
<h6 style="text-align: center;">This project is built but is not yet in the switcher.</h6>
16+
{% else %}
17+
<h6 style="text-align: center;">This project is not built yet.</h6>
18+
{% endif %}
19+
20+
<h4 style="text-align: center;">
21+
{% if project.translators.link %}
22+
<a href="{{ project.translators.link }}" style="color: #1595fe;">Contribute!</a>
23+
{% elif project.uses_platform %}
24+
<a href="https://app.transifex.com/join/?o=python-doc&p=python-newest&t=opensource" style="color: #1595fe;">Contribute!</a>
25+
{% else %}
26+
<a href="https://devguide.python.org/documentation/translations/translating/" style="color: #1595fe;">Contribute!</a>
27+
{% endif %}
28+
29+
<a href="https://github.com/{{ project.repository }}" style="color: #1595fe;">Repository</a>
30+
</h4>
31+
32+
<div class="row">
33+
<div class="card-container w-100">
34+
<div class="card">
35+
<div class="card-body">
36+
<h3 class="card-title">Translators ({{ project.translators.number }})</h3>
37+
<h4 class="card-subtitle mb-2 text-muted">{{ project.translators.list | join(', ') }}</h4>
38+
</div>
39+
</div>
40+
</div>
41+
</div>
42+
43+
</div>
944
{% endblock %}

translators.py

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,29 @@
11
from collections.abc import Iterator
22
from pathlib import Path
3-
from re import fullmatch
3+
from re import fullmatch, sub
44
from tempfile import TemporaryDirectory
55
from typing import Literal
66

77
from git import Repo
88
from polib import pofile
99

1010

11-
def get_number(path: Path) -> int:
12-
from_headers = len(set(yield_from_headers(path)))
13-
from_git_history = get_number_from_git_history(path)
14-
from_translators_file = len(get_from_translators_file(path))
15-
return max(from_headers, from_git_history, from_translators_file)
11+
def _format_translator(translator: str) -> str:
12+
translator = translator.strip()
13+
return sub(r'<.*?>', '', translator).strip()
1614

1715

18-
def get_number_from_git_history(path: Path) -> int:
19-
return len(Repo(path).git.shortlog('-s', 'HEAD').splitlines())
16+
def get_number(path: Path) -> tuple[set, int]:
17+
from_po_headers = set(yield_from_headers(path))
18+
from_git_history = set(get_from_git_history(path))
19+
from_translators_file = set(get_from_translators_file(path))
20+
largest_set = max(from_po_headers, from_git_history, from_translators_file)
21+
return largest_set, len(largest_set)
22+
23+
24+
def get_from_git_history(path: Path) -> list[str]:
25+
raw_lines = Repo(path).git.shortlog('-s', 'HEAD').splitlines()
26+
return [_format_translator(line.split('\t', 1)[-1]) for line in raw_lines]
2027

2128

2229
def yield_from_headers(path: Path) -> Iterator[str]:
@@ -31,16 +38,17 @@ def yield_from_headers(path: Path) -> Iterator[str]:
3138
try:
3239
translator, _year = translator_record.split(', ')
3340
except ValueError:
34-
yield translator_record
35-
else:
36-
yield translator
41+
translator = translator_record
42+
if translator.strip() == 'Transifex Bot <>':
43+
continue
44+
yield _format_translator(translator)
3745

3846

3947
def get_from_translators_file(path: Path) -> list[str]:
4048
if not (file := path.joinpath('TRANSLATORS')).exists():
4149
return []
4250
return list(
43-
line
51+
_format_translator(line)
4452
for line in file.read_text().splitlines()
4553
if line != 'Translators'
4654
and not fullmatch(r'-*', line)
@@ -72,7 +80,7 @@ def get_link(clone_path: Path, repo: str, branch: str) -> str | Literal[False]:
7280
branch=branch,
7381
)
7482
from_headers = len(set(yield_from_headers(path := Path(directory))))
75-
from_git_history = get_number_from_git_history(path)
83+
from_git_history = get_from_git_history(path)
7684
from_translators_file = len(get_from_translators_file(path))
7785
print(
7886
f'{lang}: {from_headers=}, {from_git_history=}, {from_translators_file=}'

0 commit comments

Comments
 (0)