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 2faffe4 commit acfa1b0
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 20 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/website.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
65 changes: 47 additions & 18 deletions website/scripts/mkdocs_tx.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,41 +6,68 @@


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"]:
nav_config.append({v: k for k, v in _entry.items()})

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__":
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")
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")
Expand All @@ -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(
Expand All @@ -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
3 changes: 2 additions & 1 deletion website/scripts/transifex_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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<lang>.md\n")
f.write("resource_name = site navigation\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")
Expand Down

0 comments on commit acfa1b0

Please sign in to comment.