# SPDX-License-Identifier: MIT """Fallback string extractor when xgettext is not available. Collects _("...") calls (with adjacent literal concatenation) from C++ sources and writes a gettext template. Prefer `cmake --build --target pot` (xgettext) when gettext is installed. usage: python3 tools/extract_strings.py po/openpu.pot src/ui-gtk/main_window.cpp ... """ import re import sys CALL = re.compile(r'\b_\(\s*((?:"(?:[^"\\]|\\.)*"\s*)+)\)', re.S) LITERAL = re.compile(r'"((?:[^"\\]|\\.)*)"') def main(): out, *sources = sys.argv[1:] seen = {} for path in sources: text = open(path, encoding="utf-8").read() for m in CALL.finditer(text): msgid = "".join(LITERAL.findall(m.group(1))) line = text.count("\n", 0, m.start()) + 1 seen.setdefault(msgid, []).append(f"{path}:{line}") with open(out, "w", encoding="utf-8", newline="\n") as f: f.write('msgid ""\nmsgstr ""\n"Project-Id-Version: openpu\\n"\n' '"Content-Type: text/plain; charset=UTF-8\\n"\n"Content-Transfer-Encoding: 8bit\\n"\n') for msgid, refs in seen.items(): f.write("\n#: " + " ".join(refs) + "\n") if "%1" in msgid: f.write("#, qt-format\n") f.write(f'msgid "{msgid}"\nmsgstr ""\n') print(f"{len(seen)} strings -> {out}") main()