From fd8a02b786c647047be7ac98a77060a2e5ac19cc Mon Sep 17 00:00:00 2001 From: anlonely Date: Sun, 6 Dec 2026 14:13:15 +0800 Subject: [PATCH] docs: explain how to combine translations with different plural rules --- docs/support.rst | 33 +++++++++++++++++++++++++++++---- 1 file changed, 29 insertions(+), 4 deletions(-) diff --git a/docs/support.rst b/docs/support.rst index baafc5451..254be56ec 100644 --- a/docs/support.rst +++ b/docs/support.rst @@ -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)