Clean the translation module
This commit is contained in:
parent
138b2c6d54
commit
7d02604407
|
@ -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)
|
||||
|
|
|
@ -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:
|
||||
|
|
|
@ -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:
|
||||
"""
|
||||
|
|
|
@ -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
|
||||
|
||||
_TRANSLATORS = dict()
|
||||
for language in SUPPORTED_LOCALES:
|
||||
_TRANSLATORS[language] = gettext.translation("squirrelbattle",
|
||||
translators[language] = gt.translation(
|
||||
"squirrelbattle",
|
||||
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:
|
||||
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)
|
||||
|
|
Loading…
Reference in New Issue