Queer European MD passionate about IT
Browse Source

Multilanguage refactoring

Davte 4 years ago
parent
commit
19cb2dca13
4 changed files with 83 additions and 19 deletions
  1. 1 1
      ciclopibot/__init__.py
  2. 27 6
      ciclopibot/bot.py
  3. 16 2
      ciclopibot/ciclopi.py
  4. 39 10
      ciclopibot/helper.py

+ 1 - 1
ciclopibot/__init__.py

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

+ 27 - 6
ciclopibot/bot.py

@@ -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__':
     path = os.path.dirname(__file__)
@@ -125,16 +150,12 @@ if __name__ == '__main__':
     ciclopi.init(bot)
     helper.init(
         bot=bot,
-        help_message="<b>📖 Guida di {bot.name}\n\n</b>"
-                     "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_messages=ciclopibot_help_messages,
         help_sections_file='ciclopibot/data/help.json'
     )
     authorization.init(bot)
     languages.init(
-        bot, language='it', language_messages=language_messages,
+        bot, language_messages=language_messages,
         supported_languages=supported_languages
     )
     # Run bot(s)

+ 16 - 2
ciclopibot/ciclopi.py

@@ -15,6 +15,15 @@ from davtelepot.utilities import (
     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"
 
 ciclopi_webpage = CachedPage.get(
@@ -1452,7 +1461,7 @@ async def _ciclopi_button(bot, update, user_record):
     return result
 
 
-def init(bot):
+def init(bot, ciclopi_messages=None):
     """Take a bot and assign commands to it."""
     with bot.db as db:
         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 🔴"],
                  show_in_keyboard=True,
-                 description="Stato delle stazioni CicloPi",
+                 description=(
+                    ciclopi_messages['ciclopi_command']['description']
+                 ),
                  authorization_level='everybody')
     async def ciclopi_command(bot, update, user_record):
         return await _ciclopi_command(bot, update, user_record)

+ 39 - 10
ciclopibot/helper.py

@@ -6,10 +6,25 @@ from davtelepot.utilities import (
     make_lines_of_buttons, make_button, MyOD
 )
 
-DENY_MESSAGE = (
-    "Chiedi di essere autorizzato: se la tua richiesta verrà accolta, "
-    "ripeti il comando /help per leggere il messaggio di aiuto."
-)
+default_help_messages = {
+    'help_command': {
+        '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):
@@ -104,10 +119,17 @@ def get_help_buttons(bot, update, user_record):
 async def _help_command(bot, update, user_record):
     if not bot.authorization_function(update=update,
                                       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)
     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',
         reply_markup=reply_markup,
         disable_web_page_preview=True
@@ -125,7 +147,11 @@ async def _help_button(bot, update, user_record):
         )
         rm = HELP_MENU_BUTTON
     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)
     else:
         for code, section in bot.help_sections.items():
@@ -169,10 +195,12 @@ async def _start_command(bot, update, user_record):
     return
 
 
-def init(bot, help_message="<b>Guida</b>",
+def init(bot, help_messages=None,
          help_sections_file='data/help.json', help_buttons=[]):
     """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_sections = MyOD()
     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)
 
     @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')
     async def help_command(bot, update, user_record):
         result = await _help_command(bot, update, user_record)