3232from messages import SayText2
3333from messages import TextMsg
3434from messages import HudDestination
35+ # Translations
36+ from translations .strings import TranslationStrings
3537
3638
3739# =============================================================================
8284# =============================================================================
8385# >> EXCEPTIONS
8486# =============================================================================
85- # TODO:
86- # We probably need to update these exceptions if we want to add translations.
8787class ValidationError (Exception ):
88- def __init__ (self , message = '' ):
88+ def __init__ (self , message = '' , language = None , ** tokens ):
8989 self .message = message
90+ self .language = language
91+ self .tokens = tokens
9092
9193class ArgumentError (ValidationError ): pass
9294class ArgumentNumberMismatch (ArgumentError ): pass
@@ -488,13 +490,17 @@ def __init__(self, command, typed_command_cls, index=None, team_only=None):
488490 self .index = index
489491 self .team_only = team_only
490492
491- def reply (self , msg ):
493+ def reply (self , msg , language = None , ** tokens ):
492494 """Reply to the command issuer.
493495
494- :param str msg:
496+ :param str/TranslationStrings msg:
495497 Message to send.
498+ :param str language:
499+ Language to be used.
500+ :param tokens:
501+ Translation tokens for message.
496502 """
497- self .typed_command_cls .send_message (self , msg )
503+ self .typed_command_cls .send_message (self , msg , language , ** tokens )
498504
499505 def is_private_command (self ):
500506 """Return ``True`` if it's a private command.
@@ -582,13 +588,12 @@ def on_command(cls, command, *args):
582588
583589 Parse the command, clean its arguments and execute the callback.
584590 """
585- # TODO: Translations!
586591 info = CommandInfo (command , cls , * args )
587592 try :
588593 command_node , args = cls .parser .parse_command (info .command )
589594 result = cls .on_clean_command (info , command_node , args )
590595 except ValidationError as e :
591- info .reply (e .message )
596+ info .reply (e .message , e . language , ** e . tokens )
592597 else :
593598 if result is None :
594599 return info .auto_command_return
@@ -623,7 +628,7 @@ def manager(self):
623628 raise NotImplementedError ('Needs to be implemented by a sub class.' )
624629
625630 @staticmethod
626- def send_message (command_info , message ):
631+ def send_message (command_info , message , language = None , ** tokens ):
627632 """Send a message."""
628633 raise NotImplementedError ('Needs to be implemented by a sub class.' )
629634
@@ -643,7 +648,11 @@ class TypedServerCommand(_TypedCommand):
643648 manager = server_command_manager
644649
645650 @staticmethod
646- def send_message (command_info , message ):
651+ def send_message (command_info , message , language = None , ** tokens ):
652+ # Translate the message if it's a :class:`TranslationStrings` object.
653+ if isinstance (message , TranslationStrings ):
654+ message = message .get_string (language , ** tokens )
655+
647656 logger .log_message (message )
648657
649658 @classmethod
@@ -684,8 +693,8 @@ class TypedClientCommand(_TypedPlayerCommand):
684693 manager = client_command_manager
685694
686695 @staticmethod
687- def send_message (command_info , message ):
688- TextMsg (message , HudDestination .CONSOLE ).send (command_info .index )
696+ def send_message (command_info , message , language = None , ** tokens ):
697+ TextMsg (message , HudDestination .CONSOLE ).send (command_info .index , ** tokens )
689698
690699 @classmethod
691700 def get_auto_command_return (cls , info ):
@@ -699,8 +708,8 @@ class TypedSayCommand(_TypedPlayerCommand):
699708 manager = say_command_manager
700709
701710 @staticmethod
702- def send_message (command_info , message ):
703- SayText2 (message ).send (command_info .index )
711+ def send_message (command_info , message , language = None , ** tokens ):
712+ SayText2 (message ).send (command_info .index , ** tokens )
704713
705714 @classmethod
706715 def get_auto_command_return (cls , info ):
0 commit comments