Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
3nids committed Feb 19, 2024
1 parent acfa1b0 commit 9da8469
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 9 deletions.
26 changes: 26 additions & 0 deletions .github/workflows/website.yml
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,32 @@ jobs:
env:
TX_TOKEN: ${{ secrets.TX_TOKEN }}

- name: Translate Mkdocs config
if: ${{ github.event_name == 'push' || github.event.pull_request.head.repo.full_name == 'opengisch/signalo' && github.actor != 'dependabot[bot]' }}
run: |
./website/scripts/mkdocs_tx.py -s fr update_config
if [[ git diff --exit-code mkdocs.yml ]]; then
echo "no change mkdocs.yml"
else
echo "detected changes in mkdocs.yml"
if [[ ${{ github.event_name } == "pull_request" ]]; then
# on PR push to the same branch
gh pr checkout ${{ github.event.pull_request.number }}
git add mkdocs.yml
git commit -m "Update mkdocs.yml translation"
git push
else
# on push create a pull request
git checkout ${{ github.head_ref }}
git checkout -b update-mkdocs-tx
git commit -m "Update mkdocs.yml translation"
gh pr create -B update-mkdocs-tx -H update-mkdocs-tx --title 'Update mkdocs translations'
fi
fi
gh pr create -B base_branch -H branch_to_merge --title 'Merge branch_to_merge into base_branch' --body 'Created by Github action'
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

- name: Build documentation
run: mkdocs build

Expand Down
51 changes: 42 additions & 9 deletions website/scripts/mkdocs_tx.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#!/usr/bin/env python

import argparse
import copy

from ruamel.yaml import YAML

Expand Down Expand Up @@ -35,21 +36,49 @@ def create_translation_source(config_path, source_path):
yaml.dump(tx_cfg, f)


def update_config(config_path, source_path):
def update_config(config_path, source_path, source_language):
config = read_config(config_path)

nav_config = {}
for _entry in config["nav"]:
for title, page in _entry.items():
nav_config[page] = title

found = False
for plugin in config["plugins"]:
if type(plugin) == dict and "i18n" in plugin:
if type(plugin) != str and "i18n" in plugin:
found = True
for lang in plugin["i18n"]["languages"]:
ltx = lang["locale"]
print(f"language found: '{ltx}'")

if ltx == source_language:
print("skipping source language")
continue

tx_file = source_path.replace(".yml", f".{ltx}.yml")
tx_file = f'{source_path.removesuffix(".yml")}.{ltx}.yml'
with open(tx_file) as f:
yaml = YAML(typ="safe")
yaml = YAML()
tx = yaml.load(f)

for nav_entry in tx["nav"]:
print(nav_entry)
for page, title in nav_entry.items():
source_language_tile = nav_config[page]
if title:
lang["nav_translations"][source_language_tile] = title

try:
lang["palette"] = copy.deepcopy(config["theme"]["palette"])
i = 0
for palette in tx["theme"]["palette"]:
lang["palette"][i]["toggle"]["name"] = palette["toggle"][
"name"
]
i += 1
except KeyError:
print("No theme/palette/toggle/name to translate")

assert found

with open(config_path, "w") as f:
yaml = YAML()
Expand All @@ -62,11 +91,13 @@ def update_config(config_path, source_path):
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"
)
parser.add_argument(
"-t",
"--translation_file_path",
default="mkdocs_tx.yaml",
default="mkdocs_tx.yml",
help="Translation file to create and translate",
)

Expand All @@ -80,7 +111,7 @@ def update_config(config_path, source_path):
# 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",
help="Updates the mkdocs.yml config file from the downloaded translated files",
)

args = parser.parse_args()
Expand All @@ -89,7 +120,9 @@ def update_config(config_path, source_path):
create_translation_source(args.config_path, args.translation_file_path)

elif args.command == "update_config":
update_config(args.config_path, args.translation_file_path)
update_config(
args.config_path, args.translation_file_path, args.source_language
)

else:
raise ValueError

0 comments on commit 9da8469

Please sign in to comment.