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
2024-04-02 16:57:35 +02:00

148 lines
5.8 KiB
Python

import time
import json
from pathlib import Path
import tomlkit
from base import Repository, login, token, WORKING_BRANCH
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()
print(app)
print("=" * len(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
if repository.run_command_as_if(["git", "rev-parse", "--verify", "origin/testing"]):
repository.run_command(
[
"git",
"checkout",
"-b",
WORKING_BRANCH,
"--track",
"origin/testing",
]
)
branch = "testing"
else:
repository.run_command(["git", "checkout", "-b", WORKING_BRANCH])
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
one_of_the_existing_languages = list(
manifest["install"][question][strings_to_translate].keys()
)[0]
current_identation = len(
manifest["install"][question][strings_to_translate][
one_of_the_existing_languages
].trivia.indent
)
manifest["install"][question][strings_to_translate][
language
] = translation[translation_key]
manifest["install"][question][strings_to_translate][
language
].indent(current_identation)
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", f"{WORKING_BRANCH}:manifest_toml_i18n"])
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",
"This pull request is automatically generated by scripts from the "
"[YunoHost/apps](https://github.com/YunoHost/apps) repository.\n\n"
"The translation is pull from weblate and is located here: "
f"https://translate.yunohost.org/projects/yunohost-apps/{app}/\n\n"
"If 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)