Clean the translation module

This commit is contained in:
Yohann D'ANELLO 2020-11-28 01:59:52 +01:00
parent 138b2c6d54
commit 7d02604407
4 changed files with 40 additions and 24 deletions

View File

@ -14,7 +14,7 @@ from .interfaces import Map, Logs
from .resources import ResourceManager from .resources import ResourceManager
from .settings import Settings from .settings import Settings
from . import menus from . import menus
from .translations import gettext as _, setlocale from .translations import gettext as _, Translator
from typing import Callable from typing import Callable
@ -35,7 +35,7 @@ class Game:
self.settings = Settings() self.settings = Settings()
self.settings.load_settings() self.settings.load_settings()
self.settings.write_settings() self.settings.write_settings()
setlocale(self.settings.LOCALE) Translator.setlocale(self.settings.LOCALE)
self.main_menu = menus.MainMenu() self.main_menu = menus.MainMenu()
self.settings_menu = menus.SettingsMenu() self.settings_menu = menus.SettingsMenu()
self.settings_menu.update_values(self.settings) self.settings_menu.update_values(self.settings)

View File

@ -7,7 +7,7 @@ from typing import Any, Optional
from .display.texturepack import TexturePack from .display.texturepack import TexturePack
from .enums import GameMode, KeyValues, DisplayActions from .enums import GameMode, KeyValues, DisplayActions
from .settings import Settings from .settings import Settings
from .translations import gettext as _, setlocale from .translations import gettext as _, Translator
class Menu: class Menu:
@ -99,7 +99,7 @@ class SettingsMenu(Menu):
elif option == "LOCALE": elif option == "LOCALE":
game.settings.LOCALE = 'fr' if game.settings.LOCALE == 'en'\ game.settings.LOCALE = 'fr' if game.settings.LOCALE == 'en'\
else 'en' else 'en'
setlocale(game.settings.LOCALE) Translator.setlocale(game.settings.LOCALE)
game.settings.write_settings() game.settings.write_settings()
self.update_values(game.settings) self.update_values(game.settings)
else: else:

View File

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

View File

@ -1,28 +1,44 @@
# 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 gettext import gettext as gt
from typing import Any, List
SUPPORTED_LOCALES = ["en", "fr"] class Translator:
DEFAULT_LOCALE = "en" """
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(
_TRANSLATORS = dict() "squirrelbattle",
for language in SUPPORTED_LOCALES:
_TRANSLATORS[language] = gettext.translation("squirrelbattle",
localedir="locale", localedir="locale",
languages=[language]) 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: def gettext(message: str) -> str:
return _TRANSLATORS.get(_current_locale, """
_TRANSLATORS.get("en")).gettext(message) Translate a message.
"""
return Translator.get_translator().gettext(message)
def setlocale(lang: str) -> None:
global _current_locale
lang = lang[:2]
if lang in SUPPORTED_LOCALES:
_current_locale = lang