2024-06-07 06:28:57 +02:00
|
|
|
#!/usr/bin/python
|
|
|
|
|
|
|
|
import subprocess
|
|
|
|
from typing import List
|
|
|
|
|
|
|
|
import requests
|
|
|
|
|
|
|
|
|
|
|
|
base_url = "https://content.minetest.net"
|
2024-06-08 12:16:38 +02:00
|
|
|
translations = []
|
2024-06-07 06:28:57 +02:00
|
|
|
|
|
|
|
|
2024-06-09 14:48:00 +02:00
|
|
|
def add_translations_from_api_array(context: str, path: str, fields: List[str]):
|
2024-06-07 06:28:57 +02:00
|
|
|
print(f"Extracting translations from {path}")
|
|
|
|
url = base_url + path
|
|
|
|
req = requests.get(url)
|
|
|
|
json = req.json()
|
|
|
|
for i, row in enumerate(json):
|
|
|
|
for field in fields:
|
|
|
|
if row.get(field) is not None:
|
2024-06-09 14:48:00 +02:00
|
|
|
hint = f"{context}: {field} for {row['name']}"
|
|
|
|
translations.append((context, hint, row[field]))
|
2024-06-07 06:28:57 +02:00
|
|
|
|
|
|
|
|
2024-06-09 14:48:00 +02:00
|
|
|
add_translations_from_api_array("tags", "/api/tags/", ["title", "description"])
|
|
|
|
add_translations_from_api_array("content_warnings", "/api/content_warnings/", ["title", "description"])
|
2024-06-07 06:28:57 +02:00
|
|
|
|
|
|
|
|
|
|
|
with open("app/_translations.py", "w") as f:
|
|
|
|
f.write("# THIS FILE IS AUTOGENERATED: utils/extract_translations.py\n\n")
|
|
|
|
f.write("from flask_babel import gettext\n\n")
|
2024-06-09 14:48:00 +02:00
|
|
|
for (context, hint, translation) in translations:
|
2024-06-07 06:28:57 +02:00
|
|
|
escaped = translation.replace('\n', '\\n').replace("\"", "\\\"")
|
2024-06-09 14:48:00 +02:00
|
|
|
if hint:
|
|
|
|
f.write(f"# NOTE: {hint}\n")
|
|
|
|
f.write(f"pgettext(\"{context}\", \"{escaped}\")\n")
|
2024-06-07 06:28:57 +02:00
|
|
|
|
|
|
|
|
2024-06-09 14:48:00 +02:00
|
|
|
subprocess.run(["pybabel", "extract", "-F", "babel.cfg", "-k", "lazy_gettext", "-o", "translations/messages.pot", "-c", "NOTE", "."])
|
2024-06-07 06:28:57 +02:00
|
|
|
subprocess.run(["pybabel", "update", "-i", "translations/messages.pot", "-d", "translations", "--no-fuzzy-matching"])
|