CLI to manage messages

This commit is contained in:
Yohann D'ANELLO 2020-11-28 03:21:20 +01:00
parent 8aad15f07b
commit 7c0cf3e029
2 changed files with 31 additions and 9 deletions

16
main.py
View File

@ -2,8 +2,24 @@
# Copyright (C) 2020 by ÿnérant, eichhornchen, nicomarg, charlse # Copyright (C) 2020 by ÿnérant, eichhornchen, nicomarg, charlse
# SPDX-License-Identifier: GPL-3.0-or-later # SPDX-License-Identifier: GPL-3.0-or-later
import argparse
import sys
from squirrelbattle.bootstrap import Bootstrap from squirrelbattle.bootstrap import Bootstrap
from squirrelbattle.translations import Translator
if __name__ == "__main__": if __name__ == "__main__":
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() Bootstrap.run_game()

View File

@ -2,6 +2,7 @@
# SPDX-License-Identifier: GPL-3.0-or-later # SPDX-License-Identifier: GPL-3.0-or-later
import gettext as gt import gettext as gt
import os
import subprocess import subprocess
from pathlib import Path from pathlib import Path
from typing import Any, List from typing import Any, List
@ -18,6 +19,9 @@ class Translator:
translators: dict = {} translators: dict = {}
for language in SUPPORTED_LOCALES: for language in SUPPORTED_LOCALES:
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( translators[language] = gt.translation(
"squirrelbattle", "squirrelbattle",
localedir=Path(__file__).parent / "locale", localedir=Path(__file__).parent / "locale",
@ -41,19 +45,21 @@ class Translator:
@classmethod @classmethod
def makemessages(cls) -> None: # pragma: no cover def makemessages(cls) -> None: # pragma: no cover
for language in cls.SUPPORTED_LOCALES: for language in cls.SUPPORTED_LOCALES:
file_name = Path(__file__).parent / "locale" / language \
/ "LC_MESSAGES" / "squirrelbattle.po"
args = ["find", "squirrelbattle", "-iname", "*.py"] args = ["find", "squirrelbattle", "-iname", "*.py"]
find = subprocess.Popen(args, cwd=Path(__file__).parent.parent, find = subprocess.Popen(args, cwd=Path(__file__).parent.parent,
stdout=subprocess.PIPE) stdout=subprocess.PIPE)
args = ["xargs", "xgettext", "--from-code", "utf-8", args = ["xargs", "xgettext", "--from-code", "utf-8",
"--join-existing",
"--add-comments", "--add-comments",
"--package-name=squirrelbattle", "--package-name=squirrelbattle",
"--package-version=3.14.1", "--package-version=3.14.1",
"--copyright-holder=ÿnérant, eichhornchen, " "--copyright-holder=ÿnérant, eichhornchen, "
"nicomarg, charlse", "nicomarg, charlse",
"--msgid-bugs-address=squirrel-battle@crans.org", "--msgid-bugs-address=squirrel-battle@crans.org",
"-o", Path(__file__).parent / "locale" / language "-o", file_name]
/ "LC_MESSAGES" / "squirrelbattle.po"] if file_name.is_file():
args.append("--join-existing")
print(f"Make {language} messages...") print(f"Make {language} messages...")
subprocess.Popen(args, stdin=find.stdout) subprocess.Popen(args, stdin=find.stdout)