Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 29 additions & 4 deletions docs/support.rst
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,35 @@ system.

To support this usage pattern, Babel provides a ``Translations`` class that is
derived from the ``GNUTranslations`` class in the ``gettext`` module. This
class adds a ``merge()`` method that takes another ``Translations`` instance,
and merges the content of the latter into the main catalog:
class adds a ``merge()`` method that takes another ``Translations`` instance
and merges its messages into the main catalog. For example, if both
components have French translations installed, you can combine them as
follows:

.. code-block:: python

translations = Translations.load('main')
translations.merge(Translations.load('plugin1'))
from babel.support import Translations

translations = Translations.load('main', locales='fr')
translations.merge(Translations.load('plugin1', locales='fr'))

The plugin's translations take precedence for messages in both catalogs.
Use ``merge()`` when the catalogs share the same plural rule. The merged
catalog uses the main catalog's rule for all messages. For example, for
a count of 2, a French catalog generated by Babel 2.19 selects index 2,
while one generated by Babel 2.18 selects index 1. If the plugin only
provides forms 0 and 1, looking up index 2 can return the untranslated
source text.

If the plural rules differ, keep the catalogs separate and use
``add_fallback()`` so that each catalog applies its own rule. To give the
plugin the same priority as above, look up messages in the plugin first
and fall back to the main catalog for missing translations:

.. code-block:: python

from babel.support import Translations

translations = Translations.load('plugin1', locales='fr')
translations.add_fallback(Translations.load('main', locales='fr'))
label = translations.ngettext('ticket', 'tickets', 2)