XZise has uploaded a new change for review.
https://gerrit.wikimedia.org/r/245886
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(-)
git pull ssh://gerrit.wikimedia.org:29418/pywikibot/core
refs/changes/86/245886/1
diff --git a/pywikibot/i18n.py b/pywikibot/i18n.py
index 3bd9e9e..944958b 100644
--- a/pywikibot/i18n.py
+++ b/pywikibot/i18n.py
@@ -356,11 +356,24 @@
'an int', 1)
num = int(num)
+ entries = [None] + re.split(r'\|(?: *(\d+) *= *)?', variants)
+ plural_entries = []
+ specific_entries = {}
+ for index in range(0, len(entries), 2):
+ if entries[index] is not None:
+ specific_entries[int(entries[index])] = entries[index + 1]
+ else:
+ assert not specific_entries, \
+ 'generic entries defined after specific in
"{0}"'.format(variants)
+ plural_entries += [entries[index + 1]]
+
+ 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()
--
To view, visit https://gerrit.wikimedia.org/r/245886
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: newchange
Gerrit-Change-Id: I5372ed4ef3343cbbab1b33efcee0cf707e50ae24
Gerrit-PatchSet: 1
Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-Owner: XZise <[email protected]>
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits