From 4287b4f045f91139b0c4d9c45965d2cab0adba60 Mon Sep 17 00:00:00 2001 From: Yohann D'ANELLO Date: Fri, 27 Nov 2020 20:53:24 +0100 Subject: [PATCH] Add possibility to change the language --- squirrelbattle/game.py | 3 ++- squirrelbattle/interfaces.py | 8 ++++---- squirrelbattle/menus.py | 8 +++++++- squirrelbattle/settings.py | 2 ++ squirrelbattle/translations.py | 17 +++++++++++++++-- 5 files changed, 30 insertions(+), 8 deletions(-) diff --git a/squirrelbattle/game.py b/squirrelbattle/game.py index a9689a5..be1e01a 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 _ +from .translations import gettext as _, setlocale from typing import Callable @@ -38,6 +38,7 @@ class Game: self.settings.load_settings() self.settings.write_settings() self.settings_menu.update_values(self.settings) + setlocale(self.settings.LOCALE) self.logs = Logs() self.message = None diff --git a/squirrelbattle/interfaces.py b/squirrelbattle/interfaces.py index 0e51589..845e3bd 100644 --- a/squirrelbattle/interfaces.py +++ b/squirrelbattle/interfaces.py @@ -129,7 +129,7 @@ class Map: """ Put randomly {count} hedgehogs on the map, where it is available. """ - for _ in range(count): + for ignored in range(count): y, x = 0, 0 while True: y, x = randint(0, self.height - 1), randint(0, self.width - 1) @@ -392,7 +392,7 @@ class FightingEntity(Entity): Deals damage to the opponent, based on the stats """ return _("{name} hits {opponent}.")\ - .format(name=str(self), opponent=str(opponent)) + " "\ + .format(name=self.name, opponent=opponent.name) + " "\ + opponent.take_damage(self, self.strength) def take_damage(self, attacker: "Entity", amount: int) -> str: @@ -403,8 +403,8 @@ class FightingEntity(Entity): if self.health <= 0: self.die() return _("{name} takes {amount} damage.")\ - .format(name=str(self), amount=str(amount)) \ - + (" " + "{name} dies.".format(name=str(self)) + .format(name=self.name, amount=str(amount)) \ + + (" " + "{name} dies.".format(name=self.name) if self.health <= 0 else "") def die(self) -> None: diff --git a/squirrelbattle/menus.py b/squirrelbattle/menus.py index 3c2d9e8..56a7db2 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 _ +from .translations import gettext as _, setlocale class Menu: @@ -96,6 +96,12 @@ class SettingsMenu(Menu): game.settings.TEXTURE_PACK) game.settings.write_settings() self.update_values(game.settings) + elif option == "LOCALE": + game.settings.LOCALE = 'fr' if game.settings.LOCALE == 'en'\ + else 'en' + setlocale(game.settings.LOCALE) + game.settings.write_settings() + self.update_values(game.settings) else: self.waiting_for_key = True self.update_values(game.settings) diff --git a/squirrelbattle/settings.py b/squirrelbattle/settings.py index 9601457..726d96c 100644 --- a/squirrelbattle/settings.py +++ b/squirrelbattle/settings.py @@ -2,6 +2,7 @@ # SPDX-License-Identifier: GPL-3.0-or-later import json +import locale import os from typing import Any, Generator @@ -35,6 +36,7 @@ class Settings: self.KEY_ENTER = \ ['\n', 'Touche pour valider un menu'] self.TEXTURE_PACK = ['ascii', 'Pack de textures utilisé'] + self.LOCALE = [locale.getlocale()[0][:2], 'Langue'] def __getattribute__(self, item: str) -> Any: superattribute = super().__getattribute__(item) diff --git a/squirrelbattle/translations.py b/squirrelbattle/translations.py index 27ec288..fa8196d 100644 --- a/squirrelbattle/translations.py +++ b/squirrelbattle/translations.py @@ -4,12 +4,25 @@ import gettext +SUPPORTED_LOCALES = ["en", "fr"] +DEFAULT_LOCALE = "en" + +_current_locale = DEFAULT_LOCALE + _TRANSLATORS = dict() -for language in ["en", "fr"]: +for language in SUPPORTED_LOCALES: _TRANSLATORS[language] = gettext.translation("squirrelbattle", localedir="locale", languages=[language]) def gettext(message: str) -> str: - return _TRANSLATORS.get("en", _TRANSLATORS.get("en")).gettext(message) + 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