From f44dc242d4ac5a1f5fd24aef2dbfaae44f347bb4 Mon Sep 17 00:00:00 2001 From: Oliver Hamlet Date: Tue, 21 May 2024 21:34:14 +0100 Subject: [PATCH] Add script to generate files in translations folder Unfortunately anchors are discarded while parsing YAML, so regex is used to pick out the relevant lines. --- .gitignore | 2 ++ README.md | 22 ++++++++++++++- requirements.txt | 1 + scripts/export-translations.py | 49 ++++++++++++++++++++++++++++++++++ 4 files changed, 73 insertions(+), 1 deletion(-) create mode 100644 requirements.txt create mode 100644 scripts/export-translations.py diff --git a/.gitignore b/.gitignore index b9d6bd9..8189c7a 100644 --- a/.gitignore +++ b/.gitignore @@ -213,3 +213,5 @@ pip-log.txt #Mr Developer .mr.developer.cfg + +.venv diff --git a/README.md b/README.md index 111baa1..192059f 100644 --- a/README.md +++ b/README.md @@ -2,4 +2,24 @@ This repository holds the masterlist prelude, a metadata file that is used to supply common metadata to all masterlists. -See [CONTRIBUTING.md](CONTRIBUTING.md) for information on how to contribute to the prelude. \ No newline at end of file +See [CONTRIBUTING.md](CONTRIBUTING.md) for information on how to contribute to the prelude. + +### Synchronising Weblate translations + +The `translations` directory holds files that are read and written by [Weblate](https://hosted.weblate.org/projects/loot/prelude/). There's a script that can be used to regenerate their content from `prelude.yaml`. + +To use the scripts, first install their dependencies in a virtual environment. On Windows, make sure Python 3 is installed, then run: + +``` +py -m venv .venv +.\.venv\Scripts\activate +pip install -r requirements.txt +``` + +To regenerate the files in the `translations` directory from `prelude.yaml`, run: + +``` +py scripts/export-translations.py +``` + +The script makes assumptions about the formatting and layout of entries in `prelude.yaml`, so it's worth double-checking its changes. diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..cf39afa --- /dev/null +++ b/requirements.txt @@ -0,0 +1 @@ +pyyaml==6.0.1 diff --git a/scripts/export-translations.py b/scripts/export-translations.py new file mode 100644 index 0000000..2094c47 --- /dev/null +++ b/scripts/export-translations.py @@ -0,0 +1,49 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +# Read localised message content strings from prelude.yaml and generate +# translations/messages..yaml files from them. +import math +import re + +from yaml import dump + +# This makes some assumptions about the prelude.yaml: +# - anchors are always separated from anything before them by at least one space +# - message content objects are listed with the lang as the first key, followed +# by the text +# - message content text is enclosed in single quotes + +messages = {} +with open('prelude.yaml', encoding='utf8') as input: + lines = input.readlines() + + anchor = None + lang = None + for line in lines: + match = re.search(r' &([^\s]+)$', line) + if match != None: + anchor = match.group(1) + lang = None + text = None + continue + + match = re.search(r'- lang: ([a-zA-Z_]+)', line) + if match != None: + lang = match.group(1) + text = None + continue + + match = re.search(r' text: \'(.+)\'', line) + if match != None: + text = match.group(1).replace("''", "'") + + if anchor and lang and text: + if lang in messages: + messages[lang][anchor] = text + else: + messages[lang] = { anchor: text } + +for lang in messages: + with open(f'translations/messages.{lang}.yaml', mode='w', encoding='utf8') as output: + dump(messages[lang], output, allow_unicode=True, width=math.inf)