From fb808af2f681232c14b15f39149ff2e190dfcaca Mon Sep 17 00:00:00 2001 From: benselme Date: Thu, 8 Jan 2015 19:40:28 -0500 Subject: [PATCH] More stuff merged from mitsuhiko's cldr-24 branch --- babel/plural.py | 21 ++++++++++++++++++++- tests/test_plural.py | 5 +++++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/babel/plural.py b/babel/plural.py index 297a4228..ce6659b4 100644 --- a/babel/plural.py +++ b/babel/plural.py @@ -52,6 +52,8 @@ class PluralRule(object): found = set() self.abstract = [] for key, expr in sorted(list(rules)): + if key == 'other': + continue if key not in _plural_tags: raise ValueError('unknown tag %r' % key) elif key in found: @@ -450,6 +452,11 @@ class _Compiler(object): return getattr(self, 'compile_' + op)(*args) compile_n = lambda x: 'n' + compile_i = lambda x: 'i' + compile_v = lambda x: 'v' + compile_w = lambda x: 'w' + compile_f = lambda x: 'f' + compile_t = lambda x: 't' compile_value = lambda x, v: str(v) compile_and = _binary_compiler('(%s && %s)') compile_or = _binary_compiler('(%s || %s)') @@ -504,18 +511,30 @@ class _GettextCompiler(_Compiler): class _JavaScriptCompiler(_GettextCompiler): """Compiles the expression to plain of JavaScript.""" + # XXX: presently javascript does not support any of the + # fraction support and basically only deals with integers. + compile_i = lambda x: 'parseInt(n, 10)' + compile_v = lambda x: '0' + compile_w = lambda x: '0' + compile_f = lambda x: '0' + compile_t = lambda x: '0' + def compile_relation(self, method, expr, range_list): code = _GettextCompiler.compile_relation( self, method, expr, range_list) if method == 'in': expr = self.compile(expr) - code = '(parseInt(%s) == %s && %s)' % (expr, expr, code) + code = '(parseInt(%s, 10) == %s && %s)' % (expr, expr, code) return code class _UnicodeCompiler(_Compiler): """Returns a unicode pluralization rule again.""" + # XXX: this currently spits out the old syntax instead of the new + # one. We can change that, but it will break a whole bunch of stuff + # for users I suppose. + compile_is = _binary_compiler('%s is %s') compile_isnot = _binary_compiler('%s is not %s') compile_and = _binary_compiler('%s and %s') diff --git a/tests/test_plural.py b/tests/test_plural.py index 489c0efc..166e2276 100644 --- a/tests/test_plural.py +++ b/tests/test_plural.py @@ -27,6 +27,11 @@ def test_plural_rule(): assert rule.rules == {'one': 'n is 1'} +def test_plural_other_is_ignored(): + rule = plural.PluralRule({'one': 'n is 1', 'other': '@integer 2'}) + assert rule(1) == 'one' + + def test_to_javascript(): assert (plural.to_javascript({'one': 'n is 1'}) == "(function(n) { return (n == 1) ? 'one' : 'other'; })") -- 2.47.2