| 1 |
.. _intl: |
| 2 |
|
| 3 |
Internationalization |
| 4 |
==================== |
| 5 |
|
| 6 |
.. versionadded:: 1.1 |
| 7 |
|
| 8 |
Complementary to translations provided for Sphinx-generated messages such as |
| 9 |
navigation bars, Sphinx provides mechanisms facilitating the translation of |
| 10 |
*documents*. See the :ref:`intl-options` for details on configuration. |
| 11 |
|
| 12 |
.. figure:: /_static/translation.* |
| 13 |
:width: 100% |
| 14 |
|
| 15 |
Workflow visualization of translations in Sphinx. (The figure is created by |
| 16 |
`plantuml <https://plantuml.com>`_.) |
| 17 |
|
| 18 |
.. contents:: |
| 19 |
:local: |
| 20 |
|
| 21 |
Sphinx internationalization details |
| 22 |
----------------------------------- |
| 23 |
|
| 24 |
**gettext** [1]_ is an established standard for internationalization and |
| 25 |
localization. It naively maps messages in a program to a translated string. |
| 26 |
Sphinx uses these facilities to translate whole documents. |
| 27 |
|
| 28 |
Initially project maintainers have to collect all translatable strings (also |
| 29 |
referred to as *messages*) to make them known to translators. Sphinx extracts |
| 30 |
these through invocation of :command:`sphinx-build -M gettext`. |
| 31 |
|
| 32 |
Every single element in the doctree will end up in a single message which |
| 33 |
results in lists being equally split into different chunks while large |
| 34 |
paragraphs will remain as coarsely-grained as they were in the original |
| 35 |
document. This grants seamless document updates while still providing a little |
| 36 |
bit of context for translators in free-text passages. It is the maintainer's |
| 37 |
task to split up paragraphs which are too large as there is no sane automated |
| 38 |
way to do that. |
| 39 |
|
| 40 |
After Sphinx successfully ran the |
| 41 |
:class:`~sphinx.builders.gettext.MessageCatalogBuilder` you will find a |
| 42 |
collection of ``.pot`` files in your output directory. These are **catalog |
| 43 |
templates** and contain messages in your original language *only*. |
| 44 |
|
| 45 |
They can be delivered to translators which will transform them to ``.po`` files |
| 46 |
--- so called **message catalogs** --- containing a mapping from the original |
| 47 |
messages to foreign-language strings. |
| 48 |
|
| 49 |
*gettext* compiles them into a binary format known as **binary catalogs** |
| 50 |
through :program:`msgfmt` for efficiency reasons. If you make these files |
| 51 |
discoverable with :confval:`locale_dirs` for your :confval:`language`, Sphinx |
| 52 |
will pick them up automatically. |
| 53 |
|
| 54 |
An example: you have a document :file:`usage.rst` in your Sphinx project. The |
| 55 |
*gettext* builder will put its messages into :file:`usage.pot`. Imagine you have |
| 56 |
Spanish translations [2]_ stored in :file:`usage.po` --- for your builds to |
| 57 |
be translated you need to follow these instructions: |
| 58 |
|
| 59 |
* Compile your message catalog to a locale directory, say ``locale``, so it |
| 60 |
ends up in :file:`./locale/es/LC_MESSAGES/usage.mo` in your source directory |
| 61 |
(where ``es`` is the language code for Spanish.) :: |
| 62 |
|
| 63 |
msgfmt "usage.po" -o "locale/es/LC_MESSAGES/usage.mo" |
| 64 |
|
| 65 |
* Set :confval:`locale_dirs` to ``["locale/"]``. |
| 66 |
* Set :confval:`language` to ``es`` (also possible via |
| 67 |
:option:`-D <sphinx-build -D>`). |
| 68 |
* Run your desired build. |
| 69 |
|
| 70 |
|
| 71 |
In order to protect against mistakes, a warning is emitted if |
| 72 |
cross-references in the translated paragraph do not match those from the |
| 73 |
original. This can be turned off globally using the |
| 74 |
:confval:`suppress_warnings` configuration variable. Alternatively, to |
| 75 |
turn it off for one message only, end the message with ``#noqa`` like |
| 76 |
this:: |
| 77 |
|
| 78 |
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse |
| 79 |
risus tortor, luctus id ultrices at. #noqa |
| 80 |
|
| 81 |
(Write ``\#noqa`` in case you want to have "#noqa" literally in the |
| 82 |
text. This does not apply to code blocks, where ``#noqa`` is ignored |
| 83 |
because code blocks do not contain references anyway.) |
| 84 |
|
| 85 |
.. versionadded:: 4.5 |
| 86 |
The ``#noqa`` mechanism. |
| 87 |
|
| 88 |
|
| 89 |
Translating with sphinx-intl |
| 90 |
---------------------------- |
| 91 |
|
| 92 |
Quick guide |
| 93 |
~~~~~~~~~~~ |
| 94 |
|
| 95 |
`sphinx-intl`_ is a useful tool to work with Sphinx translation flow. This |
| 96 |
section describe an easy way to translate with *sphinx-intl*. |
| 97 |
|
| 98 |
#. Install `sphinx-intl`_. |
| 99 |
|
| 100 |
.. code-block:: console |
| 101 |
|
| 102 |
$ pip install sphinx-intl |
| 103 |
|
| 104 |
#. Add configurations to :file:`conf.py`. |
| 105 |
|
| 106 |
:: |
| 107 |
|
| 108 |
locale_dirs = ['locale/'] # path is example but recommended. |
| 109 |
gettext_compact = False # optional. |
| 110 |
|
| 111 |
This case-study assumes that BUILDDIR is set to ``_build``, |
| 112 |
:confval:`locale_dirs` is set to ``locale/`` and :confval:`gettext_compact` |
| 113 |
is set to ``False`` (the Sphinx document is already configured as such). |
| 114 |
|
| 115 |
#. Extract translatable messages into pot files. |
| 116 |
|
| 117 |
.. code-block:: console |
| 118 |
|
| 119 |
$ make gettext |
| 120 |
|
| 121 |
The generated pot files will be placed in the ``_build/gettext`` directory. |
| 122 |
If you want to customize the output beyond what can be done via the |
| 123 |
:ref:`intl-options`, the |
| 124 |
:download:`default pot file template <../../../sphinx/templates/gettext/message.pot.jinja>` |
| 125 |
can be replaced by a custom :file:`message.pot.jinja` file placed in any |
| 126 |
directory listed in :confval:`templates_path`. |
| 127 |
|
| 128 |
#. Generate po files. |
| 129 |
|
| 130 |
We'll use the pot files generated in the above step. |
| 131 |
|
| 132 |
.. code-block:: console |
| 133 |
|
| 134 |
$ sphinx-intl update -p _build/gettext -l de -l ja |
| 135 |
|
| 136 |
Once completed, the generated po files will be placed in the below |
| 137 |
directories: |
| 138 |
|
| 139 |
* ``./locale/de/LC_MESSAGES/`` |
| 140 |
* ``./locale/ja/LC_MESSAGES/`` |
| 141 |
|
| 142 |
#. Translate po files. |
| 143 |
|
| 144 |
As noted above, these are located in the ``./locale/<lang>/LC_MESSAGES`` |
| 145 |
directory. An example of one such file, from Sphinx, :file:`builders.po`, is |
| 146 |
given below. |
| 147 |
|
| 148 |
.. code-block:: po |
| 149 |
|
| 150 |
# a5600c3d2e3d48fc8c261ea0284db79b |
| 151 |
#: ../../builders.rst:4 |
| 152 |
msgid "Available builders" |
| 153 |
msgstr "<FILL HERE BY TARGET LANGUAGE>" |
| 154 |
|
| 155 |
Another case, msgid is multi-line text and contains reStructuredText syntax: |
| 156 |
|
| 157 |
.. code-block:: po |
| 158 |
|
| 159 |
# 302558364e1d41c69b3277277e34b184 |
| 160 |
#: ../../builders.rst:9 |
| 161 |
msgid "" |
| 162 |
"These are the built-in Sphinx builders. More builders can be added by " |
| 163 |
":ref:`extensions <extensions>`." |
| 164 |
msgstr "" |
| 165 |
"FILL HERE BY TARGET LANGUAGE FILL HERE BY TARGET LANGUAGE FILL HERE " |
| 166 |
"BY TARGET LANGUAGE :ref:`EXTENSIONS <extensions>` FILL HERE." |
| 167 |
|
| 168 |
Please be careful not to break reStructuredText notation. |
| 169 |
Most po-editors will help you with that. |
| 170 |
|
| 171 |
#. Build translated document. |
| 172 |
|
| 173 |
You need a :confval:`language` parameter in :file:`conf.py` or you may also |
| 174 |
specify the parameter on the command line. |
| 175 |
|
| 176 |
For BSD/GNU make, run: |
| 177 |
|
| 178 |
.. code-block:: console |
| 179 |
|
| 180 |
$ make -e SPHINXOPTS="-D language='de'" html |
| 181 |
|
| 182 |
For Windows :command:`cmd.exe`, run: |
| 183 |
|
| 184 |
.. code-block:: doscon |
| 185 |
|
| 186 |
> set SPHINXOPTS=-D language=de |
| 187 |
> .\make.bat html |
| 188 |
|
| 189 |
For PowerShell, run: |
| 190 |
|
| 191 |
.. code-block:: ps1con |
| 192 |
|
| 193 |
PS> Set-Item env:SPHINXOPTS "-D language=de" |
| 194 |
PS> .\make.bat html |
| 195 |
|
| 196 |
Congratulations! You got the translated documentation in the ``_build/html`` |
| 197 |
directory. |
| 198 |
|
| 199 |
.. versionadded:: 1.3 |
| 200 |
|
| 201 |
:program:`sphinx-build` that is invoked by make command will build po files |
| 202 |
into mo files. |
| 203 |
|
| 204 |
If you are using 1.2.x or earlier, please invoke :command:`sphinx-intl build` |
| 205 |
command before :command:`make` command. |
| 206 |
|
| 207 |
Translating |
| 208 |
~~~~~~~~~~~ |
| 209 |
|
| 210 |
Update your po files by new pot files |
| 211 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 212 |
|
| 213 |
If a document is updated, it is necessary to generate updated pot files and to |
| 214 |
apply differences to translated po files. In order to apply the updates from a |
| 215 |
pot file to the po file, use the :command:`sphinx-intl update` command. |
| 216 |
|
| 217 |
.. code-block:: console |
| 218 |
|
| 219 |
$ sphinx-intl update -p _build/gettext |
| 220 |
|
| 221 |
|
| 222 |
Using Transifex service for team translation |
| 223 |
-------------------------------------------- |
| 224 |
|
| 225 |
Transifex_ is one of several services that allow collaborative translation via a |
| 226 |
web interface. It has a nifty Go-based command line client that makes it |
| 227 |
easy to fetch and push translations. |
| 228 |
|
| 229 |
.. TODO: why use transifex? |
| 230 |
|
| 231 |
|
| 232 |
#. Install the `Transifex CLI tool`_. |
| 233 |
|
| 234 |
You need the :command:`tx` command line tool for uploading resources (pot files). |
| 235 |
The official installation process place the :file:`tx` binary file in |
| 236 |
the current directory along with a README and a LICENSE file, and adds |
| 237 |
the current directory to ``$PATH``. |
| 238 |
|
| 239 |
.. code-block:: console |
| 240 |
|
| 241 |
$ curl -o- https://raw.githubusercontent.com/transifex/cli/master/install.sh | bash |
| 242 |
|
| 243 |
.. seealso:: `Transifex Client documentation`_ |
| 244 |
|
| 245 |
#. Create your Transifex_ account and create a new project and an organization |
| 246 |
for your document. |
| 247 |
|
| 248 |
Currently, Transifex does not allow for a translation project to have more |
| 249 |
than one version of the document, so you'd better include a version number in |
| 250 |
your project name. |
| 251 |
|
| 252 |
For example: |
| 253 |
|
| 254 |
:Organization ID: ``sphinx-document`` |
| 255 |
:Project ID: ``sphinx-document-test_1_0`` |
| 256 |
:Project URL: ``https://www.transifex.com/projects/p/sphinx-document-test_1_0/`` |
| 257 |
|
| 258 |
#. Create an API token to be used in the command-line. |
| 259 |
|
| 260 |
Go to your `Transifex API token`_ page and generate a token. |
| 261 |
Copy the generated token now, as you will not be able to see it again later. |
| 262 |
|
| 263 |
#. Set your Transifex API token in the user configuration file |
| 264 |
:file:`$HOME/.transifexrc`. |
| 265 |
|
| 266 |
.. code-block:: ini |
| 267 |
|
| 268 |
[https://app.transifex.com] |
| 269 |
rest_hostname = https://rest.api.transifex.com |
| 270 |
token = paste_your_api_token_here |
| 271 |
|
| 272 |
#. Alternatively, you can store your Transifex API token as the environment variable |
| 273 |
``TX_TOKEN``, which is recognized and used by :command:`tx`. |
| 274 |
|
| 275 |
.. code-block:: console |
| 276 |
|
| 277 |
$ export TX_TOKEN=paste_your_api_token_here |
| 278 |
|
| 279 |
#. Create the project's config file for :command:`tx` command. |
| 280 |
|
| 281 |
This process will create ``.tx/config`` in the current directory. |
| 282 |
|
| 283 |
.. code-block:: console |
| 284 |
|
| 285 |
$ cd /your/document/root |
| 286 |
$ tx init |
| 287 |
|
| 288 |
Successful creation of '.tx/config' file |
| 289 |
|
| 290 |
#. Upload pot files to Transifex service. |
| 291 |
|
| 292 |
Register pot files to ``.tx/config`` file using |
| 293 |
:command:`sphinx-intl update-txconfig-resources`, adjusting |
| 294 |
``--pot-dir`` value to your project's pot files' directory: |
| 295 |
|
| 296 |
.. code-block:: console |
| 297 |
|
| 298 |
$ cd /your/document/root |
| 299 |
$ sphinx-intl update-txconfig-resources --pot-dir _build/locale \ |
| 300 |
--transifex-organization-name=sphinx-document \ |
| 301 |
--transifex-project-name=sphinx-document-test_1_0 |
| 302 |
|
| 303 |
You can use the ``SPHINXINTL_TRANSIFEX_ORGANIZATION_NAME`` and |
| 304 |
``SPHINXINTL_TRANSIFEX_PROJECT_NAME`` environment variables |
| 305 |
instead of the respective command line arguments. |
| 306 |
|
| 307 |
.. seealso:: `sphinx-intl update-txconfig-resources documentation`_ |
| 308 |
|
| 309 |
and upload pot files: |
| 310 |
|
| 311 |
.. code-block:: console |
| 312 |
|
| 313 |
$ tx push -s |
| 314 |
# Getting info about resources |
| 315 |
|
| 316 |
sphinx-document-test_1_0.builders - Getting info |
| 317 |
sphinx-document-test_1_0.builders - Done |
| 318 |
|
| 319 |
# Pushing source files |
| 320 |
|
| 321 |
sphinx-document-test_1_0.builders - Uploading file |
| 322 |
sphinx-document-test_1_0.builders - Done |
| 323 |
|
| 324 |
#. Forward the translation on Transifex. |
| 325 |
|
| 326 |
.. TODO: write this section |
| 327 |
|
| 328 |
#. Pull translated po files and make translated HTML. |
| 329 |
|
| 330 |
Get translated catalogs and build mo files. For example, to build mo files |
| 331 |
for German (de): |
| 332 |
|
| 333 |
.. code-block:: console |
| 334 |
|
| 335 |
$ cd /your/document/root |
| 336 |
$ tx pull -l de |
| 337 |
# Getting info about resources |
| 338 |
|
| 339 |
sphinx-document-test_1_0.builders - Getting info |
| 340 |
sphinx-document-test_1_0.builders - Done |
| 341 |
|
| 342 |
# Pulling files |
| 343 |
|
| 344 |
sphinx-document-test_1_0.builders [de] - Pulling file |
| 345 |
sphinx-document-test_1_0.builders [de] - Creating download job |
| 346 |
sphinx-document-test_1_0.builders [de] - Done |
| 347 |
|
| 348 |
Invoke :command:`make html` (for BSD/GNU make) passing the language code: |
| 349 |
|
| 350 |
.. code-block:: console |
| 351 |
|
| 352 |
$ make -e SPHINXOPTS="-D language='de'" html |
| 353 |
|
| 354 |
That's all! |
| 355 |
|
| 356 |
.. tip:: Translating locally and on Transifex |
| 357 |
|
| 358 |
If you want to push all language's po files, you can be done by using |
| 359 |
:command:`tx push -t` command. Watch out! This operation overwrites |
| 360 |
translations in Transifex. |
| 361 |
|
| 362 |
In other words, if you have updated each in the service and local po files, |
| 363 |
it would take much time and effort to integrate them. |
| 364 |
|
| 365 |
|
| 366 |
Using Weblate service for team translation |
| 367 |
------------------------------------------ |
| 368 |
|
| 369 |
Read more in `Weblate's documentation`_. |
| 370 |
|
| 371 |
|
| 372 |
Contributing to Sphinx reference translation |
| 373 |
-------------------------------------------- |
| 374 |
|
| 375 |
The recommended way for new contributors to translate Sphinx reference is to |
| 376 |
join the translation team on Transifex. |
| 377 |
|
| 378 |
There is a `Sphinx translation page`_ for Sphinx (master) documentation. |
| 379 |
|
| 380 |
1. Login to Transifex_ service. |
| 381 |
2. Go to the `"Sphinx's documentation" translation project |
| 382 |
<https://app.transifex.com/sphinx-doc/sphinx-doc/>`__. |
| 383 |
3. Click ``Request language`` and fill form. |
| 384 |
4. Wait acceptance by Transifex sphinx translation maintainers. |
| 385 |
5. (After acceptance) Translate on Transifex. |
| 386 |
|
| 387 |
Detail is here: |
| 388 |
https://help.transifex.com/en/articles/6248698-getting-started-as-a-translator |
| 389 |
|
| 390 |
|
| 391 |
Translation progress and statistics |
| 392 |
----------------------------------- |
| 393 |
|
| 394 |
.. versionadded:: 7.1.0 |
| 395 |
|
| 396 |
During the rendering process, |
| 397 |
Sphinx marks each translatable node with a ``translated`` attribute, |
| 398 |
indicating if a translation was found for the text in that node. |
| 399 |
|
| 400 |
The :confval:`translation_progress_classes` configuration value |
| 401 |
can be used to add a class to each element, |
| 402 |
depending on the value of the ``translated`` attribute. |
| 403 |
|
| 404 |
The ``|translation progress|`` substitution can be used to display the |
| 405 |
percentage of nodes that have been translated on a per-document basis. |
| 406 |
|
| 407 |
.. rubric:: Footnotes |
| 408 |
|
| 409 |
.. [1] See the `GNU gettext utilities |
| 410 |
<https://www.gnu.org/software/gettext/manual/gettext.html#Introduction>`_ |
| 411 |
for details on that software suite. |
| 412 |
.. [2] Because nobody expects the Spanish Inquisition! |
| 413 |
|
| 414 |
.. _`Transifex CLI tool`: https://github.com/transifex/cli/ |
| 415 |
.. _`sphinx-intl`: https://pypi.org/project/sphinx-intl/ |
| 416 |
.. _Transifex: https://app.transifex.com/ |
| 417 |
.. _Weblate's documentation: https://docs.weblate.org/en/latest/devel/sphinx.html |
| 418 |
.. _`Sphinx translation page`: https://explore.transifex.com/sphinx-doc/sphinx-doc/ |
| 419 |
.. _`Transifex Client documentation`: https://developers.transifex.com/docs/using-the-client |
| 420 |
.. _`Transifex API token`: https://app.transifex.com/user/settings/api/ |
| 421 |
.. _`sphinx-intl update-txconfig-resources documentation`: https://sphinx-intl.readthedocs.io/en/master/refs.html#sphinx-intl-update-txconfig-resources |