Mercurial > genshi > mirror
changeset 873:4e8100e83bdd trunk
Any `py:strip` involved on `i18n:singular` or `i18n:plural` is now handled separately, ie, if one has `py:strip` on a `i18n:singular` element or directive and the plural form is the one chosen, no stripping will be performed. Refs #371.
author | palgarvio |
---|---|
date | Tue, 23 Feb 2010 18:58:47 +0000 |
parents | 21392d1af4d4 |
children | 49614704bbc7 |
files | genshi/filters/i18n.py genshi/filters/tests/i18n.py |
diffstat | 2 files changed, 81 insertions(+), 14 deletions(-) [+] |
line wrap: on
line diff
--- a/genshi/filters/i18n.py +++ b/genshi/filters/i18n.py @@ -343,21 +343,19 @@ dngettext = ctxt.get('_i18n.dngettext') if not dngettext: dngettext = lambda d, s, p, n: ngettext(s, p, n) - for kind, event, pos in stream: if kind is SUB: subdirectives, substream = event - strip_directive_present = [] - for idx, subdirective in enumerate(subdirectives): - if isinstance(subdirective, StripDirective): - # XXX: Any strip directive should be applied AFTER the - # event's have been translated and singular or plural - # form has been chosen. So, by having py:strip on - # an i18n:singular element is as if i18n:plural had it - # too. - strip_directive_present.append(subdirectives.pop(idx)) if isinstance(subdirectives[0], SingularDirective) and not singular_stream: + strip_directive_present = [] + for idx, subdirective in enumerate(subdirectives): + if isinstance(subdirective, StripDirective): + # Any strip directive should be applied AFTER + # the event's have been translated. + strip_directive_present.append( + subdirectives.pop(idx) + ) # Apply directives to update context singular_stream = list(_apply_directives(substream, subdirectives, @@ -368,14 +366,30 @@ strip_directive_present, ctxt, vars) ) + del strip_directive_present new_stream.append((MSGBUF, (), ('', -1))) # msgbuf place holder singular_msgbuf = ctxt.get('_i18n.choose.SingularDirective') elif isinstance(subdirectives[0], PluralDirective) and not plural_stream: + strip_directive_present = [] + for idx, subdirective in enumerate(subdirectives): + if isinstance(subdirective, StripDirective): + # Any strip directive should be applied AFTER + # the event's have been translated. + strip_directive_present.append( + subdirectives.pop(idx) + ) # Apply directives to update context plural_stream = list(_apply_directives(substream, subdirectives, ctxt, vars)) + if strip_directive_present: + plural_stream = list( + _apply_directives(plural_stream, + strip_directive_present, + ctxt, vars) + ) + del strip_directive_present plural_msgbuf = ctxt.get('_i18n.choose.PluralDirective') else: new_stream.append((kind, event, pos)) @@ -386,14 +400,29 @@ ngettext = lambda s, p, n: dngettext(ctxt.get('_i18n.domain'), s, p, n) + # XXX: should we test which form was chosen like this!?!?!? + # There should be no match in any catalogue for these singular and + # plural test strings + singular_test = ur'O\x85\xbe\xa9\xa8az\xc3?\xe6\xa1\x02n\x84\x93' + plural_test = ur'\xcc\xfb+\xd3Pn\x9d\tT\xec\x1d\xda\x1a\x88\x00' + translation = ngettext(singular_test, plural_test, + self.numeral.evaluate(ctxt)) + if translation==singular_test: + chosen_msgbuf = singular_msgbuf + chosen_stream = singular_stream + else: + chosen_msgbuf = plural_msgbuf + chosen_stream = plural_stream + del singular_test, plural_test, translation + for kind, data, pos in new_stream: if kind is MSGBUF: - for skind, sdata, spos in singular_stream: + for skind, sdata, spos in chosen_stream: if skind is MSGBUF: translation = ngettext(singular_msgbuf.format(), plural_msgbuf.format(), self.numeral.evaluate(ctxt)) - for event in singular_msgbuf.translate(translation): + for event in chosen_msgbuf.translate(translation): yield event else: yield skind, sdata, spos
--- a/genshi/filters/tests/i18n.py +++ b/genshi/filters/tests/i18n.py @@ -931,6 +931,36 @@ <p>FooBars</p> <p>FooBar</p> </html>""", tmpl.generate(one=1, two=2).render()) + + def test_translate_i18n_choose_as_directive_singular_and_plural_with_strip(self): + tmpl = MarkupTemplate("""<html xmlns:py="http://genshi.edgewall.org/" + xmlns:i18n="http://genshi.edgewall.org/i18n"> + <i18n:choose numeral="two"> + <p i18n:singular="" py:strip="">FooBar Singular with Strip</p> + <p i18n:plural="">FooBars Plural without Strip</p> + </i18n:choose> + <i18n:choose numeral="two"> + <p i18n:singular="">FooBar singular without strip</p> + <p i18n:plural="" py:strip="">FooBars plural with strip</p> + </i18n:choose> + <i18n:choose numeral="one"> + <p i18n:singular="">FooBar singular without strip</p> + <p i18n:plural="" py:strip="">FooBars plural with strip</p> + </i18n:choose> + <i18n:choose numeral="one"> + <p i18n:singular="" py:strip="">FooBar singular with strip</p> + <p i18n:plural="">FooBars plural without strip</p> + </i18n:choose> + </html>""") + translations = DummyTranslations() + translator = Translator(translations) + translator.setup(tmpl) + self.assertEqual("""<html> + <p>FooBars Plural without Strip</p> + FooBars plural with strip + <p>FooBar singular without strip</p> + FooBar singular with strip + </html>""", tmpl.generate(one=1, two=2).render()) def test_translate_i18n_choose_plural_singular_as_directive(self): # Ticket 371 @@ -1437,6 +1467,10 @@ <p i18n:singular="" py:strip="">Foo $fname $lname</p> <p i18n:plural="">Foos $fname $lname</p> </div> + <div i18n:choose="one; fname, lname"> + <p i18n:singular="" py:strip="">Foo $fname $lname</p> + <p i18n:plural="">Foos $fname $lname</p> + </div> </html>""") translations = DummyTranslations({ ('Foo %(fname)s %(lname)s', 0): 'Voh %(fname)s %(lname)s', @@ -1448,9 +1482,13 @@ translator.setup(tmpl) self.assertEqual("""<html> <div> - Vohs John Doe + <p>Vohs John Doe</p> </div> - </html>""", tmpl.generate(two=2, fname='John', lname='Doe').render()) + <div> + Voh John Doe + </div> + </html>""", tmpl.generate( + one=1, two=2, fname='John',lname='Doe').render()) def test_translate_i18n_choose_and_plural_with_py_strip(self): tmpl = MarkupTemplate("""<html xmlns:py="http://genshi.edgewall.org/"