Add gettext context to tags and warnings, update translations
This commit is contained in:
@@ -10,7 +10,7 @@ base_url = "https://content.minetest.net"
|
||||
translations = []
|
||||
|
||||
|
||||
def add_translations_from_api_array(path: str, fields: List[str]):
|
||||
def add_translations_from_api_array(context: str, path: str, fields: List[str]):
|
||||
print(f"Extracting translations from {path}")
|
||||
url = base_url + path
|
||||
req = requests.get(url)
|
||||
@@ -18,20 +18,23 @@ def add_translations_from_api_array(path: str, fields: List[str]):
|
||||
for i, row in enumerate(json):
|
||||
for field in fields:
|
||||
if row.get(field) is not None:
|
||||
translations.append(row[field])
|
||||
hint = f"{context}: {field} for {row['name']}"
|
||||
translations.append((context, hint, row[field]))
|
||||
|
||||
|
||||
add_translations_from_api_array("/api/tags/", ["title", "description"])
|
||||
add_translations_from_api_array("/api/content_warnings/", ["title", "description"])
|
||||
add_translations_from_api_array("tags", "/api/tags/", ["title", "description"])
|
||||
add_translations_from_api_array("content_warnings", "/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:
|
||||
for (context, hint, translation) in translations:
|
||||
escaped = translation.replace('\n', '\\n').replace("\"", "\\\"")
|
||||
f.write(f"gettext(\"{escaped}\")\n")
|
||||
if hint:
|
||||
f.write(f"# NOTE: {hint}\n")
|
||||
f.write(f"pgettext(\"{context}\", \"{escaped}\")\n")
|
||||
|
||||
|
||||
subprocess.run(["pybabel", "extract", "-F", "babel.cfg", "-k", "lazy_gettext", "-o", "translations/messages.pot", "."])
|
||||
subprocess.run(["pybabel", "extract", "-F", "babel.cfg", "-k", "lazy_gettext", "-o", "translations/messages.pot", "-c", "NOTE", "."])
|
||||
subprocess.run(["pybabel", "update", "-i", "translations/messages.pot", "-d", "translations", "--no-fuzzy-matching"])
|
||||
|
||||
Reference in New Issue
Block a user