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

18
main.py
View File

@ -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()

View File

@ -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)