]> git.ipfire.org Git - thirdparty/babel.git/commitdiff
Allow passthrough of xgettext's plural templates weird-plurals 1185/head
authorAarni Koskela <akx@iki.fi>
Mon, 3 Feb 2025 09:34:27 +0000 (11:34 +0200)
committerAarni Koskela <akx@iki.fi>
Mon, 3 Feb 2025 09:34:27 +0000 (11:34 +0200)
Fixes #1154

babel/messages/catalog.py
tests/messages/test_pofile.py

index 954dad0e3d09cc475e9b68f9f4e678915ea4bb54..856ccbc6f5da5ee4d65447af0eb106175f4f831f 100644 (file)
@@ -398,8 +398,9 @@ class Catalog:
 
         # Dictionary of obsolete messages
         self.obsolete: dict[str | tuple[str, str], Message] = {}
-        self._num_plurals = None
-        self._plural_expr = None
+
+        self._num_plurals: str | int | None = None
+        self._plural_expr: str | None = None
 
     def _set_locale(self, locale: Locale | str | None) -> None:
         if locale is None:
@@ -538,7 +539,8 @@ class Catalog:
                     self.charset = params['charset'].lower()
             elif name == 'plural-forms':
                 params = parse_separated_header(f" ;{value}")
-                self._num_plurals = int(params.get('nplurals', 2))
+                nplurals = params.get('nplurals')
+                self._num_plurals = int(nplurals, 10) if nplurals.isdigit() else nplurals
                 self._plural_expr = params.get('plural', '(n != 1)')
             elif name == 'pot-creation-date':
                 self.creation_date = _parse_datetime_header(value)
@@ -600,15 +602,16 @@ class Catalog:
     """)
 
     @property
-    def num_plurals(self) -> int:
+    def num_plurals(self) -> int | str:
         """The number of plurals used by the catalog or locale.
 
+        If read from a catalog template, this may be a string.
+
         >>> Catalog(locale='en').num_plurals
         2
         >>> Catalog(locale='ga').num_plurals
         5
-
-        :type: `int`"""
+        """
         if self._num_plurals is None:
             num = 2
             if self.locale:
index 2bcc3df8d9cadacbf16a19941114b26ecaae195d..f8eabfad1eb4780a7cacced011fa1a9457999aa2 100644 (file)
@@ -1096,3 +1096,17 @@ def test_issue_1134(case: str, abort_invalid: bool):
         output = pofile.read_po(buf)
         assert len(output) == 1
         assert output["foo"].string in ((''), ('', ''))
+
+
+def test_issue_1154():
+    # Via `echo 'ngettext("Hello World!", "Hello Worlds!", 3);' | xgettext --output=- - --language=C`,
+    # minimized for reproducing the issue.
+    template = """
+msgid ""
+msgstr ""
+"Plural-Forms: nplurals=INTEGER; plural=EXPRESSION;\n"
+    """.strip()
+    cat = pofile.read_po(StringIO(template))
+    assert cat.num_plurals == "INTEGER"
+    assert cat.plural_expr == "EXPRESSION"
+    assert cat.plural_forms == "nplurals=INTEGER; plural=EXPRESSION;"