Edgewall Software

Ticket #371: ticket-371.patch

File ticket-371.patch, 6.9 KB (added by palgarvio, 14 years ago)
  • genshi/filters/tests/i18n.py

     
    932932          <p>FooBar</p>
    933933        </html>""", tmpl.generate(one=1, two=2).render())
    934934
     935    def test_translate_i18n_choose_plural_singular_as_directive(self):
     936        # Ticket 371
     937        tmpl = MarkupTemplate("""<html xmlns:py="http://genshi.edgewall.org/"
     938            xmlns:i18n="http://genshi.edgewall.org/i18n">
     939        <i18n:choose numeral="two">
     940          <i18n:singular>FooBar</i18n:singular>
     941          <i18n:plural>FooBars</i18n:plural>
     942        </i18n:choose>
     943        <i18n:choose numeral="one">
     944          <i18n:singular>FooBar</i18n:singular>
     945          <i18n:plural>FooBars</i18n:plural>
     946        </i18n:choose>
     947        </html>""")
     948        translations = DummyTranslations({
     949            ('FooBar', 0): 'FuBar',
     950            ('FooBars', 1): 'FuBars',
     951            'FooBar': 'FuBar',
     952            'FooBars': 'FuBars',
     953        })
     954        translator = Translator(translations)
     955        translator.setup(tmpl)
     956        self.assertEqual("""<html>
     957          FuBars
     958          FuBar
     959        </html>""", tmpl.generate(one=1, two=2).render())
     960
    935961    def test_translate_i18n_choose_as_attribute_with_params(self):
    936962        tmpl = MarkupTemplate("""<html xmlns:py="http://genshi.edgewall.org/"
    937963            xmlns:i18n="http://genshi.edgewall.org/i18n">
     
    14031429            <p>Vohs John Doe</p>
    14041430          </div>
    14051431        </html>""", tmpl.generate(two=2, fname='John', lname='Doe').render())
     1432       
     1433    def test_translate_i18n_choose_and_singular_with_py_strip(self):
     1434        tmpl = MarkupTemplate("""<html xmlns:py="http://genshi.edgewall.org/"
     1435            xmlns:i18n="http://genshi.edgewall.org/i18n">
     1436          <div i18n:choose="two; fname, lname">
     1437            <p i18n:singular="" py:strip="">Foo $fname $lname</p>
     1438            <p i18n:plural="">Foos $fname $lname</p>
     1439          </div>
     1440        </html>""")
     1441        translations = DummyTranslations({
     1442            ('Foo %(fname)s %(lname)s', 0): 'Voh %(fname)s %(lname)s',
     1443            ('Foo %(fname)s %(lname)s', 1): 'Vohs %(fname)s %(lname)s',
     1444                 'Foo %(fname)s %(lname)s': 'Voh %(fname)s %(lname)s',
     1445                'Foos %(fname)s %(lname)s': 'Vohs %(fname)s %(lname)s',
     1446        })
     1447        translator = Translator(translations)
     1448        translator.setup(tmpl)
     1449        self.assertEqual("""<html>
     1450          <div>
     1451            Vohs John Doe
     1452          </div>
     1453        </html>""", tmpl.generate(two=2, fname='John', lname='Doe').render())
     1454       
     1455    def test_translate_i18n_choose_and_plural_with_py_strip(self):
     1456        tmpl = MarkupTemplate("""<html xmlns:py="http://genshi.edgewall.org/"
     1457            xmlns:i18n="http://genshi.edgewall.org/i18n">
     1458          <div i18n:choose="two; fname, lname">
     1459            <p i18n:singular="" py:strip="">Foo $fname $lname</p>
     1460            <p i18n:plural="">Foos $fname $lname</p>
     1461          </div>
     1462        </html>""")
     1463        translations = DummyTranslations({
     1464            ('Foo %(fname)s %(lname)s', 0): 'Voh %(fname)s %(lname)s',
     1465            ('Foo %(fname)s %(lname)s', 1): 'Vohs %(fname)s %(lname)s',
     1466                 'Foo %(fname)s %(lname)s': 'Voh %(fname)s %(lname)s',
     1467                'Foos %(fname)s %(lname)s': 'Vohs %(fname)s %(lname)s',
     1468        })
     1469        translator = Translator(translations)
     1470        translator.setup(tmpl)
     1471        self.assertEqual("""<html>
     1472          <div>
     1473            Voh John Doe
     1474          </div>
     1475        </html>""", tmpl.generate(two=1, fname='John', lname='Doe').render())
    14061476
    14071477    def test_extract_i18n_msg_with_py_strip(self):
    14081478        tmpl = MarkupTemplate("""<html xmlns:py="http://genshi.edgewall.org/"
  • genshi/filters/i18n.py

     
    193193
    194194class ChooseBranchDirective(I18NDirective):
    195195    __slots__ = ['params']
    196 
     196       
    197197    def __call__(self, stream, directives, ctxt, **vars):
    198198        self.params = ctxt.get('_i18n.choose.params', [])[:]
    199199        msgbuf = MessageBuffer(self)
    200200
    201201        stream = iter(_apply_directives(stream, directives, ctxt, vars))
    202         yield stream.next() # the outer start tag
     202       
    203203        previous = stream.next()
     204        if previous[0] is START:
     205            yield previous
     206        else:
     207            msgbuf.append(*previous)
     208           
     209        try:
     210            previous = stream.next()
     211        except StopIteration:
     212            # For example <i18n:singular> or <i18n:plural> directives
     213            yield MSGBUF, (), -1 # the place holder for msgbuf output
     214            ctxt['_i18n.choose.%s' % type(self).__name__] = msgbuf
     215            return
     216       
    204217        for kind, data, pos in stream:
    205218            msgbuf.append(*previous)
    206219            previous = kind, data, pos
    207220        yield MSGBUF, (), -1 # the place holder for msgbuf output
    208         yield previous # the outer end tag
     221
     222        if previous[0] is END:
     223            yield previous # the outer end tag
     224        else:
     225            msgbuf.append(*previous)
    209226        ctxt['_i18n.choose.%s' % type(self).__name__] = msgbuf
    210227
    211228
     
    330347        for kind, event, pos in stream:
    331348            if kind is SUB:
    332349                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))
    333359                if isinstance(subdirectives[0],
    334360                              SingularDirective) and not singular_stream:
    335361                    # Apply directives to update context
    336362                    singular_stream = list(_apply_directives(substream,
    337363                                                             subdirectives,
    338364                                                             ctxt, vars))
     365                    if strip_directive_present:
     366                        singular_stream = list(
     367                            _apply_directives(singular_stream,
     368                                              strip_directive_present,
     369                                              ctxt, vars)
     370                        )
    339371                    new_stream.append((MSGBUF, (), ('', -1))) # msgbuf place holder
    340372                    singular_msgbuf = ctxt.get('_i18n.choose.SingularDirective')
    341373                elif isinstance(subdirectives[0],