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

@ -7,6 +7,7 @@ py37:
stage: test stage: test
image: python:3.7-alpine image: python:3.7-alpine
before_script: before_script:
- apk add --no-cache gettext
- pip install tox - pip install tox
script: tox -e py3 script: tox -e py3
@ -14,6 +15,7 @@ py38:
stage: test stage: test
image: python:3.8-alpine image: python:3.8-alpine
before_script: before_script:
- apk add --no-cache gettext
- pip install tox - pip install tox
script: tox -e py3 script: tox -e py3
@ -22,6 +24,7 @@ py39:
stage: test stage: test
image: python:3.9-alpine image: python:3.9-alpine
before_script: before_script:
- apk add --no-cache gettext
- pip install tox - pip install tox
script: tox -e py3 script: tox -e py3
@ -29,6 +32,7 @@ linters:
stage: quality-assurance stage: quality-assurance
image: python:3-alpine image: python:3-alpine
before_script: before_script:
- apk add --no-cache gettext
- pip install tox - pip install tox
script: tox -e linters script: tox -e linters
allow_failure: true allow_failure: true
@ -37,7 +41,7 @@ build-deb:
image: debian:buster-slim image: debian:buster-slim
stage: build stage: build
before_script: before_script:
- apt-get update && apt-get -y --no-install-recommends install build-essential debmake dh-python debhelper python3-all python3-setuptools - apt-get update && apt-get -y --no-install-recommends install build-essential debmake dh-python debhelper gettext python3-all python3-setuptools
script: script:
- dpkg-buildpackage - dpkg-buildpackage
- mkdir build && cp ../*.deb build/ - mkdir build && cp ../*.deb build/

View File

@ -4,18 +4,16 @@
import os import os
import unittest import unittest
from squirrelbattle.resources import ResourceManager from ..bootstrap import Bootstrap
from ..display.display import Display
from squirrelbattle.enums import DisplayActions from ..display.display_manager import DisplayManager
from ..entities.player import Player
from squirrelbattle.bootstrap import Bootstrap from ..enums import DisplayActions
from squirrelbattle.display.display import Display from ..game import Game, KeyValues, GameMode
from squirrelbattle.display.display_manager import DisplayManager from ..menus import MainMenuValues
from squirrelbattle.entities.player import Player from ..resources import ResourceManager
from squirrelbattle.game import Game, KeyValues, GameMode from ..settings import Settings
from squirrelbattle.menus import MainMenuValues from ..translations import gettext as _, Translator
from squirrelbattle.settings import Settings
from squirrelbattle.translations import gettext as _
class TestGame(unittest.TestCase): class TestGame(unittest.TestCase):
@ -277,6 +275,8 @@ class TestGame(unittest.TestCase):
self.assertEqual(self.game.settings.TEXTURE_PACK, "ascii") self.assertEqual(self.game.settings.TEXTURE_PACK, "ascii")
# Change language # Change language
Translator.compilemessages()
Translator.refresh_translations()
self.game.settings.LOCALE = "en" self.game.settings.LOCALE = "en"
self.game.handle_key_pressed(KeyValues.DOWN) self.game.handle_key_pressed(KeyValues.DOWN)
self.game.handle_key_pressed(KeyValues.ENTER) self.game.handle_key_pressed(KeyValues.ENTER)

View File

@ -5,6 +5,8 @@ from squirrelbattle.translations import gettext as _, Translator
class TestTranslations(unittest.TestCase): class TestTranslations(unittest.TestCase):
def setUp(self) -> None: def setUp(self) -> None:
Translator.compilemessages()
Translator.refresh_translations()
Translator.setlocale("fr") Translator.setlocale("fr")
def test_main_menu_translation(self) -> None: def test_main_menu_translation(self) -> None:

View File

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