Add possibility to change the language
This commit is contained in:
parent
2498fd2a61
commit
4287b4f045
@ -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
|
||||
|
||||
|
@ -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:
|
||||
|
@ -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)
|
||||
|
@ -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)
|
||||
|
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user