jenkins-bot has submitted this change and it was merged. ( https://gerrit.wikimedia.org/r/502536 )
Change subject: [bugfix] Take the last plural entry if we don't have enough ......................................................................
[bugfix] Take the last plural entry if we don't have enough
As noted in https://translatewiki.net/wiki/Plural#Plural_syntax_in_MediaWiki if the number of forms written is less than the number of forms required by the plural rules of the language, the last available form will be used for all missing forms.
Bug: T219097 Bug: T99057 Change-Id: I7bbdd93a0b753e19123afe8ae956437416ec86c1 --- M pywikibot/i18n.py M tests/i18n_tests.py 2 files changed, 30 insertions(+), 7 deletions(-)
Approvals: Dvorapa: Looks good to me, but someone else must approve D3r1ck01: Looks good to me, approved jenkins-bot: Verified
diff --git a/pywikibot/i18n.py b/pywikibot/i18n.py index ca6168d..092395a 100644 --- a/pywikibot/i18n.py +++ b/pywikibot/i18n.py @@ -499,11 +499,9 @@ assert index == 0
if index >= len(plural_entries): - raise IndexError( - 'language "{}" requires {} plural variants for "{}" but ' - 'only {} ("{}") provided'.format( - code, needed, selector, len(plural_entries), - '", "'.join(plural_entries))) + # take the last entry in that case, see + # https://translatewiki.net/wiki/Plural#Plural_syntax_in_MediaWiki + index = -1 return plural_entries[index]
assert isinstance(parameters, Mapping), \ diff --git a/tests/i18n_tests.py b/tests/i18n_tests.py index 3b700fe..1ee14f8 100644 --- a/tests/i18n_tests.py +++ b/tests/i18n_tests.py @@ -452,8 +452,11 @@ 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}) + + # two variants expected but only one given + self.assertEqual( + i18n._extract_plural('en', '{{PLURAL:foo|one}}', {'foo': 0}), + 'one')
def test_specific(self): """Test using a specific plural.""" @@ -466,6 +469,28 @@ {'foo': 12}), 'dozen')
+ def test_more(self): + """Test the number of plurals are more than expected.""" + test = [(0, 2), (1, 0), (2, 1), (3, 2), (4, 2), (7, 2), (8, 3)] + for num, result in test: + self.assertEqual( + i18n._extract_plural( + 'cy', + '{{PLURAL:num|0|1|2|3|4|5}}', + {'num': num}), + str(result)) + + def test_less(self): + """Test the number of plurals are less than expected.""" + test = [(0, 2), (1, 0), (2, 1), (3, 2), (4, 2), (7, 2), (8, 3)] + for num, result in test: + self.assertEqual( + i18n._extract_plural( + 'cy', + '{{PLURAL:num|0|1}}', + {'num': num}), + str(min(result, 1))) +
if __name__ == '__main__': # pragma: no cover try: