import time import json from pathlib import Path import tomlkit from base import Repository, login, token, WORKING_BRANCH, get_repository_branches 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 if "testing" in get_repository_branches(repository_uri, token): branch = "testing" with Repository( f"https://{login}:{token}@github.com/{repository_uri}", branch ) as repository: if not repository.file_exists("manifest.toml"): continue repository.run_command( [ "git", "checkout", "-b", WORKING_BRANCH, "--track", "origin/{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)