def plural_to_gettext(rule): """This is a copy of the code of ``babel.plural.to_gettext``. We need to use a custom version, because the original only returns a full plural_forms string, which the Babel catalog object does not allow us to assign to anything. Instead, we need the expr and the plural count separately. See http://babel.edgewall.org/ticket/291. """ from babel.plural import (PluralRule, _fallback_tag, _plural_tags, _GettextCompiler) rule = PluralRule.parse(rule) used_tags = rule.tags | set([_fallback_tag]) _compile = _GettextCompiler().compile _get_index = [tag for tag in _plural_tags if tag in used_tags].index expr = ['('] for tag, ast in rule.abstract: expr.append('%s ? %d : ' % (_compile(ast), _get_index(tag))) expr.append('%d)' % _get_index(_fallback_tag)) return len(used_tags), ''.join(expr)
def plural_to_gettext(rule): """This is a copy of the code of ``babel.plural.to_gettext``. We need to use a custom version, because the original only returns a full plural_forms string, which the Babel catalog object does not allow us to assign to anything. Instead, we need the expr and the plural count separately. See http://babel.edgewall.org/ticket/291. """ from babel.plural import PluralRule, _fallback_tag, _plural_tags, _GettextCompiler rule = PluralRule.parse(rule) used_tags = rule.tags | set([_fallback_tag]) _compile = _GettextCompiler().compile _get_index = [tag for tag in _plural_tags if tag in used_tags].index expr = ["("] for tag, ast in rule.abstract: expr.append("%s ? %d : " % (_compile(ast), _get_index(tag))) expr.append("%d)" % _get_index(_fallback_tag)) return len(used_tags), "".join(expr)