| 1 |
.. _i18n-api: |
| 2 |
|
| 3 |
i18n API |
| 4 |
======== |
| 5 |
|
| 6 |
.. currentmodule:: sphinx.locale |
| 7 |
|
| 8 |
.. autofunction:: init |
| 9 |
|
| 10 |
.. autofunction:: init_console |
| 11 |
|
| 12 |
.. autofunction:: get_translation |
| 13 |
|
| 14 |
.. autofunction:: _ |
| 15 |
|
| 16 |
.. autofunction:: __ |
| 17 |
|
| 18 |
|
| 19 |
.. _ext-i18n: |
| 20 |
|
| 21 |
Extension internationalization (``i18n``) and localization (``l10n``) using i18n API |
| 22 |
------------------------------------------------------------------------------------ |
| 23 |
|
| 24 |
.. versionadded:: 1.8 |
| 25 |
|
| 26 |
An extension may naturally come with message translations. This is briefly |
| 27 |
summarized in :func:`sphinx.locale.get_translation` help. |
| 28 |
|
| 29 |
In practice, you have to: |
| 30 |
|
| 31 |
#. Choose a name for your message catalog, which must be unique. Usually |
| 32 |
the name of your extension is used for the name of message catalog. |
| 33 |
|
| 34 |
#. Mark in your extension sources all messages as translatable, via |
| 35 |
:func:`sphinx.locale.get_translation` function, usually renamed ``_()``, |
| 36 |
e.g.: |
| 37 |
|
| 38 |
.. code-block:: python |
| 39 |
:caption: src/__init__.py |
| 40 |
|
| 41 |
from sphinx.locale import get_translation |
| 42 |
|
| 43 |
MESSAGE_CATALOG_NAME = 'myextension' |
| 44 |
_ = get_translation(MESSAGE_CATALOG_NAME) |
| 45 |
|
| 46 |
translated_text = _('Hello Sphinx!') |
| 47 |
|
| 48 |
#. Set up your extension to be aware of its dedicated translations: |
| 49 |
|
| 50 |
.. code-block:: python |
| 51 |
:caption: src/__init__.py |
| 52 |
|
| 53 |
def setup(app): |
| 54 |
package_dir = Path(__file__).resolve().parent |
| 55 |
locale_dir = package_dir / 'locales' |
| 56 |
app.add_message_catalog(MESSAGE_CATALOG_NAME, locale_dir) |
| 57 |
|
| 58 |
#. Generate message catalog template ``*.pot`` file, usually in ``locale/`` |
| 59 |
source directory, for example via `Babel`_: |
| 60 |
|
| 61 |
.. code-block:: console |
| 62 |
|
| 63 |
$ pybabel extract --output=src/locale/myextension.pot src/ |
| 64 |
|
| 65 |
#. Create message catalogs (``*.po``) for each language which your extension |
| 66 |
will provide localization, for example via `Babel`_: |
| 67 |
|
| 68 |
.. code-block:: console |
| 69 |
|
| 70 |
$ pybabel init --input-file=src/locale/myextension.pot --domain=myextension --output-dir=src/locale --locale=fr_FR |
| 71 |
|
| 72 |
#. Translate message catalogs for each language manually |
| 73 |
|
| 74 |
#. Compile message catalogs into ``*.mo`` files, for example via `Babel`_: |
| 75 |
|
| 76 |
.. code-block:: console |
| 77 |
|
| 78 |
$ pybabel compile --directory=src/locale --domain=myextension |
| 79 |
|
| 80 |
#. Ensure that message catalog files are distributed when your package will |
| 81 |
be installed, by adding equivalent line in your extension ``MANIFEST.in``: |
| 82 |
|
| 83 |
.. code-block:: ini |
| 84 |
:caption: MANIFEST.in |
| 85 |
|
| 86 |
recursive-include src *.pot *.po *.mo |
| 87 |
|
| 88 |
|
| 89 |
When the messages on your extension has been changed, you need to also update |
| 90 |
message catalog template and message catalogs, for example via `Babel`_: |
| 91 |
|
| 92 |
.. code-block:: console |
| 93 |
|
| 94 |
$ pybabel extract --output=src/locale/myextension.pot src/ |
| 95 |
$ pybabel update --input-file=src/locale/myextension.pot --domain=myextension --output-dir=src/locale |
| 96 |
|
| 97 |
.. _Babel: https://babel.pocoo.org/ |