- Timestamp:
- Feb 23, 2010, 7:58:47 PM (14 years ago)
- Location:
- trunk/genshi/filters
- Files:
-
- 2 edited
-
i18n.py (modified) (3 diffs)
-
tests/i18n.py (modified) (3 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/genshi/filters/i18n.py
r1095 r1096 344 344 if not dngettext: 345 345 dngettext = lambda d, s, p, n: ngettext(s, p, n) 346 347 346 for kind, event, pos in stream: 348 347 if kind is SUB: 349 348 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 the354 # event's have been translated and singular or plural355 # form has been chosen. So, by having py:strip on356 # an i18n:singular element is as if i18n:plural had it357 # too.358 strip_directive_present.append(subdirectives.pop(idx))359 349 if isinstance(subdirectives[0], 360 350 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 ) 361 359 # Apply directives to update context 362 360 singular_stream = list(_apply_directives(substream, … … 369 367 ctxt, vars) 370 368 ) 369 del strip_directive_present 371 370 new_stream.append((MSGBUF, (), ('', -1))) # msgbuf place holder 372 371 singular_msgbuf = ctxt.get('_i18n.choose.SingularDirective') 373 372 elif isinstance(subdirectives[0], 374 373 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 ) 375 382 # Apply directives to update context 376 383 plural_stream = list(_apply_directives(substream, 377 384 subdirectives, 378 385 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 379 393 plural_msgbuf = ctxt.get('_i18n.choose.PluralDirective') 380 394 else: … … 387 401 s, p, n) 388 402 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 389 418 for kind, data, pos in new_stream: 390 419 if kind is MSGBUF: 391 for skind, sdata, spos in singular_stream:420 for skind, sdata, spos in chosen_stream: 392 421 if skind is MSGBUF: 393 422 translation = ngettext(singular_msgbuf.format(), 394 423 plural_msgbuf.format(), 395 424 self.numeral.evaluate(ctxt)) 396 for event in singular_msgbuf.translate(translation):425 for event in chosen_msgbuf.translate(translation): 397 426 yield event 398 427 else: -
trunk/genshi/filters/tests/i18n.py
r1094 r1096 931 931 <p>FooBars</p> 932 932 <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 933 963 </html>""", tmpl.generate(one=1, two=2).render()) 934 964 … … 1438 1468 <p i18n:plural="">Foos $fname $lname</p> 1439 1469 </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> 1440 1474 </html>""") 1441 1475 translations = DummyTranslations({ … … 1449 1483 self.assertEqual("""<html> 1450 1484 <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()) 1454 1492 1455 1493 def test_translate_i18n_choose_and_plural_with_py_strip(self):
Note: See TracChangeset
for help on using the changeset viewer.
