]> git.ipfire.org Git - thirdparty/babel.git/commitdiff
Fix read_pofile handling of missing plurals 452/head
authorMichael Birtwell <michael.birtwell@starleaf.com>
Wed, 12 Oct 2016 16:46:06 +0000 (17:46 +0100)
committerMichael Birtwell <michael.birtwell@starleaf.com>
Mon, 21 Nov 2016 14:32:49 +0000 (14:32 +0000)
babel/messages/pofile.py
tests/messages/test_pofile.py

index 47db8a9f74c0a931730cf7dff3005fcef2f87ba5..696ec3e9736f50405357b9bd95eb274376e9b7d8 100644 (file)
@@ -135,13 +135,13 @@ class PoFileParser(object):
         else:
             msgid = self.messages[0].denormalize()
         if isinstance(msgid, (list, tuple)):
-            string = []
-            for idx in range(self.catalog.num_plurals):
-                try:
-                    string.append(self.translations[idx])
-                except IndexError:
-                    string.append((idx, ''))
-            string = tuple([t[1].denormalize() for t in string])
+            string = ['' for _ in range(self.catalog.num_plurals)]
+            for idx, translation in self.translations:
+                if idx >= self.catalog.num_plurals:
+                    self._invalid_pofile("", self.offset, "msg has more translations than num_plurals of catalog")
+                    continue
+                string[idx] = translation.denormalize()
+            string = tuple(string)
         else:
             string = self.translations[0][1].denormalize()
         if self.context:
index 64bf7148cfc19ea3c9d7afc2e48562d39602c26a..cef69138725717746abcc1be91efd2d64f7b7f60 100644 (file)
@@ -383,6 +383,46 @@ msgstr[1] "Vohs [text]"''')
         self.assertEqual("Voh [text]", message.string[0])
         self.assertEqual("Vohs [text]", message.string[1])
 
+    def test_missing_plural(self):
+        buf = StringIO('''\
+msgid ""
+msgstr ""
+"Plural-Forms: nplurals=3; plural=(n < 2) ? n : 2\n"
+
+msgid "foo"
+msgid_plural "foos"
+msgstr[0] "Voh [text]"
+msgstr[1] "Vohs [text]"
+''')
+        catalog = pofile.read_po(buf, locale='nb_NO')
+        self.assertEqual(1, len(catalog))
+        self.assertEqual(3, catalog.num_plurals)
+        message = catalog['foo']
+        self.assertEqual(3, len(message.string))
+        self.assertEqual("Voh [text]", message.string[0])
+        self.assertEqual("Vohs [text]", message.string[1])
+        self.assertEqual("", message.string[2])
+
+    def test_missing_plural_in_the_middle(self):
+        buf = StringIO('''\
+msgid ""
+msgstr ""
+"Plural-Forms: nplurals=3; plural=(n < 2) ? n : 2\n"
+
+msgid "foo"
+msgid_plural "foos"
+msgstr[0] "Voh [text]"
+msgstr[2] "Vohs [text]"
+''')
+        catalog = pofile.read_po(buf, locale='nb_NO')
+        self.assertEqual(1, len(catalog))
+        self.assertEqual(3, catalog.num_plurals)
+        message = catalog['foo']
+        self.assertEqual(3, len(message.string))
+        self.assertEqual("Voh [text]", message.string[0])
+        self.assertEqual("", message.string[1])
+        self.assertEqual("Vohs [text]", message.string[2])
+
 
 class WritePoTestCase(unittest.TestCase):