Skip to content

Commit 7c41373

Browse files
committed
Add narrative documentation on botogram i18n
1 parent 564893d commit 7c41373

File tree

3 files changed

+188
-0
lines changed

3 files changed

+188
-0
lines changed

docs/api/bot.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,10 @@ components.
8383
The :py:class:`botogram.User` representation of the bot's user account.
8484
From this you can access its id, username and more.
8585

86+
.. py:attribute:: lang
87+
88+
The `ISO 639-1 code`_ assigned the language used by the bot.
89+
8690
.. py:attribute:: override_i18n
8791
8892
A dictionary that allows to override default i18n messages by associating
@@ -797,3 +801,5 @@ components.
797801
available for use or detailed help.
798802

799803
.. deprecated:: 0.3 it will be removed in botogram 1.0
804+
805+
.. _`ISO 639-1 code`: https://en.wikipedia.org/wiki/List_of_ISO_639-1_codes

docs/i18n.rst

Lines changed: 181 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,181 @@
1+
.. Copyright (c) 2015-2017 The Botogram Authors (see AUTHORS)
2+
Documentation released under the MIT license (see LICENSE)
3+
4+
.. _i18n:
5+
6+
======================
7+
Working with languages
8+
======================
9+
10+
Delivering content in a way that users can understand is vital in providing a
11+
good UX. Since a bot could focus on an audience that speaks a particular
12+
language, botogram provides an i18n platform that allows you to translate its
13+
default messages, such as ``/help``'s one, to another language.
14+
15+
.. _i18n-available-translations:
16+
17+
Available translations
18+
======================
19+
20+
The following languages are currently included in the botogram package:
21+
22+
* ``en`` (English)
23+
* ``it`` (Italian)
24+
25+
.. _i18n-setting-language:
26+
27+
Setting your bot's language
28+
===========================
29+
30+
Botogram bots currently only support using a single language for their messages.
31+
This also means that it is not possible at the moment to translate messages on
32+
a per-user basis.
33+
34+
While bots will use the English translation by default, it is possible to change
35+
the language in use by changing the bot's :py:attr:`botogram.Bot.lang` property
36+
to the target language's code. Please note that the selected language must be
37+
:ref:`supported <i18n-available-translations>` by the botogram version you are
38+
using.
39+
40+
.. code-block:: python
41+
42+
bot.lang = "it"
43+
44+
After doing this, the bot will start using the translated messages included in
45+
the package. If a message hasn't been translated to the selected language, the
46+
bot will fall back on the English default.
47+
48+
.. _i18n-new-language:
49+
50+
Translating botogram to a new language
51+
======================================
52+
53+
If your language isn't yet supported by botogram, you can contribute your own
54+
translation by forking the project's `git repository
55+
<https://github.com/pietroalbini/botogram>`_ and opening a new pull request.
56+
See :ref:`install-edge` for instructions on how to clone the repository and
57+
install the required dependencies. It is recommended that you don't install this
58+
bleeding edge clone as a global package: in fact, you can completely avoid
59+
installing it, while building and testing it in a local virtual environment may
60+
be useful in order to catch errors.
61+
62+
Botogram handles i18n via `GNU gettext`_, which stores translations in
63+
plain-text ``.po`` files that are then compiled while installing the package.
64+
You can find the all the translations that are currently included in the package
65+
in the ``i18n/`` directory.
66+
67+
You can generate a new language file with the following command: ::
68+
69+
$ invoke i18n-new <code>
70+
71+
where ``code`` is the `ISO 639-1 code`_ assigned the language you are
72+
translating to. This will create a new language file located at
73+
``i18n/langs/<code>.po``. The first few line will look like this:
74+
75+
.. code-block:: none
76+
77+
msgid ""
78+
msgstr ""
79+
"Project-Id-Version: botogram 1.0.dev0\n"
80+
"Report-Msgid-Bugs-To: https://github.com/pietroalbini/botogram/issues\n"
81+
"POT-Creation-Date: 2017-10-06 19:21+0200\n"
82+
"PO-Revision-Date: 2017-10-11 15:02+0200\n"
83+
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
84+
"Language: de\n"
85+
"Language-Team: de <LL@li.org>\n"
86+
"Plural-Forms: nplurals=2; plural=(n != 1)\n"
87+
"MIME-Version: 1.0\n"
88+
"Content-Type: text/plain; charset=utf-8\n"
89+
"Content-Transfer-Encoding: 8bit\n"
90+
"Generated-By: Babel 2.3.4\n"
91+
92+
The first thing you should do is to fill out the ``Last-Translator`` field with
93+
your contact information. You may also want to do the same with the copyright
94+
notice at the top of the document.
95+
96+
The remainder of the file is were translations are actually defined:
97+
98+
.. code-block:: none
99+
100+
#: botogram/defaults.py:46
101+
msgid "Use /help to get a list of all the commands."
102+
msgstr ""
103+
104+
Each message is assigned a ``msgid`` string which identifies it across
105+
translations: in botogram it is the English translation for that message.
106+
``msgstr`` fields are instead specific to each translation and define that
107+
message's translation for the file's language: this is where you need to enter
108+
your translation. If a ``msgstr`` is empty (as they are by default) botogram
109+
will default to the English translation.
110+
111+
Some messages could contain HTML formatting or
112+
`Python string interpolation`_: your translation should reflect these as closely
113+
as possible. If you need context on the usage of a message, you can refer to its
114+
usages in the source code included in the comment line above each string.
115+
116+
Just to be sure your syntax is correct, you can ensure your translation will
117+
compile correctly by invoking ::
118+
119+
$ invoke i18n-compile
120+
121+
If the command succeeds there's good chance you didn't mess up anything.
122+
123+
Once you're done, you can commit and push your changes to your fork and propose
124+
them to be merged into the upstream repository to be included in the next
125+
botogram release.
126+
127+
.. _i18n-update-translation:
128+
129+
Updating a translation
130+
======================
131+
132+
As botogram evolves, more message will probably be added to the codebase, and it
133+
is also possible for currently included translations to contain mistakes.
134+
135+
The workflow for updating a translation is basically the same as the one
136+
described in `i18n-new-language`_, but you may also need to use ::
137+
138+
$ invoke i18n-extract
139+
140+
to extract new messages from the codebase. The command also ensures references
141+
in comments are up-to-date with their current location.
142+
143+
Running the command will always result in the ``.pot`` file and ``.po`` files
144+
being updated, at least for what concerns the ``POT-Creation-Date`` header. You
145+
should check your diff and avoid committing any change that doesn't impact the
146+
actual translation and the source code references in comments.
147+
148+
While trivial, it would be nice if you also changed the ``PO-Revision-Date``
149+
header to reflect your changes.
150+
151+
.. _i18n-overriding:
152+
153+
Overriding default messages
154+
===========================
155+
156+
.. versionadded:: 0.5
157+
158+
Translating botogram to a new language is a non-trivial process and its effects
159+
are only available once a new version has been released. To avoid needing to
160+
use a custom build of the package in order to use your new translation, you can
161+
programmatically override botogram's messages while you wait for the next
162+
release.
163+
164+
This is possible through :py:attr:`botogram.Bot.override_i18n`, a dictionary
165+
that works basically the same way as a ``.po`` file, associating ``msgid``'s to
166+
``msgstr``'s:
167+
168+
.. code-block:: python
169+
170+
bot.override_i18n = {
171+
"Use /help to get a list of all the commands.": \
172+
"Utilizza /help per ottenere la lista di tutti i comandi."
173+
}
174+
175+
176+
.. _`GNU gettext`: https://www.gnu.org/software/gettext/
177+
178+
.. _`ISO 639-1 code`: https://en.wikipedia.org/wiki/List_of_ISO_639-1_codes
179+
180+
.. _`Python string interpolation`:
181+
https://docs.python.org/2/library/stdtypes.html#string-formatting

docs/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ Narrative documentation
6262
custom-components
6363
deploy/index
6464
channels
65+
i18n
6566

6667
.. _index-reference:
6768

0 commit comments

Comments
 (0)