From acfa1b03ed921fde1165d0d743dcf6a444f39d38 Mon Sep 17 00:00:00 2001 From: Denis Rouzaud Date: Mon, 19 Feb 2024 08:16:55 +0100 Subject: [PATCH] fix --- .github/workflows/website.yml | 2 +- website/scripts/mkdocs_tx.py | 65 +++++++++++++++++++++--------- website/scripts/transifex_utils.py | 3 +- 3 files changed, 50 insertions(+), 20 deletions(-) diff --git a/.github/workflows/website.yml b/.github/workflows/website.yml index f94c10f5..5d6632eb 100644 --- a/.github/workflows/website.yml +++ b/.github/workflows/website.yml @@ -53,7 +53,7 @@ jobs: tar -xvzf tx-linux-amd64.tar.gz - name: Extract translatable content from mkdocs.yml config - run: ./scripts/mkdocs_tx.py create_source mkdocs_tx.yaml + run: ./scripts/mkdocs_tx.py create_source - name: Configure Transifex run: scripts/transifex_utils.py diff --git a/website/scripts/mkdocs_tx.py b/website/scripts/mkdocs_tx.py index 6fa3965c..acf2bcf5 100755 --- a/website/scripts/mkdocs_tx.py +++ b/website/scripts/mkdocs_tx.py @@ -6,16 +6,14 @@ def read_config(file_path: str): - yaml = YAML(typ="safe") + yaml = YAML(typ="rt") + yaml.preserve_quotes = True with open(file_path) as f: return yaml.load(f) -def create_translation_source(config, source_path, source_language): - for plugin in config["plugins"]: - if type(plugin) == dict and "i18n" in plugin: - for lang in plugin["i18n"]["languages"]: - print(lang["locale"]) +def create_translation_source(config_path, source_path): + config = read_config(config_path) nav_config = [] for _entry in config["nav"]: @@ -23,15 +21,40 @@ def create_translation_source(config, source_path, source_language): tx_cfg = {"nav": nav_config} - tx_cfg["theme"] = {"palette": []} - for palette in config["theme"]["palette"]: - tx_cfg["theme"]["palette"].append( - {"toggle": {"name": palette["toggle"]["name"]}} - ) + try: + tx_cfg["theme"] = {"palette": []} + for palette in config["theme"]["palette"]: + tx_cfg["theme"]["palette"].append( + {"toggle": {"name": palette["toggle"]["name"]}} + ) + except KeyError: + print("No theme/palette/toggle/name to translate") with open(source_path, "w") as f: yaml = YAML() - yaml.dump({source_language: tx_cfg}, f) + yaml.dump(tx_cfg, f) + + +def update_config(config_path, source_path): + config = read_config(config_path) + + for plugin in config["plugins"]: + if type(plugin) == dict and "i18n" in plugin: + for lang in plugin["i18n"]["languages"]: + ltx = lang["locale"] + + tx_file = source_path.replace(".yml", f".{ltx}.yml") + with open(tx_file) as f: + yaml = YAML(typ="safe") + tx = yaml.load(f) + + for nav_entry in tx["nav"]: + print(nav_entry) + + with open(config_path, "w") as f: + yaml = YAML() + yaml.indent(mapping=2, sequence=4, offset=2) + yaml.dump(config, f) if __name__ == "__main__": @@ -39,8 +62,12 @@ def create_translation_source(config, source_path, source_language): parser.add_argument( "-c", "--config_path", default="mkdocs.yml", help="mkdocs.yml complete path" ) + # parser.add_argument("-s", "--source_language", default="en", help="source language of the config") parser.add_argument( - "-s", "--source_language", default="en", help="source language of the config" + "-t", + "--translation_file_path", + default="mkdocs_tx.yaml", + help="Translation file to create and translate", ) subparsers = parser.add_subparsers(title="command", dest="command") @@ -49,9 +76,6 @@ def create_translation_source(config, source_path, source_language): parser_source = subparsers.add_parser( "create_source", help="Creates the source file to be translated" ) - parser_source.add_argument( - "translation_file_path", help="Translation file to create" - ) # create the parser for the update_config command parser_update = subparsers.add_parser( @@ -60,7 +84,12 @@ def create_translation_source(config, source_path, source_language): ) args = parser.parse_args() - cfg = read_config(args.config_path) if args.command == "create_source": - create_translation_source(cfg, args.translation_file_path, args.source_language) + create_translation_source(args.config_path, args.translation_file_path) + + elif args.command == "update_config": + update_config(args.config_path, args.translation_file_path) + + else: + raise ValueError diff --git a/website/scripts/transifex_utils.py b/website/scripts/transifex_utils.py index cd62bdb7..6f4f2f8c 100755 --- a/website/scripts/transifex_utils.py +++ b/website/scripts/transifex_utils.py @@ -28,7 +28,8 @@ def create_transifex_config(): if os.path.isfile(f"{root}/mkdocs_tx.yaml"): print(f"Found mkdocs config translated content") f.write(f"[o:{TX_ORGANIZATION}:p:{TX_PROJECT}:r:site_navigation]\n") - f.write("file_filter = mkdocs_tx.md\n") + f.write("resource_name = site navigation\n") + f.write("file_filter = mkdocs_tx..md\n") f.write(f"source_file = mkdocs_tx.yaml\n") f.write(f"source_lang = {TX_SOURCE_LANG}\n") f.write(f"type = YAML_GENERIC\n\n")