jenkins-bot has submitted this change and it was merged.
Change subject: [bugfix] Enable empty plural variant ......................................................................
[bugfix] Enable empty plural variant
- regex for variants does not match empty plural variant. - Some tests added.
Bug: T125402 Bug: T99057 Change-Id: I1e77cbe96b6b8861d05dc0c83d3cde1b636b54fa --- M pywikibot/i18n.py M tests/i18n_tests.py 2 files changed, 18 insertions(+), 2 deletions(-)
Approvals: Xqt: Looks good to me, approved jenkins-bot: Verified
diff --git a/pywikibot/i18n.py b/pywikibot/i18n.py index 5739837..f529aba 100644 --- a/pywikibot/i18n.py +++ b/pywikibot/i18n.py @@ -17,7 +17,7 @@ messages. See L{twntranslate} for more information on the messages. """ # -# (C) Pywikibot team, 2004-2015 +# (C) Pywikibot team, 2004-2016 # # Distributed under the terms of the MIT license. # @@ -357,7 +357,9 @@
plural_entries = [] specific_entries = {} - for number, plural in re.findall(r'|?(?: *(\d+) *= *)?([^|]+)', + # A plural entry can not start at the end of the variants list, + # and must end with | or the end of the variants list. + for number, plural in re.findall(r'(?!$)(?: *(\d+) *= *)?(.*?)(?:||$)', variants): if number: specific_entries[int(number)] = plural diff --git a/tests/i18n_tests.py b/tests/i18n_tests.py index 32f5f3a..22fd9b0 100644 --- a/tests/i18n_tests.py +++ b/tests/i18n_tests.py @@ -440,6 +440,20 @@ i18n._extract_plural('en', '{{PLURAL:foo|one|other}}', {'foo': 0}), 'other')
+ def test_empty_fields(self): + """Test default usage using a dict and no specific plurals.""" + self.assertEqual( + i18n._extract_plural('en', '{{PLURAL:foo||other}}', {'foo': 42}), + 'other') + self.assertEqual( + i18n._extract_plural('en', '{{PLURAL:foo||other}}', {'foo': 1}), + '') + self.assertEqual( + i18n._extract_plural('en', '{{PLURAL:foo|one|}}', {'foo': 1}), + 'one') + with self.assertRaises(IndexError): + i18n._extract_plural('en', '{{PLURAL:foo|one}}', {'foo': 0}) + def test_specific(self): """Test using a specific plural.""" self.assertEqual(