Multilanguage refactoring

This commit is contained in:
Davte 2019-07-21 23:54:31 +02:00
parent 20c0717b81
commit 19cb2dca13
4 changed files with 83 additions and 19 deletions

View File

@ -3,6 +3,6 @@
__author__ = "Davide Testa" __author__ = "Davide Testa"
__email__ = "davide@davte.it" __email__ = "davide@davte.it"
__license__ = "GNU General Public License v3.0" __license__ = "GNU General Public License v3.0"
__version__ = "1.0.11" __version__ = "1.0.12"
__maintainer__ = "Davide Testa" __maintainer__ = "Davide Testa"
__contact__ = "t.me/davte" __contact__ = "t.me/davte"

View File

@ -54,6 +54,31 @@ supported_languages = {
} }
} }
ciclopibot_help_messages = {
'help_command': {
'text': {
'en': "<b>{bot.name} guide</b>\n\n"
"Welcome!\n"
"To visit a guide section, press the corresponding "
"button.\n"
"To view all available commands, see section "
"`Comandi`.\n\n"
"Bot author and administrator: @Davte",
'it': "<b>📖 Guida di {bot.name}\n\n</b>"
"Benvenuto!\n"
"Per leggere una sezione della guida premi il bottone "
"corrispondente. Per conoscere tutti i comandi "
"disponibili, visita l'apposita sezione della guida "
"premendo il pulsante Comandi.\n\n"
"Autore e amministratore del bot: @Davte"
},
'description': {
'en': "Help",
'it': "Aiuto"
}
}
}
if __name__ == '__main__': if __name__ == '__main__':
path = os.path.dirname(__file__) path = os.path.dirname(__file__)
@ -125,16 +150,12 @@ if __name__ == '__main__':
ciclopi.init(bot) ciclopi.init(bot)
helper.init( helper.init(
bot=bot, bot=bot,
help_message="<b>📖 Guida di {bot.name}\n\n</b>" help_messages=ciclopibot_help_messages,
"Benvenuto!\n"
"Per conoscere i comandi disponibili visita l'apposita "
"sezione della guida premendo il pulsante Comandi.\n\n"
"Autore e amministratore del bot: @davte",
help_sections_file='ciclopibot/data/help.json' help_sections_file='ciclopibot/data/help.json'
) )
authorization.init(bot) authorization.init(bot)
languages.init( languages.init(
bot, language='it', language_messages=language_messages, bot, language_messages=language_messages,
supported_languages=supported_languages supported_languages=supported_languages
) )
# Run bot(s) # Run bot(s)

View File

@ -15,6 +15,15 @@ from davtelepot.utilities import (
make_lines_of_buttons make_lines_of_buttons
) )
default_ciclopi_messages = {
'ciclopi_command': {
'description': {
'en': "CiloPi stations status",
'it': "Stato delle stazioni CicloPi"
}
}
}
_URL = "http://www.ciclopi.eu/frmLeStazioni.aspx" _URL = "http://www.ciclopi.eu/frmLeStazioni.aspx"
ciclopi_webpage = CachedPage.get( ciclopi_webpage = CachedPage.get(
@ -1452,7 +1461,7 @@ async def _ciclopi_button(bot, update, user_record):
return result return result
def init(bot): def init(bot, ciclopi_messages=None):
"""Take a bot and assign commands to it.""" """Take a bot and assign commands to it."""
with bot.db as db: with bot.db as db:
if 'ciclopi_stations' not in db.tables: if 'ciclopi_stations' not in db.tables:
@ -1481,9 +1490,14 @@ def init(bot):
) )
) )
if ciclopi_messages is None:
ciclopi_messages = default_ciclopi_messages
@bot.command(command='/ciclopi', aliases=["CicloPi 🚲", "🚲 CicloPi 🔴"], @bot.command(command='/ciclopi', aliases=["CicloPi 🚲", "🚲 CicloPi 🔴"],
show_in_keyboard=True, show_in_keyboard=True,
description="Stato delle stazioni CicloPi", description=(
ciclopi_messages['ciclopi_command']['description']
),
authorization_level='everybody') authorization_level='everybody')
async def ciclopi_command(bot, update, user_record): async def ciclopi_command(bot, update, user_record):
return await _ciclopi_command(bot, update, user_record) return await _ciclopi_command(bot, update, user_record)

View File

@ -6,10 +6,25 @@ from davtelepot.utilities import (
make_lines_of_buttons, make_button, MyOD make_lines_of_buttons, make_button, MyOD
) )
DENY_MESSAGE = ( default_help_messages = {
"Chiedi di essere autorizzato: se la tua richiesta verrà accolta, " 'help_command': {
"ripeti il comando /help per leggere il messaggio di aiuto." 'text': {
) 'en': "<b>Guide</b>",
'it': "<b>Guida</b>"
},
'description': {
'en': "Help",
'it': "Aiuto"
},
'access_denied_message': {
'en': "Ask for authorization. If your request is accepted, send "
"/help command again to read the guide.",
'it': "Chiedi di essere autorizzato: se la tua richiesta "
"verrà accolta, ripeti il comando /help per leggere "
"il messaggio di aiuto."
}
}
}
def get_command_description(bot, update, user_record): def get_command_description(bot, update, user_record):
@ -104,10 +119,17 @@ def get_help_buttons(bot, update, user_record):
async def _help_command(bot, update, user_record): async def _help_command(bot, update, user_record):
if not bot.authorization_function(update=update, if not bot.authorization_function(update=update,
authorization_level='everybody'): authorization_level='everybody'):
return DENY_MESSAGE return bot.get_message(
'help', 'help_command', 'access_denied_message',
update=update, user_record=user_record
)
reply_markup = get_help_buttons(bot, update, user_record) reply_markup = get_help_buttons(bot, update, user_record)
return dict( return dict(
text=bot.help_message.format(bot=bot), text=bot.get_message(
'help', 'help_command', 'text',
update=update, user_record=user_record,
bot=bot
),
parse_mode='HTML', parse_mode='HTML',
reply_markup=reply_markup, reply_markup=reply_markup,
disable_web_page_preview=True disable_web_page_preview=True
@ -125,7 +147,11 @@ async def _help_button(bot, update, user_record):
) )
rm = HELP_MENU_BUTTON rm = HELP_MENU_BUTTON
elif command == 'menu': elif command == 'menu':
text = bot.help_message.format(bot=bot) text = bot.get_message(
'help', 'help_command', 'text',
update=update, user_record=user_record,
bot=bot
)
rm = get_help_buttons(bot, update, user_record) rm = get_help_buttons(bot, update, user_record)
else: else:
for code, section in bot.help_sections.items(): for code, section in bot.help_sections.items():
@ -169,10 +195,12 @@ async def _start_command(bot, update, user_record):
return return
def init(bot, help_message="<b>Guida</b>", def init(bot, help_messages=None,
help_sections_file='data/help.json', help_buttons=[]): help_sections_file='data/help.json', help_buttons=[]):
"""Assign parsers, commands, buttons and queries to given `bot`.""" """Assign parsers, commands, buttons and queries to given `bot`."""
bot.help_message = help_message if help_messages is None:
help_messages = default_help_messages
bot.messages['help'] = help_messages
bot.help_buttons = help_buttons bot.help_buttons = help_buttons
bot.help_sections = MyOD() bot.help_sections = MyOD()
for code, section in enumerate( for code, section in enumerate(
@ -188,7 +216,8 @@ def init(bot, help_message="<b>Guida</b>",
return await _start_command(bot, update, user_record) return await _start_command(bot, update, user_record)
@bot.command(command='/help', aliases=['Guida 📖', '00help'], @bot.command(command='/help', aliases=['Guida 📖', '00help'],
show_in_keyboard=True, description="Aiuto", show_in_keyboard=True,
description=help_messages['help_command']['description'],
authorization_level='everybody') authorization_level='everybody')
async def help_command(bot, update, user_record): async def help_command(bot, update, user_record):
result = await _help_command(bot, update, user_record) result = await _help_command(bot, update, user_record)