Skip to content

Allow users to programmatically override default i18n strings #91

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Oct 9, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion botogram/bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ def __init__(self, api_connection):

self._lang = ""
self._lang_inst = None
self.override_i18n = {}

# Support for the old, deprecated bot.hide_commands
self._hide_commands = []
Expand Down Expand Up @@ -273,7 +274,7 @@ def freeze(self):
self._commands, chains, self._scheduler,
self._main_component._component_id,
self._bot_id, self._shared_memory,
self._update_processors)
self._update_processors, self.override_i18n)

@property
def lang(self):
Expand Down
12 changes: 9 additions & 3 deletions botogram/frozenbot.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def __init__(self, api, about, owner, hide_commands, before_help,
after_help, link_preview_in_help,
validate_callback_signatures, process_backlog, lang, itself,
commands_re, commands, chains, scheduler, main_component_id,
bot_id, shared_memory, update_processors):
bot_id, shared_memory, update_processors, override_i18n):
# This attribute should be added with the default setattr, because is
# needed by the custom setattr
object.__setattr__(self, "_frozen", False)
Expand All @@ -61,6 +61,7 @@ def __init__(self, api, about, owner, hide_commands, before_help,
self._update_processors = update_processors
self._commands = {name: command.for_bot(self)
for name, command in commands.items()}
self.override_i18n = override_i18n

# Setup the logger
self.logger = logbook.Logger('botogram bot')
Expand All @@ -82,7 +83,7 @@ def __reduce__(self):
self.validate_callback_signatures, self.process_backlog, self.lang,
self.itself, self._commands_re, self._commands, self._chains,
self._scheduler, self._main_component_id, self._bot_id,
self._shared_memory, self._update_processors,
self._shared_memory, self._update_processors, self.override_i18n,
)
return restore, args

Expand Down Expand Up @@ -240,7 +241,12 @@ def register_update_processor(self, kind, processor):

def _(self, message, **args):
"""Translate a string"""
return self._lang_inst.gettext(message) % args
# Check if the message has been overridden
if message in self.override_i18n:
return self.override_i18n[message] % args
# Otherwise try to return the original message
else:
return self._lang_inst.gettext(message) % args

# And some internal methods used by botogram

Expand Down
7 changes: 7 additions & 0 deletions docs/api/bot.rst
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,13 @@ components.
The :py:class:`botogram.User` representation of the bot's user account.
From this you can access its id, username and more.

.. py:attribute:: override_i18n

A dictionary that allows to override default i18n messages by associating
the default ``msgid`` string of a message with its alternative.

.. versionadded:: 0.4.1
Copy link
Member

@paolobarbolini paolobarbolini Oct 9, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since it's a new feature it probably will be added in 0.5

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, but I'll tweak the changelog myself after merging, don't worry.


.. py:decoratormethod:: before_processing

Functions decorated with this decorator will be called before an update
Expand Down
7 changes: 7 additions & 0 deletions docs/changelog/0.4.rst
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,13 @@ botogram 0.4.1

Release description not yet written.

New features
------------

* Added ability to override default i18n messages

* New attribute :py:attr:`botogram.Bot.override_i18n`

Bug fixes
---------

Expand Down
39 changes: 20 additions & 19 deletions i18n/botogram.pot
Original file line number Diff line number Diff line change
@@ -1,70 +1,71 @@
# Translations template for botogram.
# Copyright (C) 2016 Pietro Albini <pietro@pietroalbini.io>
# Copyright (C) 2017 Pietro Albini <pietro@pietroalbini.io>
# This file is distributed under the same license as the botogram project.
# FIRST AUTHOR <EMAIL@ADDRESS>, 2016.
# FIRST AUTHOR <EMAIL@ADDRESS>, 2017.
#
#, fuzzy
msgid ""
msgstr ""
"Project-Id-Version: botogram 1.0.dev0\n"
"Report-Msgid-Bugs-To: https://github.com/pietroalbini/botogram/issues\n"
"POT-Creation-Date: 2016-03-30 22:31+0200\n"
"POT-Creation-Date: 2017-10-06 19:21+0200\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <LL@li.org>\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=utf-8\n"
"Content-Transfer-Encoding: 8bit\n"
"Generated-By: Babel 2.1.1\n"
"Generated-By: Babel 2.3.4\n"

#: botogram/defaults.py:32
#: botogram/defaults.py:46
msgid "Use /help to get a list of all the commands."
msgstr ""

#: botogram/defaults.py:39
#: botogram/defaults.py:53
msgid "Start using the bot."
msgstr ""

#: botogram/defaults.py:40
#: botogram/defaults.py:54
msgid "This shows a greeting message."
msgstr ""

#: botogram/defaults.py:48
#: botogram/defaults.py:62
msgid "<b>Error!</b> The <code>/help</code> command allows up to one argument."
msgstr ""

#: botogram/defaults.py:54 botogram/defaults.py:153
#: botogram/defaults.py:68 botogram/defaults.py:167
#, python-format
msgid "<b>Unknown command:</b> <code>/%(name)s</code>"
msgstr ""

#: botogram/defaults.py:57 botogram/defaults.py:155
#: botogram/defaults.py:71 botogram/defaults.py:169
msgid "Use /help to get a list of the commands."
msgstr ""

#: botogram/defaults.py:78
#: botogram/defaults.py:93
msgid "<b>This bot supports those commands:</b>"
msgstr ""

#: botogram/defaults.py:89 botogram/defaults.py:130
#: botogram/defaults.py:97 botogram/defaults.py:127
msgid "No description available."
msgstr ""

#: botogram/defaults.py:101 botogram/defaults.py:144
msgid ""
"You can also use <code>/help &lt;command&gt;</code> to get help about a "
"specific command."
msgstr ""

#: botogram/defaults.py:93
#: botogram/defaults.py:105
msgid "<i>This bot has no commands.</i>"
msgstr ""

#: botogram/defaults.py:102 botogram/defaults.py:119
#: botogram/defaults.py:114 botogram/defaults.py:133
#, python-format
msgid "Please contact %(owner)s if you have problems with this bot."
msgstr ""

#: botogram/defaults.py:129
#: botogram/defaults.py:143
msgid "Show this help message."
msgstr ""

#: botogram/utils.py:144
msgid "No description available."
msgstr ""

33 changes: 17 additions & 16 deletions i18n/langs/en.po
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ msgid ""
msgstr ""
"Project-Id-Version: botogram 1.0.dev0\n"
"Report-Msgid-Bugs-To: https://github.com/pietroalbini/botogram/issues\n"
"POT-Creation-Date: 2016-03-30 22:31+0200\n"
"POT-Creation-Date: 2017-10-06 19:21+0200\n"
"PO-Revision-Date: 2015-08-01 17:20+0200\n"
"Last-Translator: Pietro Albini <pietro@pietroalbini.io>\n"
"Language: en\n"
Expand All @@ -16,56 +16,57 @@ msgstr ""
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=utf-8\n"
"Content-Transfer-Encoding: 8bit\n"
"Generated-By: Babel 2.1.1\n"
"Generated-By: Babel 2.3.4\n"

#: botogram/defaults.py:32
#: botogram/defaults.py:46
msgid "Use /help to get a list of all the commands."
msgstr ""

#: botogram/defaults.py:39
#: botogram/defaults.py:53
msgid "Start using the bot."
msgstr ""

#: botogram/defaults.py:40
#: botogram/defaults.py:54
msgid "This shows a greeting message."
msgstr ""

#: botogram/defaults.py:48
#: botogram/defaults.py:62
msgid "<b>Error!</b> The <code>/help</code> command allows up to one argument."
msgstr ""

#: botogram/defaults.py:54 botogram/defaults.py:153
#: botogram/defaults.py:68 botogram/defaults.py:167
#, python-format
msgid "<b>Unknown command:</b> <code>/%(name)s</code>"
msgstr ""

#: botogram/defaults.py:57 botogram/defaults.py:155
#: botogram/defaults.py:71 botogram/defaults.py:169
msgid "Use /help to get a list of the commands."
msgstr ""

#: botogram/defaults.py:78
#: botogram/defaults.py:93
msgid "<b>This bot supports those commands:</b>"
msgstr ""

#: botogram/defaults.py:89 botogram/defaults.py:130
#: botogram/defaults.py:97 botogram/defaults.py:127
msgid "No description available."
msgstr ""

#: botogram/defaults.py:101 botogram/defaults.py:144
msgid ""
"You can also use <code>/help &lt;command&gt;</code> to get help about a "
"specific command."
msgstr ""

#: botogram/defaults.py:93
#: botogram/defaults.py:105
msgid "<i>This bot has no commands.</i>"
msgstr ""

#: botogram/defaults.py:102 botogram/defaults.py:119
#: botogram/defaults.py:114 botogram/defaults.py:133
#, python-format
msgid "Please contact %(owner)s if you have problems with this bot."
msgstr ""

#: botogram/defaults.py:129
#: botogram/defaults.py:143
msgid "Show this help message."
msgstr ""

#: botogram/utils.py:144
msgid "No description available."
msgstr ""
42 changes: 23 additions & 19 deletions i18n/langs/it.po
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ msgid ""
msgstr ""
"Project-Id-Version: botogram 1.0.dev0\n"
"Report-Msgid-Bugs-To: https://github.com/pietroalbini/botogram/issues\n"
"POT-Creation-Date: 2016-03-30 22:31+0200\n"
"POT-Creation-Date: 2017-10-06 19:21+0200\n"
"PO-Revision-Date: 2015-08-01 17:06+0200\n"
"Last-Translator: Pietro Albini <pietro@pietroalbini.io>\n"
"Language: it\n"
Expand All @@ -16,57 +16,61 @@ msgstr ""
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=utf-8\n"
"Content-Transfer-Encoding: 8bit\n"
"Generated-By: Babel 2.1.1\n"
"Generated-By: Babel 2.3.4\n"

#: botogram/defaults.py:32
#: botogram/defaults.py:46
msgid "Use /help to get a list of all the commands."
msgstr "Utilizza /help per ottenere la lista di tutti i comandi."

#: botogram/defaults.py:39
#: botogram/defaults.py:53
msgid "Start using the bot."
msgstr "Inizia ad usare il bot."

#: botogram/defaults.py:40
#: botogram/defaults.py:54
msgid "This shows a greeting message."
msgstr "Questo comando visualizza un messaggio di benvenuto."

#: botogram/defaults.py:48
#: botogram/defaults.py:62
msgid "<b>Error!</b> The <code>/help</code> command allows up to one argument."
msgstr "<b>Errore!</b> Il comando <code>/help</code> non accetta più di un argomento."
msgstr ""
"<b>Errore!</b> Il comando <code>/help</code> non accetta più di un "
"argomento."

#: botogram/defaults.py:54 botogram/defaults.py:153
#: botogram/defaults.py:68 botogram/defaults.py:167
#, python-format
msgid "<b>Unknown command:</b> <code>/%(name)s</code>"
msgstr "<b>Comando sconosciuto:</b> <code>/%(name)s</code>"

#: botogram/defaults.py:57 botogram/defaults.py:155
#: botogram/defaults.py:71 botogram/defaults.py:169
msgid "Use /help to get a list of the commands."
msgstr "Utilizza /help per ottenere la lista dei comandi."

#: botogram/defaults.py:78
#: botogram/defaults.py:93
msgid "<b>This bot supports those commands:</b>"
msgstr "<b>Questo bot supporta i seguenti comandi:</b>"

#: botogram/defaults.py:89 botogram/defaults.py:130
#: botogram/defaults.py:97 botogram/defaults.py:127
msgid "No description available."
msgstr "Nessuna descrizione disponibile."

#: botogram/defaults.py:101 botogram/defaults.py:144
msgid ""
"You can also use <code>/help &lt;command&gt;</code> to get help about a "
"specific command."
msgstr "Puoi anche usare <code>/help &lt;comando&gt;</code> per ottenere aiuto su un comando."
msgstr ""
"Puoi anche usare <code>/help &lt;comando&gt;</code> per ottenere aiuto su"
" un comando."

#: botogram/defaults.py:93
#: botogram/defaults.py:105
msgid "<i>This bot has no commands.</i>"
msgstr "<i>Questo bot non ha comandi.</i>"

#: botogram/defaults.py:102 botogram/defaults.py:119
#: botogram/defaults.py:114 botogram/defaults.py:133
#, python-format
msgid "Please contact %(owner)s if you have problems with this bot."
msgstr "Contatta %(owner)s se hai problemi con questo bot."

#: botogram/defaults.py:129
#: botogram/defaults.py:143
msgid "Show this help message."
msgstr "Visualizza questo messaggio di aiuto."

#: botogram/utils.py:144
msgid "No description available."
msgstr "Nessuna descrizione disponibile."

21 changes: 20 additions & 1 deletion tests/test_bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

import botogram.bot
import botogram.components
import botogram.utils

import conftest

Expand All @@ -30,7 +31,7 @@ def test_bot_creation(api, mock_req):
# Mock the getMe request
mock_req({
"getMe": {"ok": True, "result": {"id": 1, "first_name": "test",
"username": "test_bot"}},
"username": "test_bot"}},
})

bot1 = botogram.bot.create(conftest.API_KEY)
Expand Down Expand Up @@ -76,3 +77,21 @@ def test_bot_freeze(bot):
frozen = bot.freeze()

assert bot == frozen


def test_i18n_override(bot):
default_message = botogram.utils.get_language("en") \
.gettext("Use /help to get a list of all the commands.")
override_message = "git gud"

bot.override_i18n = {
default_message: override_message
}

assert bot._("Use /help to get a list of all the commands.") \
== override_message

bot.override_i18n = {}

assert bot._("Use /help to get a list of all the commands.") \
== default_message