Bug 748909: Accept plural rules without generic case in .properties files #414

Merged
merged 1 commit into from May 2, 2012
@@ -11,3 +11,6 @@ downloadsCount[one]=one download
pluralTest=fallback to other
pluralTest[zero]=optional zero form
+
+explicitPlural[one]=one
+explicitPlural[other]=other
@@ -126,6 +126,16 @@ exports.testEnUsLocaleName = function(test) {
"fallback to other",
"If the specific plural form is missing, we fallback to 'other'");
+ // Ensure that we can omit specifying the generic key without [other]
+ // key[one] = ...
+ // key[other] = ... # Instead of `key = ...`
+ test.assertEqual(_("explicitPlural", 1),
+ "one",
+ "PluralForm form can be omitting generic key [i.e. without ...[other] at end of key)");
+ test.assertEqual(_("explicitPlural", 10),
+ "other",
+ "PluralForm form can be omitting generic key [i.e. without ...[other] at end of key)");
+
loader.unload();
resetLocale();
}
@@ -91,13 +91,17 @@ def normalize_plural(path, pairs):
continue
main_key = m.group(1)
plural_form = m.group(2)
+ # Allows not specifying a generic key (i.e a key without [form])
if not main_key in pairs:
- raise MalformedLocaleFileError(
- 'Following locale file is not a valid UTF-8 file: %s\n'
- 'This plural form doesn\'t have a matching generic form:\n'
- '%s\n'
- 'You have to defined following key:\n%s'
- % (path, key, main_key))
+ pairs[main_key] = {}
+ # Ensure that we always have the [other] form
+ if not main_key + "[other]" in pairs:
+ raise MalformedLocaleFileError(
+ 'Following locale file is not a valid UTF-8 file: %s\n'
+ 'This plural form doesn\'t have a matching `%s[other]` form:\n'
+ '%s\n'
+ 'You have to defined following key:\n%s'
+ % (path, main_key, key, main_key))
# convert generic form into an object if it is still a string
if isinstance(pairs[main_key], unicode):
pairs[main_key] = {"other": pairs[main_key]}
@@ -37,10 +37,19 @@ def test_parse(self):
# With multiline string, left spaces are stripped ...
"some= spaces\\", " are\\ ", " stripped ",
# ... but not right spaces, except the last line!
- "but=not \\", "all of \\", " them "
+ "but=not \\", "all of \\", " them ",
+
+ # Explicit [other] plural definition
+ "explicitPlural[one] = one",
+ "explicitPlural[other] = other",
+
+ # Implicit [other] plural definition
+ "implicitPlural[one] = one",
+ "implicitPlural = other", # This key is the [other] one
]
- # Ensure that lines end with a `\n`
- lines = [l + "\n" for l in lines]
+ # Ensure that all lines end with a `\n`
+ # And that strings are unicode ones (parser code relies on it)
+ lines = [unicode(l + "\n") for l in lines]
pairs = parse(lines)
expected = {
"sharp": "#can be in value",
@@ -55,15 +64,24 @@ def test_parse(self):
"multi": "linevalue",
"some": "spacesarestripped",
- "but": "not all of them"
+ "but": "not all of them",
+
+ "implicitPlural": {
+ "one": "one",
+ "other": "other"
+ },
+ "explicitPlural": {
+ "one": "one",
+ "other": "other"
+ },
}
self.assertEqual(pairs, expected)
def test_exceptions(self):
self.failUnlessRaises(MalformedLocaleFileError, parse,
["invalid line with no key value"])
self.failUnlessRaises(MalformedLocaleFileError, parse,
- ["plural[one]=plural with no generic value"])
+ ["plural[one]=plural with no [other] value"])
self.failUnlessRaises(MalformedLocaleFileError, parse,
["multiline with no last empty line=\\"])
self.failUnlessRaises(MalformedLocaleFileError, parse,