Translate entities

This commit is contained in:
Yohann D'ANELLO
2020-11-27 22:33:58 +01:00
parent 8f85093eb8
commit 70ae60b9a4
5 changed files with 113 additions and 14 deletions

View File

@ -315,6 +315,10 @@ class Entity:
from squirrelbattle.entities.items import Item
return isinstance(self, Item)
@property
def translated_name(self) -> str:
return _(self.name.replace("_", " "))
@staticmethod
def get_all_entity_classes():
"""
@ -392,8 +396,9 @@ class FightingEntity(Entity):
Deals damage to the opponent, based on the stats
"""
return _("{name} hits {opponent}.")\
.format(name=self.name, opponent=opponent.name) + " "\
+ opponent.take_damage(self, self.strength)
.format(name=_(self.translated_name.capitalize()),
opponent=_(opponent.translated_name)) + " " + \
opponent.take_damage(self, self.strength)
def take_damage(self, attacker: "Entity", amount: int) -> str:
"""
@ -403,8 +408,9 @@ class FightingEntity(Entity):
if self.health <= 0:
self.die()
return _("{name} takes {amount} damage.")\
.format(name=self.name, amount=str(amount)) \
+ (" " + _("{name} dies.").format(name=self.name)
.format(name=self.translated_name.capitalize(), amount=str(amount))\
+ (" " + _("{name} dies.")
.format(name=self.translated_name.capitalize())
if self.health <= 0 else "")
def die(self) -> None:

View File

@ -7,9 +7,9 @@ class TestTranslations(unittest.TestCase):
def setUp(self) -> None:
setlocale("fr")
def test_translations(self) -> None:
def test_main_menu_translation(self) -> None:
"""
Ensure that some strings are well-translated.
Ensure that the main menu is translated.
"""
self.assertEqual(_("New game"), "Nouvelle partie")
self.assertEqual(_("Resume"), "Continuer")
@ -18,6 +18,10 @@ class TestTranslations(unittest.TestCase):
self.assertEqual(_("Settings"), "Paramètres")
self.assertEqual(_("Exit"), "Quitter")
def test_settings_menu_translation(self) -> None:
"""
Ensure that the settings menu is translated.
"""
self.assertEqual(_("Main key to move up"),
"Touche principale pour aller vers le haut")
self.assertEqual(_("Secondary key to move up"),
@ -38,3 +42,14 @@ class TestTranslations(unittest.TestCase):
"Touche pour valider un menu")
self.assertEqual(_("Texture pack"), "Pack de textures")
self.assertEqual(_("Language"), "Langue")
def test_entities_translation(self) -> None:
self.assertEqual(_("player"), "joueur")
self.assertEqual(_("tiger"), "tigre")
self.assertEqual(_("hedgehog"), "hérisson")
self.assertEqual(_("rabbit"), "lapin")
self.assertEqual(_("teddy bear"), "nounours")
self.assertEqual(_("bomb"), "bombe")
self.assertEqual(_("heart"), "cœur")