jenkins-bot has submitted this change and it was merged.
Change subject: [FEAT] i18n: Support specific plural messages ......................................................................
[FEAT] i18n: Support specific plural messages
This adds support for translations which use specific plural texts for specific values.
Bug: T115297 Change-Id: I5372ed4ef3343cbbab1b33efcee0cf707e50ae24 --- M pywikibot/i18n.py M tests/i18n_tests.py 2 files changed, 44 insertions(+), 1 deletion(-)
Approvals: John Vandenberg: Looks good to me, approved jenkins-bot: Verified
diff --git a/pywikibot/i18n.py b/pywikibot/i18n.py index 3bd9e9e..7259abd 100644 --- a/pywikibot/i18n.py +++ b/pywikibot/i18n.py @@ -356,11 +356,24 @@ 'an int', 1) num = int(num)
+ plural_entries = [] + specific_entries = {} + for number, plural in re.findall(r'|?(?: *(\d+) *= *)?([^|]+)', + variants): + if number: + specific_entries[int(number)] = plural + else: + assert not specific_entries, \ + 'generic entries defined after specific in "{0}"'.format(variants) + plural_entries += [plural] + + if num in specific_entries: + return specific_entries[num] + index = plural_value(num) if rule['nplurals'] == 1: assert index == 0
- plural_entries = variants.split('|') if index >= len(plural_entries): raise IndexError( 'requested plural {0} for {1} but only {2} ("{3}") ' diff --git a/tests/i18n_tests.py b/tests/i18n_tests.py index 0ffb661..9b68faf 100644 --- a/tests/i18n_tests.py +++ b/tests/i18n_tests.py @@ -413,6 +413,36 @@ self.assertIn('dummy output: ', self.output_text)
+class TestExtractPlural(TestCase): + + """Test extracting plurals from a dummy string.""" + + net = False + + def test_standard(self): + """Test default usage using a dict and no specific plurals.""" + self.assertEqual( + i18n._extract_plural('en', '{{PLURAL:foo|one|other}}', {'foo': 42}), + 'other') + self.assertEqual( + i18n._extract_plural('en', '{{PLURAL:foo|one|other}}', {'foo': 1}), + 'one') + self.assertEqual( + i18n._extract_plural('en', '{{PLURAL:foo|one|other}}', {'foo': 0}), + 'other') + + def test_specific(self): + """Test using a specific plural.""" + self.assertEqual( + i18n._extract_plural('en', '{{PLURAL:foo|one|other|12=dozen}}', + {'foo': 42}), + 'other') + self.assertEqual( + i18n._extract_plural('en', '{{PLURAL:foo|one|other|12=dozen}}', + {'foo': 12}), + 'dozen') + + if __name__ == '__main__': try: unittest.main()
pywikibot-commits@lists.wikimedia.org