From 7c0cf3e029d408e857e7172a0567af387216eed2 Mon Sep 17 00:00:00 2001 From: Yohann D'ANELLO Date: Sat, 28 Nov 2020 03:21:20 +0100 Subject: [PATCH] CLI to manage messages --- main.py | 18 +++++++++++++++++- squirrelbattle/translations.py | 22 ++++++++++++++-------- 2 files changed, 31 insertions(+), 9 deletions(-) diff --git a/main.py b/main.py index e8c333e..fbbbb35 100755 --- a/main.py +++ b/main.py @@ -2,8 +2,24 @@ # Copyright (C) 2020 by ÿnérant, eichhornchen, nicomarg, charlse # SPDX-License-Identifier: GPL-3.0-or-later +import argparse +import sys from squirrelbattle.bootstrap import Bootstrap +from squirrelbattle.translations import Translator if __name__ == "__main__": - Bootstrap.run_game() + parser = argparse.ArgumentParser() + + parser.add_argument("--makemessages", "-mm", action="store_true", + help="Extract translatable strings") + parser.add_argument("--compilemessages", "-cm", action="store_true", + help="Compile translatable strings") + + args = parser.parse_args(sys.argv[1:]) + if args.makemessages: + Translator.makemessages() + elif args.compilemessages: + Translator.compilemessages() + else: + Bootstrap.run_game() diff --git a/squirrelbattle/translations.py b/squirrelbattle/translations.py index 7a0e524..dfd3cf3 100644 --- a/squirrelbattle/translations.py +++ b/squirrelbattle/translations.py @@ -2,6 +2,7 @@ # SPDX-License-Identifier: GPL-3.0-or-later import gettext as gt +import os import subprocess from pathlib import Path from typing import Any, List @@ -18,11 +19,14 @@ class Translator: translators: dict = {} for language in SUPPORTED_LOCALES: - translators[language] = gt.translation( - "squirrelbattle", - localedir=Path(__file__).parent / "locale", - languages=[language], - ) + dir = Path(__file__).parent / "locale" / language / "LC_MESSAGES" + dir.mkdir(parents=True) if not dir.is_dir() else None + if os.path.isfile(dir / "squirrelbattle.mo"): + translators[language] = gt.translation( + "squirrelbattle", + localedir=Path(__file__).parent / "locale", + languages=[language], + ) @classmethod def setlocale(cls, lang: str) -> None: @@ -41,19 +45,21 @@ class Translator: @classmethod def makemessages(cls) -> None: # pragma: no cover for language in cls.SUPPORTED_LOCALES: + file_name = Path(__file__).parent / "locale" / language \ + / "LC_MESSAGES" / "squirrelbattle.po" args = ["find", "squirrelbattle", "-iname", "*.py"] find = subprocess.Popen(args, cwd=Path(__file__).parent.parent, stdout=subprocess.PIPE) args = ["xargs", "xgettext", "--from-code", "utf-8", - "--join-existing", "--add-comments", "--package-name=squirrelbattle", "--package-version=3.14.1", "--copyright-holder=ÿnérant, eichhornchen, " "nicomarg, charlse", "--msgid-bugs-address=squirrel-battle@crans.org", - "-o", Path(__file__).parent / "locale" / language - / "LC_MESSAGES" / "squirrelbattle.po"] + "-o", file_name] + if file_name.is_file(): + args.append("--join-existing") print(f"Make {language} messages...") subprocess.Popen(args, stdin=find.stdout)