| 1 |
.. _user-guide-i18n: |
| 2 |
|
| 3 |
Internationalization |
| 4 |
==================== |
| 5 |
|
| 6 |
This theme contains localizable (translatable) strings. |
| 7 |
There are two kinds of strings in this theme, with different steps to translate each: |
| 8 |
|
| 9 |
* **Built-in strings** are hard-coded in the theme's templates. They will be translated by volunteers if the language is `supported <https://github.com/pydata/pydata-sphinx-theme/tree/main/src/pydata_sphinx_theme/locale>`__. |
| 10 |
To add another language, refer to the :ref:`localizing-the-theme` and :ref:`adding-new-language` sections in the documentation. |
| 11 |
|
| 12 |
* **Configurable strings** are user-defined with the ``html_theme_options`` variable in your ``conf.py`` file (see other |
| 13 |
sections in :doc:`the user guide <index>` for examples of these configurable strings). |
| 14 |
To translate these strings, see the :ref:`translating-configurable-strings` section in this page. |
| 15 |
|
| 16 |
.. _translating-configurable-strings: |
| 17 |
|
| 18 |
Translating configurable strings |
| 19 |
-------------------------------- |
| 20 |
|
| 21 |
These instructions are for translating configurable strings (those that are customizable in ``html_theme_options`` within |
| 22 |
the ``conf.py`` file). |
| 23 |
|
| 24 |
These instructions assume that you store your translations in a ``locale`` directory under your documentation directory |
| 25 |
and that you want to use ``messages`` as the name of the message catalog for these strings (you can change this name if |
| 26 |
needed). |
| 27 |
|
| 28 |
Note you will also need to install `pybabel <https://babel.pocoo.org/en/latest/installation.html>`__ to handle your |
| 29 |
documentation translations. |
| 30 |
|
| 31 |
#. In your ``conf.py`` file: |
| 32 |
|
| 33 |
.. code-block:: python |
| 34 |
|
| 35 |
import os.path |
| 36 |
from sphinx.locale import get_translation |
| 37 |
|
| 38 |
catalog = "messages" |
| 39 |
_ = get_translation(catalog) |
| 40 |
|
| 41 |
html_theme_options = { |
| 42 |
"search_bar_text": _("Search the docs..."), |
| 43 |
|
| 44 |
# You only need to translate the following if you use these features. |
| 45 |
"icon_links_label": _("Quick Links"), |
| 46 |
"icon_links": [ |
| 47 |
{ |
| 48 |
"name": _("GitHub"), |
| 49 |
"url": "https://github.com/<your-org>/<your-repo>", |
| 50 |
"icon": "fab fa-github-square", |
| 51 |
}, |
| 52 |
], |
| 53 |
"external_links": [ |
| 54 |
{ |
| 55 |
"name": _("link-one-name"), |
| 56 |
"url": "https://<link-one>", |
| 57 |
}, |
| 58 |
], |
| 59 |
} |
| 60 |
|
| 61 |
def setup(app): |
| 62 |
locale_dir = os.path.join(os.path.abspath(os.path.dirname(__file__)), "locale") |
| 63 |
|
| 64 |
app.add_message_catalog(catalog, locale_dir) |
| 65 |
|
| 66 |
#. Extract the strings to localize: |
| 67 |
|
| 68 |
.. code-block:: bash |
| 69 |
|
| 70 |
pybabel extract . -o locale/messages.pot |
| 71 |
|
| 72 |
#. Create a message catalog by specifying the `ISO 639-1 code <https://en.wikipedia.org/wiki/List_of_ISO_639-1_codes>`__ for the new language (using the ``--locale`` flag): |
| 73 |
|
| 74 |
.. code-block:: bash |
| 75 |
|
| 76 |
# for example, to add French (ISO 639-1 code: fr) |
| 77 |
pybabel init --input-file=locale/messages.pot --domain=messages --output-dir=locale --locale=fr |
| 78 |
|
| 79 |
|
| 80 |
#. Translate the message catalog by editing the file. |
| 81 |
|
| 82 |
#. Compile the message catalog: |
| 83 |
|
| 84 |
.. code-block:: bash |
| 85 |
|
| 86 |
pybabel compile --directory=locale --domain=messages |
| 87 |
|
| 88 |
Done! Your configurable strings are now localized. |