-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathtranslated_names.py
More file actions
33 lines (26 loc) · 1 KB
/
translated_names.py
File metadata and controls
33 lines (26 loc) · 1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
"""
Fetch translated names of languages.
Yield a tuple of language code and a string with the translated name.
"""
import tomllib
from collections.abc import Iterator
from babel import Locale
from babel.core import UnknownLocaleError
from urllib3 import PoolManager
def _babel_autonym(code: str) -> str | None:
"""Get the translated name for a language code with Babel"""
try:
locale = Locale.parse(code.replace('-', '_'))
return locale.get_display_name(locale)
except (UnknownLocaleError, ValueError):
return None
def get_languages(http: PoolManager) -> Iterator[tuple[str, str]]:
data = http.request(
'GET',
'https://raw.githubusercontent.com/python/docsbuild-scripts/refs/heads/main/config.toml',
).data
config = tomllib.loads(data.decode())
for code, language in config['languages'].items():
language_code = code.lower().replace('_', '-')
translated_name = language.get('translated_name')
yield language_code, translated_name