Edgewall Software

Changeset 1096 for trunk


Ignore:
Timestamp:
Feb 23, 2010, 7:58:47 PM (14 years ago)
Author:
palgarvio
Message:

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.

Location:
trunk/genshi/filters
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/genshi/filters/i18n.py

    r1095 r1096  
    344344        if not dngettext:
    345345            dngettext = lambda d, s, p, n: ngettext(s, p, n)
    346 
    347346        for kind, event, pos in stream:
    348347            if kind is SUB:
    349348                subdirectives, substream = event
    350                 strip_directive_present = []
    351                 for idx, subdirective in enumerate(subdirectives):
    352                     if isinstance(subdirective, StripDirective):
    353                         # XXX: Any strip directive should be applied AFTER the
    354                         # event's have been translated and singular or plural
    355                         # form has been chosen. So, by having py:strip on
    356                         # an i18n:singular element is as if i18n:plural had it
    357                         # too.
    358                         strip_directive_present.append(subdirectives.pop(idx))
    359349                if isinstance(subdirectives[0],
    360350                              SingularDirective) and not singular_stream:
     351                    strip_directive_present = []
     352                    for idx, subdirective in enumerate(subdirectives):
     353                        if isinstance(subdirective, StripDirective):
     354                            # Any strip directive should be applied AFTER
     355                            # the event's have been translated.
     356                            strip_directive_present.append(
     357                                subdirectives.pop(idx)
     358                            )
    361359                    # Apply directives to update context
    362360                    singular_stream = list(_apply_directives(substream,
     
    369367                                              ctxt, vars)
    370368                        )
     369                    del strip_directive_present
    371370                    new_stream.append((MSGBUF, (), ('', -1))) # msgbuf place holder
    372371                    singular_msgbuf = ctxt.get('_i18n.choose.SingularDirective')
    373372                elif isinstance(subdirectives[0],
    374373                                PluralDirective) and not plural_stream:
     374                    strip_directive_present = []
     375                    for idx, subdirective in enumerate(subdirectives):
     376                        if isinstance(subdirective, StripDirective):
     377                            # Any strip directive should be applied AFTER
     378                            # the event's have been translated.
     379                            strip_directive_present.append(
     380                                subdirectives.pop(idx)
     381                            )
    375382                    # Apply directives to update context
    376383                    plural_stream = list(_apply_directives(substream,
    377384                                                           subdirectives,
    378385                                                           ctxt, vars))
     386                    if strip_directive_present:
     387                        plural_stream = list(
     388                            _apply_directives(plural_stream,
     389                                              strip_directive_present,
     390                                              ctxt, vars)
     391                        )
     392                    del strip_directive_present
    379393                    plural_msgbuf = ctxt.get('_i18n.choose.PluralDirective')
    380394                else:
     
    387401                                                 s, p, n)
    388402
     403        # XXX: should we test which form was chosen like this!?!?!?
     404        # There should be no match in any catalogue for these singular and
     405        # plural test strings
     406        singular_test = ur'O\x85\xbe\xa9\xa8az\xc3?\xe6\xa1\x02n\x84\x93'
     407        plural_test = ur'\xcc\xfb+\xd3Pn\x9d\tT\xec\x1d\xda\x1a\x88\x00'
     408        translation = ngettext(singular_test, plural_test,
     409                               self.numeral.evaluate(ctxt))
     410        if translation==singular_test:
     411            chosen_msgbuf = singular_msgbuf
     412            chosen_stream = singular_stream
     413        else:
     414            chosen_msgbuf = plural_msgbuf
     415            chosen_stream = plural_stream
     416        del singular_test, plural_test, translation
     417
    389418        for kind, data, pos in new_stream:
    390419            if kind is MSGBUF:
    391                 for skind, sdata, spos in singular_stream:
     420                for skind, sdata, spos in chosen_stream:
    392421                    if skind is MSGBUF:
    393422                        translation = ngettext(singular_msgbuf.format(),
    394423                                               plural_msgbuf.format(),
    395424                                               self.numeral.evaluate(ctxt))
    396                         for event in singular_msgbuf.translate(translation):
     425                        for event in chosen_msgbuf.translate(translation):
    397426                            yield event
    398427                    else:
  • trunk/genshi/filters/tests/i18n.py

    r1094 r1096  
    931931          <p>FooBars</p>
    932932          <p>FooBar</p>
     933        </html>""", tmpl.generate(one=1, two=2).render())
     934       
     935    def test_translate_i18n_choose_as_directive_singular_and_plural_with_strip(self):
     936        tmpl = MarkupTemplate("""<html xmlns:py="http://genshi.edgewall.org/"
     937            xmlns:i18n="http://genshi.edgewall.org/i18n">
     938        <i18n:choose numeral="two">
     939          <p i18n:singular="" py:strip="">FooBar Singular with Strip</p>
     940          <p i18n:plural="">FooBars Plural without Strip</p>
     941        </i18n:choose>
     942        <i18n:choose numeral="two">
     943          <p i18n:singular="">FooBar singular without strip</p>
     944          <p i18n:plural="" py:strip="">FooBars plural with strip</p>
     945        </i18n:choose>
     946        <i18n:choose numeral="one">
     947          <p i18n:singular="">FooBar singular without strip</p>
     948          <p i18n:plural="" py:strip="">FooBars plural with strip</p>
     949        </i18n:choose>
     950        <i18n:choose numeral="one">
     951          <p i18n:singular="" py:strip="">FooBar singular with strip</p>
     952          <p i18n:plural="">FooBars plural without strip</p>
     953        </i18n:choose>
     954        </html>""")
     955        translations = DummyTranslations()
     956        translator = Translator(translations)
     957        translator.setup(tmpl)
     958        self.assertEqual("""<html>
     959          <p>FooBars Plural without Strip</p>
     960          FooBars plural with strip
     961          <p>FooBar singular without strip</p>
     962          FooBar singular with strip
    933963        </html>""", tmpl.generate(one=1, two=2).render())
    934964
     
    14381468            <p i18n:plural="">Foos $fname $lname</p>
    14391469          </div>
     1470          <div i18n:choose="one; fname, lname">
     1471            <p i18n:singular="" py:strip="">Foo $fname $lname</p>
     1472            <p i18n:plural="">Foos $fname $lname</p>
     1473          </div>
    14401474        </html>""")
    14411475        translations = DummyTranslations({
     
    14491483        self.assertEqual("""<html>
    14501484          <div>
    1451             Vohs John Doe
    1452           </div>
    1453         </html>""", tmpl.generate(two=2, fname='John', lname='Doe').render())
     1485            <p>Vohs John Doe</p>
     1486          </div>
     1487          <div>
     1488            Voh John Doe
     1489          </div>
     1490        </html>""", tmpl.generate(
     1491            one=1, two=2, fname='John',lname='Doe').render())
    14541492       
    14551493    def test_translate_i18n_choose_and_plural_with_py_strip(self):
Note: See TracChangeset for help on using the changeset viewer.