#NEWLINE# def do_block_translate( parser , token ) : #NEWLINE# #INDENT# '\n    This will translate a block of text with parameters.\n\n    Usage::\n\n        {% blocktrans with foo|filter as bar and baz|filter as boo %}\n        This is {{ bar }} and {{ boo }}.\n        {% endblocktrans %}\n\n    Additionally, this supports pluralization::\n\n        {% blocktrans count var|length as count %}\n        There is {{ count }} object.\n        {% plural %}\n        There are {{ count }} objects.\n        {% endblocktrans %}\n\n    This is much like ngettext, only in template syntax.\n    ' #NEWLINE# class BlockTranslateParser ( TokenParser ) : #NEWLINE# #INDENT# def top( self ) : #NEWLINE# #INDENT# countervar = None #NEWLINE# counter = None #NEWLINE# extra_context = { #NEWLINE# #INDENT# #NEWLINE# #UNINDENT# } #NEWLINE# while self . more ( ) : #NEWLINE# #INDENT# tag = self . tag ( ) #NEWLINE# if ( ( tag == 'with' ) or ( tag == 'and' ) ) : #NEWLINE# #INDENT# value = self . value ( ) #NEWLINE# if ( self . tag ( ) != 'as' ) : #NEWLINE# #INDENT# raise TemplateSyntaxError ( "variable bindings in 'blocktrans' must be 'with value as variable'" ) #NEWLINE# #UNINDENT# extra_context [ self . tag ( ) ] = VariableNode ( parser . compile_filter ( value ) ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# if ( tag == 'count' ) : #NEWLINE# #INDENT# counter = parser . compile_filter ( self . value ( ) ) #NEWLINE# if ( self . tag ( ) != 'as' ) : #NEWLINE# #INDENT# raise TemplateSyntaxError ( "counter specification in 'blocktrans' must be 'count value as variable'" ) #NEWLINE# #UNINDENT# countervar = self . tag ( ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# raise TemplateSyntaxError ( ( "unknown subtag %s for 'blocktrans' found" % tag ) ) #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# return ( countervar , counter , extra_context ) #NEWLINE# #UNINDENT# #UNINDENT# ( countervar , counter , extra_context ) = BlockTranslateParser ( token . contents ) . top ( ) #NEWLINE# singular = [ ] #NEWLINE# plural = [ ] #NEWLINE# while parser . tokens : #NEWLINE# #INDENT# token = parser . next_token ( ) #NEWLINE# if ( token . token_type in ( TOKEN_VAR , TOKEN_TEXT ) ) : #NEWLINE# #INDENT# singular . append ( token ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# break #NEWLINE# #UNINDENT# #UNINDENT# if ( countervar and counter ) : #NEWLINE# #INDENT# if ( token . contents . strip ( ) != 'plural' ) : #NEWLINE# #INDENT# raise TemplateSyntaxError ( "'blocktrans' doesn't allow other block tags inside it" ) #NEWLINE# #UNINDENT# while parser . tokens : #NEWLINE# #INDENT# token = parser . next_token ( ) #NEWLINE# if ( token . token_type in ( TOKEN_VAR , TOKEN_TEXT ) ) : #NEWLINE# #INDENT# plural . append ( token ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# break #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# if ( token . contents . strip ( ) != 'endblocktrans' ) : #NEWLINE# #INDENT# raise TemplateSyntaxError ( ( "'blocktrans' doesn't allow other block tags (seen %r) inside it" % token . contents ) ) #NEWLINE# #UNINDENT# return BlockTranslateNode ( countervar , singular , plural , countervar , counter )