#!/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())