From 9b11dd980b96fde752ca104f123fc843eb007dbc Mon Sep 17 00:00:00 2001 From: fkDead Date: Thu, 26 Nov 2026 18:18:25 +0100 Subject: [PATCH] feat(installer): merge the missing translations into Anaconda's catalogs Each text is added to anaconda.mo only when Anaconda has no translation or a copy of the English text: its own translations always win. One catalog serves both the Python code and GtkBuilder, so the .mo is rewritten rather than a second domain added. --- .../lib/fws/merge-anaconda-translations.py | 112 ++++++++++++++++++ 1 file changed, 112 insertions(+) create mode 100644 configs/baseline/airootfs/usr/local/lib/fws/merge-anaconda-translations.py diff --git a/configs/baseline/airootfs/usr/local/lib/fws/merge-anaconda-translations.py b/configs/baseline/airootfs/usr/local/lib/fws/merge-anaconda-translations.py new file mode 100644 index 0000000..f77f2e4 --- /dev/null +++ b/configs/baseline/airootfs/usr/local/lib/fws/merge-anaconda-translations.py @@ -0,0 +1,112 @@ +#!/usr/bin/env python3 +"""Add the FWS translations of Anaconda's own texts to Anaconda's catalogs. + +Run at build time by customize_airootfs.sh (usage: merge-anaconda-translations.py +[--root DIR]). Anaconda's catalogs leave some of its screens partly in English +in several languages (Bulgarian lacks most of the root account screen, for +instance). anaconda-translations.toml holds translations for the texts of the +screens FWS shows; each one is added to /usr/share/locale//LC_MESSAGES/ +anaconda.mo only when Anaconda has none, or only a copy of the English text +(Italian has "_Root Account" as is): its own translations always win. One +catalog serves both the Python code and GtkBuilder, so the .mo is rewritten +rather than a second domain added. +""" + +import argparse +import os +import struct +import sys +import tomllib + +SUPPLEMENT = "usr/local/share/fws/anaconda-translations.toml" +CATALOG = "usr/share/locale/{language}/LC_MESSAGES/anaconda.mo" +CONTEXT_SEPARATOR = b"\x04" + + +def read_mo(path): + """[(original bytes, translation bytes)], plural and context keys as stored.""" + with open(path, "rb") as fh: + data = fh.read() + magic = struct.unpack("" + _, count, originals, translations = struct.unpack(order + "4I", data[4:20]) + entries = [] + for index in range(count): + length, offset = struct.unpack(order + "2I", data[originals + 8 * index:originals + 8 * index + 8]) + key = data[offset:offset + length] + length, offset = struct.unpack(order + "2I", data[translations + 8 * index:translations + 8 * index + 8]) + entries.append((key, data[offset:offset + length])) + return entries + + +def write_mo(path, entries): + """Write a .mo without hash table: gettext then looks keys up by binary + search, which needs the originals sorted bytewise.""" + entries = sorted(entries) + count = len(entries) + header = 7 * 4 + originals_table = header + translations_table = originals_table + 8 * count + data_start = translations_table + 8 * count + blobs, original_index, translation_index = b"", b"", b"" + for key, _ in entries: + original_index += struct.pack("<2I", len(key), data_start + len(blobs)) + blobs += key + b"\0" + for _, value in entries: + translation_index += struct.pack("<2I", len(value), data_start + len(blobs)) + blobs += value + b"\0" + with open(path + ".fws", "wb") as fh: + fh.write(struct.pack("<7I", 0x950412DE, 0, count, originals_table, + translations_table, 0, data_start)) + fh.write(original_index + translation_index + blobs) + os.replace(path + ".fws", path) + + +def is_translated(key, value): + """A copy of the English text is how some catalogs leave a text untranslated.""" + msgid = key.split(CONTEXT_SEPARATOR, 1)[-1] + return bool(value) and value != msgid + + +def main(): + parser = argparse.ArgumentParser(description=__doc__.splitlines()[0]) + parser.add_argument("--root", default="/") + args = parser.parse_args() + + supplement = os.path.join(args.root, SUPPLEMENT) + try: + with open(supplement, "rb") as fh: + messages = tomllib.load(fh).get("message", []) + except (OSError, tomllib.TOMLDecodeError) as exc: + print(f"merge-anaconda-translations: {supplement}: {exc}", file=sys.stderr) + return 1 + + by_language = {} + for message in messages: + key = message["msgid"].encode() + if message.get("context"): + key = message["context"].encode() + CONTEXT_SEPARATOR + key + for language, text in message.items(): + # A text that reads the same as the English (de "Region") would + # change nothing on screen and look missing again on every run. + if language not in ("msgid", "context") and text != message["msgid"]: + by_language.setdefault(language, {})[key] = text.encode() + + added = {} + for language, texts in sorted(by_language.items()): + path = os.path.join(args.root, CATALOG.format(language=language)) + if not os.path.exists(path): + print(f"merge-anaconda-translations: no Anaconda catalog for {language}", file=sys.stderr) + continue + entries = read_mo(path) + present = {key for key, value in entries if is_translated(key, value)} + new = [(key, value) for key, value in texts.items() if key not in present] + if new: + write_mo(path, [(key, value) for key, value in entries if key not in texts or key in present] + new) + added[language] = len(new) + print("merge-anaconda-translations: " + (", ".join(f"{lang} +{n}" for lang, n in added.items()) or "nothing to add")) + return 0 + + +if __name__ == "__main__": + sys.exit(main())