| 1 |
Internationalization |
| 2 |
==================== |
| 3 |
|
| 4 |
This section covers how to internationalize (I18n) and localize (L10n) the PyData Sphinx Theme. |
| 5 |
For details on how to localize the configurable strings in your Sphinx project, see the |
| 6 |
:ref:`User guide section on internationalization <user-guide-i18n>`. |
| 7 |
|
| 8 |
The PyData Sphinx Theme I18n/i10n workflows are based on `GNU Gettext <https://www.gnu.org/software/gettext/>`__ |
| 9 |
and `pybabel <https://babel.pocoo.org/en/latest/messages.html>`__ is used to keep the catalogs up to date. |
| 10 |
Crowd-sourced localizations are managed on `Transifex <https://explore.transifex.com/12rambau/pydata-sphinx-theme/>`__. |
| 11 |
|
| 12 |
.. note:: |
| 13 |
**Internationalization** (or I18n) is the process of making a program or application aware of and able to support multiple |
| 14 |
languages. |
| 15 |
|
| 16 |
**Localization** (L10n) is the process of translating localized programs or applications into different languages while |
| 17 |
ensuring that the translations are correct for some native language and cultural habits. |
| 18 |
|
| 19 |
The formal description of specific set of cultural habits for some country, together with all associated translations |
| 20 |
targeted to the same native language, is called the *locale* for this language or country. |
| 21 |
For more information about localization and internationalization see `GNU gettext concepts <https://www.gnu.org/software/gettext/manual/html_node/Concepts.html>`__. |
| 22 |
|
| 23 |
|
| 24 |
The general process for internationalizing and localizing the theme is as follows: |
| 25 |
|
| 26 |
#. :ref:`Mark strings in the theme as localizable <adding-localizable-text>`. |
| 27 |
#. :ref:`Extract localizable strings <updating-localization-files>` to a message catalog template ``POT`` (``PO`` template file). |
| 28 |
#. Generare a ``PO`` file from the ``POT`` file for :ref:`a new language (locale) you want to localize <adding-new-language>` |
| 29 |
(or :ref:`update existing localization files <updating-localization-files>`). |
| 30 |
#. :ref:`Compile the message catalogs <compiling-localization-files>` to binary ``MO`` files. |
| 31 |
#. :ref:`Localize the theme <localizing-the-theme>`. |
| 32 |
|
| 33 |
Localization files |
| 34 |
------------------- |
| 35 |
|
| 36 |
There are three types of files used in the localization process: |
| 37 |
|
| 38 |
#. `PO files <https://www.gnu.org/software/gettext/manual/gettext.html#PO-Files>`__ ( Portable Object, also known as message catalogs) |
| 39 |
associate each original, translatable string (defined in ``msgid``) of a given program with its translation in a |
| 40 |
particular target language (defined in ``msgstr`` fields). A single PO file is dedicated to a single target language. |
| 41 |
#. ``MO`` (Machine Object) files are a binary version of a PO file. |
| 42 |
#. ``POT`` (Portable Object Template) files are similar to PO files, but with empty translations. |
| 43 |
They are used as a template for new languages. |
| 44 |
|
| 45 |
|
| 46 |
.. _adding-localizable-text: |
| 47 |
|
| 48 |
Marking strings as localizable |
| 49 |
------------------------------ |
| 50 |
|
| 51 |
All natural language text in the theme's components and layouts must be marked as localizable so that they can be later translated (or localized) into other languages. |
| 52 |
For example, if you add a button with the text **Next page**, you will need to mark this text as localizable. |
| 53 |
|
| 54 |
``HTML`` templates (located in the ``src/pydata_sphinx_theme/theme/`` directory). |
| 55 |
To do so, you can use the Jinja2 ``trans`` block and/or a ``_()`` function to mark text as localizable in the corresponding |
| 56 |
|
| 57 |
For example, to mark the text **Next page** as localizable, you would write: |
| 58 |
|
| 59 |
.. code-block:: jinja |
| 60 |
|
| 61 |
<button type="button"> |
| 62 |
{{- _("Next page") -}} |
| 63 |
</button> |
| 64 |
|
| 65 |
Any text that is marked in this way will be discoverable by ``pybabel`` and used to generate the localization files |
| 66 |
(``PO`` files). |
| 67 |
|
| 68 |
Once you've marked the text as localizable, complete the steps outlined in the :ref:`updating-localization-files` |
| 69 |
section of this documentation. |
| 70 |
|
| 71 |
For more details on marking strings as localizable in jinja templates visit `the Jinja2 documentation <https://jinja.palletsprojects.com/en/stable/templates/>`__. |
| 72 |
|
| 73 |
.. tip:: |
| 74 |
Remember to `manually escape variables <https://jinja.palletsprojects.com/en/stable/templates/#working-with-manual-escaping>`__ if needed. |
| 75 |
|
| 76 |
Sometimes, it can help localizers to describe where a string comes from or whether it refers to a noun or verb, |
| 77 |
particularly if it can be difficult to find in the theme, or if the string itself is not very self-descriptive (for example, very short strings). |
| 78 |
If you immediately precede the string with a comment that starts with ``L10n:``, the comment will be added to the PO |
| 79 |
file, and visible to localizers. For example: |
| 80 |
|
| 81 |
.. code-block:: jinja |
| 82 |
|
| 83 |
{# L10n: Navigation button at the bottom of the page #} |
| 84 |
<button type="button"> |
| 85 |
{{- _('Next page') -}} |
| 86 |
</button> |
| 87 |
|
| 88 |
.. _updating-localization-files: |
| 89 |
|
| 90 |
Updating the localization files |
| 91 |
----------------------------------- |
| 92 |
|
| 93 |
When you add or change natural language text in the theme, you must update the message catalogs to include the new or |
| 94 |
updated text. Follow these steps: |
| 95 |
|
| 96 |
#. Edit the natural language text and ensure it is :ref:`marked as localizable <adding-localizable-text>`. |
| 97 |
|
| 98 |
#. Extract the strings and update the localization files (``POT`` file): |
| 99 |
|
| 100 |
.. code-block:: bash |
| 101 |
|
| 102 |
# note this will by default update all the localization files for all the supported locales |
| 103 |
tox run -e i18n-extract |
| 104 |
|
| 105 |
This will update the localization files with new information about the position and text of the language you have modified. |
| 106 |
|
| 107 |
If you *only* change non-localizable text (like HTML markup), the ``extract`` command will only update the |
| 108 |
positions (line numbers) of the localizable strings. |
| 109 |
Updating positions is optional - the line numbers are to inform the human translator, not to perform the localization. |
| 110 |
But it is best practice to keep the positions up to date. |
| 111 |
|
| 112 |
If you change localizable strings, the above command will extract the new or updated strings to localization template |
| 113 |
file (``POT``) and perform a fuzzy match between the new or updated strings and existing localizations in the |
| 114 |
localization files. |
| 115 |
If there is a fuzzy match, a comment like ``#, fuzzy`` is added before the matched entry, this means that the |
| 116 |
localization needs to be manually reviewed and possibly updated. |
| 117 |
If after reviewing the localization you decide to keep the existing localization, you can remove the ``#, fuzzy`` |
| 118 |
comment from the entry. |
| 119 |
If there is no fuzzy match, it will add a new localization entry. |
| 120 |
You can learn more about fuzzy entries in the `GNU gettext manual <https://www.gnu.org/software/gettext/manual/gettext.html#Fuzzy-Entries>`__. |
| 121 |
|
| 122 |
.. _compiling-localization-files: |
| 123 |
|
| 124 |
Compiling the localization files |
| 125 |
-------------------------------- |
| 126 |
|
| 127 |
Gettext doesn't parse any text files, it reads a binary format for faster performance. To compile the latest PO files in |
| 128 |
the repository run: |
| 129 |
|
| 130 |
.. code-block:: bash |
| 131 |
|
| 132 |
tox run -e i18n-compile |
| 133 |
|
| 134 |
You can also run the extract, update and compile commands in one go: |
| 135 |
|
| 136 |
.. code-block:: bash |
| 137 |
|
| 138 |
tox run -m i18n |
| 139 |
|
| 140 |
This will update the localization files and compile them into binary ``MO`` files in a single step. |
| 141 |
However, if there are fuzzy matches needing review, the compilation will fail, and you will need to review the |
| 142 |
localizations manually. |
| 143 |
Then compile the files again. |
| 144 |
|
| 145 |
.. _adding-new-language: |
| 146 |
|
| 147 |
Adding a new language |
| 148 |
---------------------- |
| 149 |
|
| 150 |
The list of languages with existing (possibly incomplete) localizations is available in the |
| 151 |
``src/pydata_sphinx_theme/locale`` directory. |
| 152 |
|
| 153 |
To add a new language, follow these steps: |
| 154 |
|
| 155 |
#. Identify the `ISO 639-1 code <https://en.wikipedia.org/wiki/List_of_ISO_639-1_codes>`__ for the new language. |
| 156 |
|
| 157 |
#. Generate a ``PO`` file based on ``POT`` file for this new language: |
| 158 |
|
| 159 |
.. code-block:: bash |
| 160 |
|
| 161 |
# for example, to add Quechua (ISO 639-1 code: qu) |
| 162 |
tox -e i18n-new-locale -- qu |
| 163 |
|
| 164 |
#. Update and compile the localization files as described in the :ref:`updating-localization-files` and |
| 165 |
:ref:`compiling-localization-files` sections. Then commit the changes. |
| 166 |
#. Localize the theme's into the newly added language (see :ref:`localizing-the-theme`). |
| 167 |
|
| 168 |
.. _localizing-the-theme: |
| 169 |
|
| 170 |
Localizing the theme |
| 171 |
--------------------- |
| 172 |
|
| 173 |
We manage localizations on the `PyData Sphinx Theme project on Transifex <https://explore.transifex.com/12rambau/pydata-sphinx-theme/>`__. |
| 174 |
|
| 175 |
To contribute localization, follow these steps: |
| 176 |
|
| 177 |
#. Sign up for a `Transifex account <https://app.transifex.com/signup/>`__. |
| 178 |
#. Join the `PyData Sphinx Theme project <https://explore.transifex.com/12rambau/pydata-sphinx-theme/>`__. |
| 179 |
#. Select the language you want to localize. If the language you are looking for is not listed, you can `open an issue on GitHub to request it <https://github.com/pydata/pydata-sphinx-theme/issues>`__. Then you can open a pull request |
| 180 |
to add the new language following the steps outlined in :ref:`adding-new-language`. |
| 181 |
#. Now you are ready to start localizing the theme. If you are new to Transifex you can visit the |
| 182 |
`Transifex documentation <https://help.transifex.com/en/articles/6240403-translating-html-content>`__ for more information. |
| 183 |
|
| 184 |
Once you have completed your localization, the PyData Sphinx Theme maintainers will review and approve it. |
| 185 |
|
| 186 |
Localization tips |
| 187 |
----------------- |
| 188 |
|
| 189 |
Localize phrases, not words |
| 190 |
```````````````````````````` |
| 191 |
|
| 192 |
Full sentences and clauses must always be a single localizable string. |
| 193 |
Otherwise, you can get ``next page`` localizated as ``suivant page`` instead of as ``page suivante``, etc. |
| 194 |
|
| 195 |
Dealing with variables and markup in localizations |
| 196 |
`````````````````````````````````````````````````` |
| 197 |
|
| 198 |
A localizable string can be a combination of a fixed string and a variable, for example, ``Welcome to the Spanish version of the site`` |
| 199 |
is a combination of the fixed parts ``Welcome to the`` and ``version of the site`` and the variable part ``Spanish``. |
| 200 |
|
| 201 |
.. code-block:: jinja |
| 202 |
|
| 203 |
{% trans language=language %} |
| 204 |
Welcome to the {{ language }} version of the site |
| 205 |
{% endtrans %} |
| 206 |
|
| 207 |
Binding the variable as ``language=language`` ensures the string can be properly localized, especially as the word order |
| 208 |
may vary across locales. |
| 209 |
The above string will be extracted as ``Welcome to the %(language) version of the site``. |
| 210 |
The translator must use ``%(language)`` verbatim while localizing the theme. |
| 211 |
|
| 212 |
When a block contains HTML with attributes, those which don't need to be localized should be passed as arguments. |
| 213 |
This ensures strings won't need to be re-localized if those attributes change: |
| 214 |
|
| 215 |
.. code-block:: jinja |
| 216 |
|
| 217 |
{% trans url="https://pydata.org/" %} |
| 218 |
Please visit <a href="{{ url }}" title="PyData website">the PyData website</a> for more information. |
| 219 |
{% endtrans %} |
| 220 |
|
| 221 |
|
| 222 |
References |
| 223 |
---------- |
| 224 |
|
| 225 |
I18N and L10N are deep topics. Here, we only cover the bare minimum needed to fulfill basic technical tasks. You might like: |
| 226 |
|
| 227 |
- `Internationalis(z)ing Code <https://www.youtube.com/watch?v=0j74jcxSunY>`__ by Computerphile on YouTube |
| 228 |
- `Falsehoods Programmers Believe About Language <http://garbled.benhamill.com/2017/04/18/falsehoods-programmers-believe-about-language>`__ by Ben Hamill |