contentdb/utils/extract_translations.py

38 lines
1.1 KiB
Python
Raw Normal View History

#!/usr/bin/python
import subprocess
from typing import List
import requests
base_url = "https://content.minetest.net"
translations = set()
def add_translations_from_api_array(path: str, fields: List[str]):
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:
translations.add(row[field])
add_translations_from_api_array("/api/tags/", ["title", "description"])
add_translations_from_api_array("/api/content_warnings/", ["title", "description"])
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")
for translation in translations:
escaped = translation.replace('\n', '\\n').replace("\"", "\\\"")
f.write(f"gettext(\"{escaped}\")\n")
subprocess.run(["pybabel", "extract", "-F", "babel.cfg", "-k", "lazy_gettext", "-o", "translations/messages.pot", "."])
subprocess.run(["pybabel", "update", "-i", "translations/messages.pot", "-d", "translations", "--no-fuzzy-matching"])