From 7d026044071ce214bb50d1648ca981fb4327a85e Mon Sep 17 00:00:00 2001 From: Yohann D'ANELLO Date: Sat, 28 Nov 2020 01:59:52 +0100 Subject: [PATCH] Clean the translation module --- squirrelbattle/game.py | 4 +- squirrelbattle/menus.py | 4 +- squirrelbattle/tests/translations_test.py | 4 +- squirrelbattle/translations.py | 52 +++++++++++++++-------- 4 files changed, 40 insertions(+), 24 deletions(-) diff --git a/squirrelbattle/game.py b/squirrelbattle/game.py index 09f1328..44ad349 100644 --- a/squirrelbattle/game.py +++ b/squirrelbattle/game.py @@ -14,7 +14,7 @@ from .interfaces import Map, Logs from .resources import ResourceManager from .settings import Settings from . import menus -from .translations import gettext as _, setlocale +from .translations import gettext as _, Translator from typing import Callable @@ -35,7 +35,7 @@ class Game: self.settings = Settings() self.settings.load_settings() self.settings.write_settings() - setlocale(self.settings.LOCALE) + Translator.setlocale(self.settings.LOCALE) self.main_menu = menus.MainMenu() self.settings_menu = menus.SettingsMenu() self.settings_menu.update_values(self.settings) diff --git a/squirrelbattle/menus.py b/squirrelbattle/menus.py index 3f6f7a0..1fba7ea 100644 --- a/squirrelbattle/menus.py +++ b/squirrelbattle/menus.py @@ -7,7 +7,7 @@ from typing import Any, Optional from .display.texturepack import TexturePack from .enums import GameMode, KeyValues, DisplayActions from .settings import Settings -from .translations import gettext as _, setlocale +from .translations import gettext as _, Translator class Menu: @@ -99,7 +99,7 @@ class SettingsMenu(Menu): elif option == "LOCALE": game.settings.LOCALE = 'fr' if game.settings.LOCALE == 'en'\ else 'en' - setlocale(game.settings.LOCALE) + Translator.setlocale(game.settings.LOCALE) game.settings.write_settings() self.update_values(game.settings) else: diff --git a/squirrelbattle/tests/translations_test.py b/squirrelbattle/tests/translations_test.py index 1931224..742edea 100644 --- a/squirrelbattle/tests/translations_test.py +++ b/squirrelbattle/tests/translations_test.py @@ -1,11 +1,11 @@ import unittest -from squirrelbattle.translations import gettext as _, setlocale +from squirrelbattle.translations import gettext as _, Translator class TestTranslations(unittest.TestCase): def setUp(self) -> None: - setlocale("fr") + Translator.setlocale("fr") def test_main_menu_translation(self) -> None: """ diff --git a/squirrelbattle/translations.py b/squirrelbattle/translations.py index fa8196d..47ec9fb 100644 --- a/squirrelbattle/translations.py +++ b/squirrelbattle/translations.py @@ -1,28 +1,44 @@ # Copyright (C) 2020 by ÿnérant, eichhornchen, nicomarg, charlse # SPDX-License-Identifier: GPL-3.0-or-later -import gettext +import gettext as gt +from typing import Any, List -SUPPORTED_LOCALES = ["en", "fr"] -DEFAULT_LOCALE = "en" +class Translator: + """ + This module uses gettext to translate strings. + Translator.setlocale defines the language of the strings, + then gettext() translates the message. + """ + SUPPORTED_LOCALES: List[str] = ["en", "fr"] + locale: str = "en" + translators: dict = {} -_current_locale = DEFAULT_LOCALE + for language in SUPPORTED_LOCALES: + translators[language] = gt.translation( + "squirrelbattle", + localedir="locale", + languages=[language], + ) -_TRANSLATORS = dict() -for language in SUPPORTED_LOCALES: - _TRANSLATORS[language] = gettext.translation("squirrelbattle", - localedir="locale", - languages=[language]) + @classmethod + def setlocale(cls, lang: str) -> None: + """ + Define the language used to translate the game. + The language must be supported, otherwise nothing is done. + """ + lang = lang[:2] + if lang in cls.SUPPORTED_LOCALES: + cls.locale = lang + + @classmethod + def get_translator(cls) -> Any: + return cls.translators.get(cls.locale) def gettext(message: str) -> str: - return _TRANSLATORS.get(_current_locale, - _TRANSLATORS.get("en")).gettext(message) - - -def setlocale(lang: str) -> None: - global _current_locale - lang = lang[:2] - if lang in SUPPORTED_LOCALES: - _current_locale = lang + """ + Translate a message. + """ + return Translator.get_translator().gettext(message)