Skip to content

Commit d3af0da

Browse files
committed
Merge branch 'alcc01/i18n-narrative-documentation'
2 parents eff97a6 + d16bb01 commit d3af0da

File tree

3 files changed

+191
-0
lines changed

3 files changed

+191
-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: 184 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,184 @@
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-overriding:
49+
50+
Overriding default messages
51+
===========================
52+
53+
.. versionadded:: 0.5
54+
55+
As described in :ref:`i18n-new-language`, new and updated translations'
56+
availability is limited to new botogram releases, meaning that it could take
57+
some time for them to reach end users.
58+
59+
Packaged translations may also not always fit a specific use case, making it
60+
necessary for you to edit some of the messages. While you could use a custom
61+
build of the package with a modified translation, it is also possible to
62+
programmatically override the translation of single messages through
63+
:py:attr:`botogram.Bot.override_i18n`, a dictionary that works basically the
64+
same way as a ``.po`` file, associating ``msgid``'s to ``msgstr``'s:
65+
66+
.. code-block:: python
67+
68+
bot.override_i18n = {
69+
"Use /help to get a list of all the commands.": \
70+
"Utilizza /help per ottenere la lista di tutti i comandi."
71+
}
72+
73+
We'll go more in depth on the translation format in the next section.
74+
75+
.. _i18n-new-language:
76+
77+
Translating botogram to a new language
78+
======================================
79+
80+
If your language isn't yet supported by botogram, you can contribute your own
81+
translation by forking the project's `git repository
82+
<https://github.com/pietroalbini/botogram>`_ and opening a new pull request.
83+
See :ref:`install-edge` for instructions on how to clone the repository and
84+
install the required dependencies. It is recommended that you don't install this
85+
bleeding edge clone as a global package: in fact, you can completely avoid
86+
installing it, while building and testing it in a local virtual environment may
87+
be useful in order to catch errors.
88+
89+
Botogram handles i18n via `GNU gettext`_, which stores translations in
90+
plain-text ``.po`` files that are then compiled while installing the package.
91+
You can find the all the translations that are currently included in the package
92+
in the ``i18n/`` directory.
93+
94+
You can generate a new language file with the following command: ::
95+
96+
$ invoke i18n-new <code>
97+
98+
where ``code`` is the `ISO 639-1 code`_ assigned the language you are
99+
translating to. This will create a new language file located at
100+
``i18n/langs/<code>.po``. The first few line will look like this:
101+
102+
.. code-block:: none
103+
104+
msgid ""
105+
msgstr ""
106+
"Project-Id-Version: botogram 1.0.dev0\n"
107+
"Report-Msgid-Bugs-To: https://github.com/pietroalbini/botogram/issues\n"
108+
"POT-Creation-Date: 2017-10-06 19:21+0200\n"
109+
"PO-Revision-Date: 2017-10-11 15:02+0200\n"
110+
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
111+
"Language: de\n"
112+
"Language-Team: de <[email protected]>\n"
113+
"Plural-Forms: nplurals=2; plural=(n != 1)\n"
114+
"MIME-Version: 1.0\n"
115+
"Content-Type: text/plain; charset=utf-8\n"
116+
"Content-Transfer-Encoding: 8bit\n"
117+
"Generated-By: Babel 2.3.4\n"
118+
119+
The first thing you should do is to fill out the ``Last-Translator`` field with
120+
your contact information. You may also want to do the same with the copyright
121+
notice at the top of the document.
122+
123+
The remainder of the file is were translations are actually defined:
124+
125+
.. code-block:: none
126+
127+
#: botogram/defaults.py:46
128+
msgid "Use /help to get a list of all the commands."
129+
msgstr ""
130+
131+
Each message is assigned a ``msgid`` string which identifies it across
132+
translations: in botogram it is the English translation for that message.
133+
``msgstr`` fields are instead specific to each translation and define that
134+
message's translation for the file's language: this is where you need to enter
135+
your translation. If a ``msgstr`` is empty (as they are by default) botogram
136+
will default to the English translation.
137+
138+
Some messages could contain HTML formatting or
139+
`Python string interpolation`_: your translation should reflect these as closely
140+
as possible. If you need context on the usage of a message, you can refer to its
141+
usages in the source code included in the comment line above each string.
142+
143+
Just to be sure your syntax is correct, you can ensure your translation will
144+
compile correctly by invoking ::
145+
146+
$ invoke i18n-compile
147+
148+
If the command succeeds there's good chance you didn't mess up anything.
149+
150+
Once you're done, you can commit and push your changes to your fork and propose
151+
them to be merged into the upstream repository to be included in the next
152+
botogram release.
153+
154+
.. _i18n-update-translation:
155+
156+
Updating a translation
157+
======================
158+
159+
As botogram evolves, more message will probably be added to the codebase, and it
160+
is also possible for currently included translations to contain mistakes.
161+
162+
The workflow for updating a translation is basically the same as the one
163+
described in :ref:`i18n-new-language`, but you may also need to use ::
164+
165+
$ invoke i18n-extract
166+
167+
to extract new messages from the codebase. The command also ensures references
168+
in comments are up-to-date with their current location.
169+
170+
Running the command will always result in the ``.pot`` file and ``.po`` files
171+
being updated, at least for what concerns the ``POT-Creation-Date`` header. You
172+
should check your diff and avoid committing any change that doesn't impact the
173+
actual translation and the source code references in comments.
174+
175+
While trivial, it would be nice if you also changed the ``PO-Revision-Date``
176+
header to reflect your changes.
177+
178+
179+
.. _`GNU gettext`: https://www.gnu.org/software/gettext/
180+
181+
.. _`ISO 639-1 code`: https://en.wikipedia.org/wiki/List_of_ISO_639-1_codes
182+
183+
.. _`Python string interpolation`:
184+
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)