From cf7de41671c41fbef0310231c88fd0bdf0ede108 Mon Sep 17 00:00:00 2001 From: Oliver Hamlet Date: Tue, 21 May 2024 22:20:00 +0100 Subject: [PATCH] Add script to write strings from translations to prelude.yaml --- README.md | 10 +++++-- scripts/import-translations.py | 49 ++++++++++++++++++++++++++++++++++ 2 files changed, 57 insertions(+), 2 deletions(-) create mode 100644 scripts/import-translations.py diff --git a/README.md b/README.md index 192059f..820aeec 100644 --- a/README.md +++ b/README.md @@ -6,7 +6,7 @@ See [CONTRIBUTING.md](CONTRIBUTING.md) for information on how to contribute to t ### 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`. +The `translations` directory holds files that are read and written by [Weblate](https://hosted.weblate.org/projects/loot/prelude/). There are a couple of scripts that can be used to keep them in sync with `prelude.yaml`. To use the scripts, first install their dependencies in a virtual environment. On Windows, make sure Python 3 is installed, then run: @@ -22,4 +22,10 @@ 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. +It's also possible to overwrite the message text in `prelude.yaml` using the contents of the `translations` directory: + +``` +py scripts/import-translations.py +``` + +The scripts make assumptions about the formatting and layout of entries in `prelude.yaml`, so it's worth double-checking their changes. diff --git a/scripts/import-translations.py b/scripts/import-translations.py new file mode 100644 index 0000000..2327174 --- /dev/null +++ b/scripts/import-translations.py @@ -0,0 +1,49 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +# Read localised message content strings from translations/messages..yaml +# files and use them to replace the corresponding strings in prelude.yaml. +import math +import re +import os + +import yaml + +messages = {} +for f in os.scandir('translations'): + match = re.match(r'messages\.([^\.]+)\.yaml', f.name) + if match: + lang = match.group(1) + with open(f.path, encoding='utf8') as input: + messages[lang] = yaml.safe_load(input) + +lines = [] +with open('prelude.yaml', encoding='utf8') as input: + lines = input.readlines() + + anchor = None + lang = None + for index, line in enumerate(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 and anchor and lang: + # Replace line with a new line for this message + message = yaml.dump(messages[lang][anchor], width=math.inf, allow_unicode=True, default_style="'") + + lines[index] = re.sub(r' text: \'.+\'', f' text: {message.strip()}', line) + + +with open('prelude.yaml', mode='w', encoding='utf8') as output: + output.writelines(lines)