From a162a88d484db85667c6cd4471acdebd426e9395 Mon Sep 17 00:00:00 2001 From: Christophe Larchier Date: Tue, 4 Jun 2024 11:07:40 +0200 Subject: [PATCH] Fixed RN translation script --- infomaniak-build-tools/translate_release.py | 123 ++++++++++++++++---- 1 file changed, 100 insertions(+), 23 deletions(-) diff --git a/infomaniak-build-tools/translate_release.py b/infomaniak-build-tools/translate_release.py index cfbe01646..ffd41fcf0 100755 --- a/infomaniak-build-tools/translate_release.py +++ b/infomaniak-build-tools/translate_release.py @@ -1,31 +1,69 @@ #!/usr/bin/env python3 +# +# Infomaniak kDrive - Desktop +# Copyright (C) 2023-2024 Infomaniak Network SA +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . +# +import argparse import datetime import deepl +import errno import os import re +import shutil import subprocess import sys -pattern = re.compile("^(\d+\.)?(\d+\.)?(\*|\d+)") +def version_regex(arg_value, pattern=re.compile("^(\d+\.)?(\d+\.)?(\*|\d+)")): + if not pattern.match(arg_value): + raise argparse.ArgumentTypeError("invalid version") + return arg_value -if len(sys.argv) < 2 or not pattern.match(sys.argv[1]): - version = input("Enter the release version: ") -else: - version = sys.argv[1] +parser = argparse.ArgumentParser( + description="""Translater script for the desktop kDrive release notes. +The translation is done using DeepL API from English to French, German, Italian and Spanish. +Please make sure your DeepL API key is exported as DEEPL_AUTH_KEY before running the script. -if len(sys.argv) < 3: - date = datetime.date.today().strftime('%Y%m%d') -else: - date = sys.argv[2] +A default english version of the Release Notes with all the systems combined must be created first. +This script requires a file named kDrive-template.html to exist in the current working directory.""", + epilog=""" +To add more languages in the future, edit the script to append the new language to the list. + All the languages in the list must be in the DeepL API target language list. + Language list : https://developers.deepl.com/docs/api-reference/languages -if not os.path.isfile(f"kDrive-{version}.{date}.html"): - quit(); +To add more systems, edit the script to append the new system to the list. + System specific Release Notes entries must start with the system name. + eg: "Windows - Added new feature" for Windows specific feature. + """, + formatter_class=argparse.RawTextHelpFormatter) -dirPath = "../release_notes/" -fullName = f"kDrive-{version}.{date} +parser.add_argument('-v', '--version', metavar="", type=version_regex, help='The release version (eg: 3.6.0)', required=True) +parser.add_argument('-d', '--date', metavar="", type=int, help='The planned release date (defaults to today)', default=datetime.date.today().strftime('%Y%m%d')) +args = parser.parse_args() -translator = deepl.Translator(os.getenv("DEEPL_AUTH_KEY")) +if not os.path.isfile(f"kDrive-template.html"): + sys.exit("Unable to find kDrive-template.html."); + +fullName = f"kDrive-{args.version}.{args.date}" +dirPath = f"../release_notes/{fullName}" + +deepl_key = os.getenv("DEEPL_AUTH_KEY") +if not deepl_key: + sys.exit("error: The DeepL API key is not set in env"); +translator = deepl.Translator(deepl_key) target_lang = [ 'FR', @@ -34,16 +72,55 @@ 'IT' ] -os.mkdir(f"{dirPath}/{fullName}", mode = 0o755) +os_list = [ + 'Win', + 'Linux', + 'macOS' +] + +def split_os(lang): + lang_ext = f"-{lang.lower()}" if lang != 'en' else "" + + for os_name in os_list: + os_ext = f"-{os_name.lower()}" if os_name != 'macOS' else "" + if os_name != 'macOS': + shutil.copyfile(f"{fullName}{lang_ext}.html", f"{fullName}{os_ext}{lang_ext}.html") + with open(f"{fullName}{os_ext}{lang_ext}.html", "r") as f: + lines = f.readlines() + with open(f"{fullName}{os_ext}{lang_ext}.html", "w") as f: + for line in lines: + if any(os_note in line for os_note in os_list): + if (f"
  • {os_name}" in line): + f.write(f"\t\t
  • {line[line.find('-') + 2:]}") + else: + f.write(line) + +print(f"Generating Release Notes for kDrive-{args.version}.{args.date}") + +try: + os.mkdir(dirPath, mode = 0o755) +except OSError as exc: + if exc.errno != errno.EEXIST: + raise + pass + +shutil.copyfile("kDrive-template.html", f"{dirPath}/{fullName}.html") +os.chdir(dirPath) + +try: + for lang in target_lang: + translator.translate_document_from_filepath( + f"{fullName}.html", + f"{fullName}-{lang.lower()}.html", + target_lang=lang, + ) + split_os(lang) +except Exception as e: + print(e) -for lang in target_lang: - translator.translate_document_from_filepath( - {fullName}.html", - f"{dirPath}/{fullName}-{lang.lower()}.html", - target_lang=lang, - ) +split_os('en') -subprocess.run(['./encode.sh'], shell=True) +subprocess.run(['../../infomaniak-build-tools/encode.sh'], shell=True) usage = translator.get_usage() -print(f"Translation done. {usage.character} characters used for the month.") +print(f"Translation done. {usage.character} characters used for the month.") \ No newline at end of file