Skip to content

Commit

Permalink
translate mkdocs config
Browse files Browse the repository at this point in the history
  • Loading branch information
3nids committed Feb 18, 2024
1 parent 0cc5bc3 commit 31c066e
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/website.yml
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ jobs:
curl -OL https://github.com/transifex/cli/releases/download/v1.6.10/tx-linux-amd64.tar.gz
tar -xvzf tx-linux-amd64.tar.gz
- name: Extract translatable content from mkdocs.yml config
run: ./website/scripts/mkdocs_tx.py create_source mkdocs_tx.yaml

- name: Configure Transifex
run: scripts/transifex_utils.py
env:
Expand Down
62 changes: 62 additions & 0 deletions website/scripts/mkdocs_tx.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
#!/usr/bin/env python

import argparse

from ruamel.yaml import YAML


def read_config(file_path: str):
yaml = YAML(typ="safe")
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"])

tx_cfg = {"nav": config["nav"]}

tx_cfg["theme"] = {"palette": []}
for palette in config["theme"]["palette"]:
tx_cfg["theme"]["palette"].append(
{"toggle": {"name": palette["toggle"]["name"]}}
)

with open(source_path, "w") as f:
yaml = YAML()
yaml.dump({source_language: tx_cfg}, f)


if __name__ == "__main__":
parser = argparse.ArgumentParser()
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"
)

subparsers = parser.add_subparsers(title="command", dest="command")

# create the parser for the create_source command
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(
"update_config",
help="Updates the mkdocs.yaml config file from the downloaded translated files",
)

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)
8 changes: 8 additions & 0 deletions website/scripts/transifex_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,14 @@ def create_transifex_config():
f.write("[main]\n")
f.write("host = https://www.transifex.com\n\n")

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_config]\n")
f.write("file_filter = mkdocs_tx<lang>.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")

for file in glob.iglob(
current_dir + "/../documentation/**/*.fr.md", recursive=True
):
Expand Down

0 comments on commit 31c066e

Please sign in to comment.