sphinx/doc/extdev/i18n.rst

98 lines
2.7 KiB
ReStructuredText
Raw Normal View History

2018-02-25 06:41:39 -06:00
.. _i18n-api:
i18n API
========
.. currentmodule:: sphinx.locale
.. autofunction:: init
.. autofunction:: init_console
.. autofunction:: get_translation
.. autofunction:: _
.. autofunction:: __
.. _ext-i18n:
2019-03-02 04:45:42 -06:00
Extension internationalization (`i18n`) and localization (`l10n`) using i18n API
2019-05-29 11:07:05 -05:00
--------------------------------------------------------------------------------
.. versionadded:: 1.8
2019-03-02 04:45:42 -06:00
An extension may naturally come with message translations. This is briefly
summarized in :func:`sphinx.locale.get_translation` help.
In practice, you have to:
2019-03-02 04:45:42 -06:00
#. Choose a name for your message catalog, which must be unique. Usually
the name of your extension is used for the name of message catalog.
#. Mark in your extension sources all messages as translatable, via
:func:`sphinx.locale.get_translation` function, usually renamed ``_()``,
e.g.:
.. code-block:: python
2019-03-02 04:45:42 -06:00
:caption: src/__init__.py
2019-03-02 04:45:42 -06:00
from sphinx.locale import get_translation
2019-03-02 04:45:42 -06:00
MESSAGE_CATALOG_NAME = 'myextension'
_ = get_translation(MESSAGE_CATALOG_NAME)
2019-03-02 04:45:42 -06:00
translated_text = _('Hello Sphinx!')
2019-03-02 04:45:42 -06:00
#. Set up your extension to be aware of its dedicated translations:
.. code-block:: python
2019-03-02 04:45:42 -06:00
:caption: src/__init__.py
2019-03-02 04:45:42 -06:00
def setup(app):
package_dir = path.abspath(path.dirname(__file__))
locale_dir = os.path.join(package_dir, 'locales')
app.add_message_catalog(MESSAGE_CATALOG_NAME, locale_dir)
2019-03-02 04:45:42 -06:00
#. Generate message catalog template ``*.pot`` file, usually in ``locale/``
source directory, for example via `Babel`_:
2019-03-02 04:45:42 -06:00
.. code-block:: console
2019-03-02 04:45:42 -06:00
$ pybabel extract --output=src/locale/myextension.pot src/
2019-03-02 04:45:42 -06:00
#. Create message catalogs (``*.po``) for each language which your extension
will provide localization, for example via `Babel`_:
2019-03-02 04:45:42 -06:00
.. code-block:: console
2019-03-02 04:45:42 -06:00
$ pybabel init --input-file=src/locale/myextension.pot --domain=myextension --output-dir=src/locale --locale=fr_FR
2019-03-02 04:45:42 -06:00
#. Translate message catalogs for each language manually
2019-03-02 04:45:42 -06:00
#. Compile message catalogs into ``*.mo`` files, for example via `Babel`_:
2019-03-02 04:45:42 -06:00
.. code-block:: console
2019-03-02 04:45:42 -06:00
$ pybabel compile --directory=src/locale --domain=myextension
2019-03-02 04:45:42 -06:00
#. Ensure that message catalog files are distributed when your package will
be installed, by adding equivalent line in your extension ``MANIFEST.in``:
.. code-block:: ini
2019-03-02 04:45:42 -06:00
:caption: MANIFEST.in
recursive-include src *.pot *.po *.mo
When the messages on your extension has been changed, you need to also update
message catalog template and message catalogs, for example via `Babel`_:
.. code-block:: console
2019-03-02 04:45:42 -06:00
$ pybabel extract --output=src/locale/myextension.pot src/
$ pybabel update --input-file=src/locale/myextension.pot --domain=myextension --output-dir=src/locale
.. _Babel: http://babel.pocoo.org/