diff --git a/babel/messages/pofile.py b/babel/messages/pofile.py index 63401f10d..e3d1c3b79 100644 --- a/babel/messages/pofile.py +++ b/babel/messages/pofile.py @@ -163,12 +163,16 @@ def __init__( catalog: Catalog, ignore_obsolete: bool = False, abort_invalid: bool = False, + shared_msgids: dict[str, str] | None = None, ) -> None: self.catalog = catalog self.ignore_obsolete = ignore_obsolete self.counter = 0 self.offset = 0 self.abort_invalid = abort_invalid + # Identical message IDs share one string object; passing the same dict + # to several parsers shares them between catalogs too. + self._msgids: dict[str, str] = {} if shared_msgids is None else shared_msgids self._reset_message_state() def _reset_message_state(self) -> None: @@ -205,6 +209,10 @@ def _add_message(self) -> None: else: msgid = self.messages[0].denormalize() string = self.translations[0][1].denormalize() + if isinstance(msgid, tuple): + msgid = tuple(self._msgids.setdefault(part, part) for part in msgid) + else: + msgid = self._msgids.setdefault(msgid, msgid) msgctxt = self.context.denormalize() if self.context else None message = Message( msgid, @@ -384,6 +392,7 @@ def read_po( ignore_obsolete: bool = False, charset: str | None = None, abort_invalid: bool = False, + shared_msgids: dict[str, str] | None = None, ) -> Catalog: """Read messages from a ``gettext`` PO (portable object) file from the given file-like object (or an iterable of lines) and return a `Catalog`. @@ -430,9 +439,17 @@ def read_po( :param ignore_obsolete: whether to ignore obsolete messages in the input :param charset: the character set of the catalog. :param abort_invalid: abort read if po file is invalid + :param shared_msgids: an optional dict to share message ID strings through; + pass the same dict when reading several catalogs so + their identical message IDs are only stored once """ catalog = Catalog(locale=locale, domain=domain, charset=charset) - parser = PoFileParser(catalog, ignore_obsolete, abort_invalid=abort_invalid) + parser = PoFileParser( + catalog, + ignore_obsolete, + abort_invalid=abort_invalid, + shared_msgids=shared_msgids, + ) parser.parse(fileobj) return catalog diff --git a/tests/messages/test_pofile_read.py b/tests/messages/test_pofile_read.py index d17f5d4af..c40ab34ba 100644 --- a/tests/messages/test_pofile_read.py +++ b/tests/messages/test_pofile_read.py @@ -406,6 +406,53 @@ def test_with_context_two(): assert out_buf.getvalue().strip() == buf.getvalue().strip(), out_buf.getvalue() +def test_repeated_msgids_share_strings(): + buf = StringIO(r'''msgctxt "Menu" +msgid "Open" +msgstr "Ouvrir" + +msgctxt "Button" +msgid "Open" +msgstr "Ouvrir" + +msgctxt "Menu" +msgid "%d file" +msgid_plural "%d files" +msgstr[0] "%d fichier" +msgstr[1] "%d fichiers" + +msgctxt "Button" +msgid "%d file" +msgid_plural "%d files" +msgstr[0] "%d fichier" +msgstr[1] "%d fichiers" +''') + catalog = pofile.read_po(buf) + assert catalog.get('Open', context='Menu').id is catalog.get('Open', context='Button').id + menu_id = catalog.get('%d file', context='Menu').id + button_id = catalog.get('%d file', context='Button').id + assert menu_id == button_id == ('%d file', '%d files') + assert menu_id[0] is button_id[0] + assert menu_id[1] is button_id[1] + + +def test_shared_msgids_between_catalogs(): + po = r'''msgid "Open" +msgstr "Ouvrir" + +msgid "%d file" +msgid_plural "%d files" +msgstr[0] "%d fichier" +msgstr[1] "%d fichiers" +''' + shared = {} + fr = pofile.read_po(StringIO(po), locale='fr', shared_msgids=shared) + de = pofile.read_po(StringIO(po), locale='de', shared_msgids=shared) + assert fr['Open'].id is de['Open'].id + assert fr['%d file'].id[0] is de['%d file'].id[0] + assert fr['%d file'].id[1] is de['%d file'].id[1] + + def test_single_plural_form(): buf = StringIO(r'''msgid "foo" msgid_plural "foos"