1
0
Fork 0
mirror of https://github.com/YunoHost/apps.git synced 2024-09-03 20:06:07 +02:00
apps/tools/translate_apps/apps_translations_to_apps.py

113 lines
5.1 KiB
Python

import time
import json
from pathlib import Path
import tomlkit
from base import Repository, login, token
def extract_strings_to_translate_from_apps(apps, translations_repository):
for app, infos in apps.items():
repository_uri = infos["git"]["url"].replace("https://github.com/", "")
branch = infos["git"]["branch"]
if "github.com" not in infos["git"]["url"]:
continue
if app not in (
"gotosocial",
"fluffychat",
"cinny",
"fittrackee",
"funkwhale",
"photoprism",
):
continue
print(app)
print(f"{repository_uri} -> branch '{branch}'")
translations_path = Path(f"translations/apps/{app}/manifest/")
if not translations_repository.file_exists(translations_path):
print(f"App {app} doesn't have translations on github.com/yunohost/apps_translations, skip")
continue
translations_path = translations_repository.path / translations_path
with Repository(
f"https://{login}:{token}@github.com/{repository_uri}", branch
) as repository:
if not repository.file_exists("manifest.toml"):
continue
manifest = tomlkit.loads(repository.read_file("manifest.toml"))
for translation in translations_path.glob("*.json"):
language = translation.name[:-len(".json")]
# english version is the base, never modify it
if language == "en":
continue
translation = json.load(open(translation))
if translation.get("description", "").strip():
manifest["description"][language] = translation["description"]
for question in manifest.get("install", {}):
for strings_to_translate in ["ask", "help"]:
translation_key = f"install_{question}_{strings_to_translate}"
if not translation.get(translation_key, "").strip():
continue
if strings_to_translate not in manifest["install"][question]:
continue
manifest["install"][question][strings_to_translate][language] = translation[translation_key]
repository.write_file("manifest.toml", tomlkit.dumps(manifest))
if not repository.run_command("git status -s", capture_output=True).strip():
continue
# create or update merge request
repository.run_command("git diff")
repository.run_command("git add manifest.toml")
repository.run_command(["git", "commit", "-m", "feat(i18n): update translations for manifest.toml"])
repository.run_command(["git", "push", "-f", "origin", "master:manifest_toml_i18n"])
# if no PR exist, create one
if not repository.run_command("hub pr list -h manifest_toml_i18n", capture_output=True):
repository.run_command(["hub", "pull-request", "-m", "Update translations for manifest.toml", "-b", branch, "-h", "manifest_toml_i18n", "-p", "-m", f"This pull request is automatically generated by scripts from the [YunoHost/apps](https://github.com/YunoHost/apps) repository.\n\nThe translation is pull from weblate and is located here: https://translate.yunohost.org/projects/yunohost-apps/{app}/\n\nIf you wish to modify the translation (other than in english), please do that directly on weblate since this is now the source of authority for it.\n\nDon't hesitate to reach the YunoHost team on [matrix](https://matrix.to/#/#yunohost:matrix.org) if there is any problem :heart:"])
time.sleep(2)
if __name__ == "__main__":
apps = json.load(open("../../builds/default/v3/apps.json"))["apps"]
with Repository(
f"https://{login}:{token}@github.com/yunohost/apps_translations", "main"
) as repository:
extract_strings_to_translate_from_apps(apps, repository)
if not repository.run_command(
"hub pr list -h manifest_toml_i18n", capture_output=True
):
repository.run_command(
[
"hub",
"pull-request",
"-m",
"Update translations for manifest.toml",
"-b",
branch,
"-h",
"manifest_toml_i18n",
"-p",
"-m",
f"This pull request is automatically generated by scripts from the [YunoHost/apps](https://github.com/YunoHost/apps) repository.\n\nThe translation is pull from weblate and is located here: https://translate.yunohost.org/projects/yunohost-apps/{app}/\n\nIf you wish to modify the translation (other than in english), please do that directly on weblate since this is now the source of authority for it.\n\nDon't hesitate to reach the YunoHost team on [matrix](https://matrix.to/#/#yunohost:matrix.org) if there is any problem :heart:",
]
)