Tests and the CI are compiling messages

This commit is contained in:
Yohann D'ANELLO
2020-11-28 14:02:23 +01:00
parent a34dae2ad0
commit f78c73a703
4 changed files with 46 additions and 26 deletions

View File

@ -18,15 +18,20 @@ class Translator:
locale: str = "en"
translators: dict = {}
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(
"squirrelbattle",
localedir=Path(__file__).parent / "locale",
languages=[language],
)
@classmethod
def refresh_translations(cls) -> None:
"""
Load compiled translations.
"""
for language in cls.SUPPORTED_LOCALES:
rep = Path(__file__).parent / "locale" / language / "LC_MESSAGES"
rep.mkdir(parents=True) if not rep.is_dir() else None
if os.path.isfile(rep / "squirrelbattle.mo"):
cls.translators[language] = gt.translation(
"squirrelbattle",
localedir=Path(__file__).parent / "locale",
languages=[language],
)
@classmethod
def setlocale(cls, lang: str) -> None:
@ -40,10 +45,13 @@ class Translator:
@classmethod
def get_translator(cls) -> Any:
return cls.translators.get(cls.locale)
return cls.translators.get(cls.locale, gt.NullTranslations())
@classmethod
def makemessages(cls) -> None: # pragma: no cover
"""
Analyse all strings in the project and extract them.
"""
for language in cls.SUPPORTED_LOCALES:
file_name = Path(__file__).parent / "locale" / language \
/ "LC_MESSAGES" / "squirrelbattle.po"
@ -61,10 +69,13 @@ class Translator:
if file_name.is_file():
args.append("--join-existing")
print(f"Make {language} messages...")
subprocess.Popen(args, stdin=find.stdout)
subprocess.Popen(args, stdin=find.stdout).wait()
@classmethod
def compilemessages(cls) -> None: # pragma: no cover
def compilemessages(cls) -> None:
"""
Compile translation messages from source files.
"""
for language in cls.SUPPORTED_LOCALES:
args = ["msgfmt", "--check-format",
"-o", Path(__file__).parent / "locale" / language
@ -72,7 +83,7 @@ class Translator:
Path(__file__).parent / "locale" / language
/ "LC_MESSAGES" / "squirrelbattle.po"]
print(f"Compiling {language} messages...")
subprocess.Popen(args)
subprocess.Popen(args).wait()
def gettext(message: str) -> str:
@ -80,3 +91,6 @@ def gettext(message: str) -> str:
Translate a message.
"""
return Translator.get_translator().gettext(message)
Translator.refresh_translations()