new-locales.py: fix weblate pagination

This commit is contained in:
FC (Fay) Stegerman
2024-05-24 20:57:18 +02:00
parent 39f50bad30
commit 14c12533a7
2 changed files with 23 additions and 11 deletions

View File

@@ -24,7 +24,7 @@ sed = [
]
subprocess.run(sed, check=True)
with open("app/src/main/res/xml/locales_config.xml", "w") as fh:
with open("app/src/main/res/xml/locales_config.xml", "w", encoding="utf-8") as fh:
fh.write('<?xml version="1.0" encoding="utf-8"?>\n')
fh.write('<locale-config xmlns:android="http://schemas.android.com/apk/res/android">\n')
fh.write(' <locale android:name="en-US" />\n')

View File

@@ -19,15 +19,27 @@ REPLACE_CODES = {
STATS_URL = "https://hosted.weblate.org/api/components/catima/catima/statistics/"
class Error(Exception):
pass
def get_weblate_langs() -> List[Tuple[str, int]]:
r = requests.get(STATS_URL, timeout=5)
r.raise_for_status()
url = STATS_URL
results = []
for lang in r.json()["results"]:
if lang["code"] != "en":
code = REPLACE_CODES.get(lang["code"], lang["code"]).replace("_", "-r")
results.append((code, round(lang["translated_percent"])))
return sorted(results)
for _ in range(16): # avoid endless loops just in case
r = requests.get(url, timeout=5)
r.raise_for_status()
data = r.json()
for lang in data["results"]:
if lang["code"] != "en":
code = REPLACE_CODES.get(lang["code"], lang["code"]).replace("_", "-r")
results.append((code, round(lang["translated_percent"])))
url = data["next"]
if not url:
return sorted(results)
if not url.split("?")[0] == STATS_URL:
raise Error(f"Unexpected next URL: {url}")
raise Error("Too many pages")
def get_dir_langs() -> List[str]:
@@ -42,7 +54,7 @@ def get_dir_langs() -> List[str]:
def get_xml_langs() -> List[Tuple[str, bool]]:
results = []
in_section = False
with open("app/src/main/res/values/settings.xml") as fh:
with open("app/src/main/res/values/settings.xml", encoding="utf-8") as fh:
for line in fh:
if not in_section and 'name="locale_values"' in line:
in_section = True
@@ -59,7 +71,7 @@ def get_xml_langs() -> List[Tuple[str, bool]]:
def update_xml_langs(langs: List[Tuple[str, bool]]) -> None:
lines: List[str] = []
in_section = False
with open("app/src/main/res/values/settings.xml") as fh:
with open("app/src/main/res/values/settings.xml", encoding="utf-8") as fh:
for line in fh:
if not in_section and 'name="locale_values"' in line:
in_section = True
@@ -70,7 +82,7 @@ def update_xml_langs(langs: List[Tuple[str, bool]]) -> None:
else:
continue
lines.append(line)
with open("app/src/main/res/values/settings.xml", "w") as fh:
with open("app/src/main/res/values/settings.xml", "w", encoding="utf-8") as fh:
for line in lines:
fh.write(line)