Add possibility to change the language

This commit is contained in:
Yohann D'ANELLO 2020-11-27 20:53:24 +01:00
parent 2498fd2a61
commit 4287b4f045
5 changed files with 30 additions and 8 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 _ from .translations import gettext as _, setlocale
from typing import Callable from typing import Callable
@ -38,6 +38,7 @@ class Game:
self.settings.load_settings() self.settings.load_settings()
self.settings.write_settings() self.settings.write_settings()
self.settings_menu.update_values(self.settings) self.settings_menu.update_values(self.settings)
setlocale(self.settings.LOCALE)
self.logs = Logs() self.logs = Logs()
self.message = None self.message = None

View File

@ -129,7 +129,7 @@ class Map:
""" """
Put randomly {count} hedgehogs on the map, where it is available. Put randomly {count} hedgehogs on the map, where it is available.
""" """
for _ in range(count): for ignored in range(count):
y, x = 0, 0 y, x = 0, 0
while True: while True:
y, x = randint(0, self.height - 1), randint(0, self.width - 1) 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 Deals damage to the opponent, based on the stats
""" """
return _("{name} hits {opponent}.")\ return _("{name} hits {opponent}.")\
.format(name=str(self), opponent=str(opponent)) + " "\ .format(name=self.name, opponent=opponent.name) + " "\
+ opponent.take_damage(self, self.strength) + opponent.take_damage(self, self.strength)
def take_damage(self, attacker: "Entity", amount: int) -> str: def take_damage(self, attacker: "Entity", amount: int) -> str:
@ -403,8 +403,8 @@ class FightingEntity(Entity):
if self.health <= 0: if self.health <= 0:
self.die() self.die()
return _("{name} takes {amount} damage.")\ return _("{name} takes {amount} damage.")\
.format(name=str(self), amount=str(amount)) \ .format(name=self.name, amount=str(amount)) \
+ (" " + "{name} dies.".format(name=str(self)) + (" " + "{name} dies.".format(name=self.name)
if self.health <= 0 else "") if self.health <= 0 else "")
def die(self) -> None: def die(self) -> None:

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 _ from .translations import gettext as _, setlocale
class Menu: class Menu:
@ -96,6 +96,12 @@ class SettingsMenu(Menu):
game.settings.TEXTURE_PACK) game.settings.TEXTURE_PACK)
game.settings.write_settings() game.settings.write_settings()
self.update_values(game.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: else:
self.waiting_for_key = True self.waiting_for_key = True
self.update_values(game.settings) self.update_values(game.settings)

View File

@ -2,6 +2,7 @@
# SPDX-License-Identifier: GPL-3.0-or-later # SPDX-License-Identifier: GPL-3.0-or-later
import json import json
import locale
import os import os
from typing import Any, Generator from typing import Any, Generator
@ -35,6 +36,7 @@ class Settings:
self.KEY_ENTER = \ self.KEY_ENTER = \
['\n', 'Touche pour valider un menu'] ['\n', 'Touche pour valider un menu']
self.TEXTURE_PACK = ['ascii', 'Pack de textures utilisé'] self.TEXTURE_PACK = ['ascii', 'Pack de textures utilisé']
self.LOCALE = [locale.getlocale()[0][:2], 'Langue']
def __getattribute__(self, item: str) -> Any: def __getattribute__(self, item: str) -> Any:
superattribute = super().__getattribute__(item) superattribute = super().__getattribute__(item)

View File

@ -4,12 +4,25 @@
import gettext import gettext
SUPPORTED_LOCALES = ["en", "fr"]
DEFAULT_LOCALE = "en"
_current_locale = DEFAULT_LOCALE
_TRANSLATORS = dict() _TRANSLATORS = dict()
for language in ["en", "fr"]: for language in SUPPORTED_LOCALES:
_TRANSLATORS[language] = gettext.translation("squirrelbattle", _TRANSLATORS[language] = gettext.translation("squirrelbattle",
localedir="locale", localedir="locale",
languages=[language]) languages=[language])
def gettext(message: str) -> str: 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