Multi-language support for administration core functions

This commit is contained in:
Davte 2020-02-18 15:36:58 +01:00
parent 8d861757a1
commit 61cebd0c1f
4 changed files with 21 additions and 9 deletions

View File

@ -14,7 +14,7 @@ __author__ = "Davide Testa"
__email__ = "davide@davte.it" __email__ = "davide@davte.it"
__credits__ = ["Marco Origlia", "Nick Lee @Nickoala"] __credits__ = ["Marco Origlia", "Nick Lee @Nickoala"]
__license__ = "GNU General Public License v3.0" __license__ = "GNU General Public License v3.0"
__version__ = "2.4.4" __version__ = "2.4.5"
__maintainer__ = "Davide Testa" __maintainer__ = "Davide Testa"
__contact__ = "t.me/davte" __contact__ = "t.me/davte"

View File

@ -1070,8 +1070,11 @@ async def _errors_command(bot, update, user_record):
async def _maintenance_command(bot, update, user_record): async def _maintenance_command(bot, update, user_record):
maintenance_status = bot.change_maintenance_status(
maintenance_message = get_cleaned_text(update, bot, ['maintenance']) maintenance_message = get_cleaned_text(update, bot, ['maintenance'])
if maintenance_message.startswith('{'):
maintenance_message = json.loads(maintenance_message)
maintenance_status = bot.change_maintenance_status(
maintenance_message=maintenance_message
) )
if maintenance_status: if maintenance_status:
return bot.get_message( return bot.get_message(

View File

@ -476,7 +476,7 @@ class Bot(TelegramBot, ObjectWithDatabase, MultiLanguageObject):
message = self._unknown_command_message message = self._unknown_command_message
else: else:
message = self.__class__._unknown_command_message message = self.__class__._unknown_command_message
if message is not None: if isinstance(message, str):
message = message.format(bot=self) message = message.format(bot=self)
return message return message
@ -726,7 +726,7 @@ class Bot(TelegramBot, ObjectWithDatabase, MultiLanguageObject):
if command in self.commands: if command in self.commands:
replier = self.commands[command]['handler'] replier = self.commands[command]['handler']
elif 'chat' in update and update['chat']['id'] > 0: elif 'chat' in update and update['chat']['id'] > 0:
reply = self.unknown_command_message reply = dict(text=self.unknown_command_message)
else: # Handle command aliases and text parsers else: # Handle command aliases and text parsers
# Aliases are case insensitive: text and alias are both .lower() # Aliases are case insensitive: text and alias are both .lower()
for alias, function in self.command_aliases.items(): for alias, function in self.command_aliases.items():
@ -1086,7 +1086,8 @@ class Bot(TelegramBot, ObjectWithDatabase, MultiLanguageObject):
reply_markup=None, reply_markup=None,
update=None, update=None,
reply_to_update=False, reply_to_update=False,
send_default_keyboard=True): send_default_keyboard=True,
user_record=None):
"""Send text via message(s). """Send text via message(s).
This method wraps lower-level `TelegramBot.sendMessage` method. This method wraps lower-level `TelegramBot.sendMessage` method.
@ -1119,6 +1120,14 @@ class Bot(TelegramBot, ObjectWithDatabase, MultiLanguageObject):
if not text: if not text:
return return
parse_mode = str(parse_mode) parse_mode = str(parse_mode)
if isinstance(text, dict) and chat_id > 0:
if user_record is None :
user_record = self.db['users'].find_one(telegram_id=chat_id)
text = self.get_message(
update=update,
user_record=user_record,
messages=text
)
text_chunks = self.split_message_text( text_chunks = self.split_message_text(
text=text, text=text,
limit=self.__class__.TELEGRAM_MESSAGES_MAX_LEN - 100, limit=self.__class__.TELEGRAM_MESSAGES_MAX_LEN - 100,
@ -2026,7 +2035,7 @@ class Bot(TelegramBot, ObjectWithDatabase, MultiLanguageObject):
).parameters ).parameters
} }
) )
return self.authorization_denied_message return dict(text=self.authorization_denied_message)
self.commands[command] = dict( self.commands[command] = dict(
handler=decorated_command_handler, handler=decorated_command_handler,
description=description, description=description,
@ -2101,7 +2110,7 @@ class Bot(TelegramBot, ObjectWithDatabase, MultiLanguageObject):
if name in inspect.signature(parser).parameters if name in inspect.signature(parser).parameters
} }
) )
return bot.authorization_denied_message return dict(text=bot.authorization_denied_message)
self.text_message_parsers[condition] = dict( self.text_message_parsers[condition] = dict(
handler=decorated_parser, handler=decorated_parser,
description=description, description=description,

View File

@ -120,7 +120,7 @@ class MultiLanguageObject(object):
return language or self.default_language return language or self.default_language
def get_message(self, *fields, update=None, user_record=None, def get_message(self, *fields, update=None, user_record=None,
default_message=None, language=None, **format_kwargs): default_message=None, language=None, messages=None, **format_kwargs):
"""Given a list of strings (`fields`), return proper message. """Given a list of strings (`fields`), return proper message.
Language will be determined by `get_language` method. Language will be determined by `get_language` method.
@ -137,7 +137,7 @@ class MultiLanguageObject(object):
language=language language=language
) )
# Find result for `language` # Find result for `language`
result = self.messages result = messages or self.messages
for field in fields: for field in fields:
if field not in result: if field not in result:
logging.debug( logging.debug(