views:

545

answers:

1

Given a gettext Plural-Forms line, general a few example values for each n. I'd like this feature for the web interface for my site's translators, so that they know which plural form to put where. For example, given:

"Plural-Forms: nplurals=3; plural=n%10==1 && n%100!=11 ? 0 : n%10>=2 && n%" "10<=4 && (n%100<10 || n%100>=20) ? 1 : 2;\n"

... I want the first text field to be labeled "1, 21..", then "2, 3, 4...", then "5, 6..." (not sure if this is exactly right, but you get the idea.)

Right now the best thing I can come up with is to parse the expression somehow, then iterate x from 0 to 100 and see what n it produces. This isn't guaranteed to work (what if the lowest x is over 100 for some language?) but it's probably good enough. Any better ideas or existing Python code?

+1  A: 

Given that it's late, I'll bite.

The following solution is hacky, and relies on converting your plural form to python code that can be evaluated (basically converting the x ? y : z statements to the python x and y or z equivalent, and changing &&/|| to and/or)

I'm not sure if your plural form rule is a contrived example, and I don't understand what you mean with your first text field, but I'm sure you'll get where I'm going with my example solution:

# -*- Mode: Python -*-
# vi:si:et:sw=4:sts=4:ts=4

p = "Plural-Forms: nplurals=3; plural=n%10==1 && n%100!=11 ? 0 : n%10>=2 && n%10<=4 && (n%100<10 || n%100>=20) ? 1 : 2;\n"

# extract rule
import re
matcher = re.compile('plural=(.*);')
match = matcher.search(p)
rule = match.expand("\\1")

# convert rule to python syntax
oldrule = None
while oldrule != rule:
    oldrule = rule
    rule = re.sub('(.*)\?(.*):(.*)', r'(\1) and (\2) or (\3)', oldrule)

rule = re.sub('&&', 'and', rule)
rule = re.sub('\|\|', 'or', rule)

for n in range(40):
    code = "n = %d" % n
    print n, eval(rule)
Thomas Vander Stichele
You have a very good approach. I'd thought of converting the ? operator to Python's "x if y else z" format, but that would require more parsing.The code in this solution is susceptible to the main problem of "x and y or z", which is "what if y is false?", but converting 0 to 'zero' works. Hmmm....
Andrew B.