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.
This commit is contained in:
Oliver Hamlet
2024-06-30 09:37:28 +01:00
parent 93c03717cf
commit f44dc242d4
4 changed files with 73 additions and 1 deletions
+2
View File
@@ -213,3 +213,5 @@ pip-log.txt
#Mr Developer
.mr.developer.cfg
.venv
+21 -1
View File
@@ -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.
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.
+1
View File
@@ -0,0 +1 @@
pyyaml==6.0.1
+49
View File
@@ -0,0 +1,49 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Read localised message content strings from prelude.yaml and generate
# translations/messages.<lang>.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)