<s> import re <EOL> from django . template import Node , Variable , VariableNode <EOL> from django . template import TemplateSyntaxError , TokenParser , Library <EOL> from django . template import TOKEN_TEXT , TOKEN_VAR <EOL> from django . utils import translation <EOL> from django . utils . encoding import force_unicode <EOL> register = Library ( ) <EOL> class GetAvailableLanguagesNode ( Node ) : <EOL> def __init__ ( self , variable ) : <EOL> self . variable = variable <EOL> def render ( self , context ) : <EOL> from django . conf import settings <EOL> context [ self . variable ] = [ ( k , translation . ugettext ( v ) ) for k , v in settings . LANGUAGES ] <EOL> return '<STR_LIT>' <EOL> class GetCurrentLanguageNode ( Node ) : <EOL> def __init__ ( self , variable ) : <EOL> self . variable = variable <EOL> def render ( self , context ) : <EOL> context [ self . variable ] = translation . get_language ( ) <EOL> return '<STR_LIT>' <EOL> class GetCurrentLanguageBidiNode ( Node ) : <EOL> def __init__ ( self , variable ) : <EOL> self . variable = variable <EOL> def render ( self , context ) : <EOL> context [ self . variable ] = translation . get_language_bidi ( ) <EOL> return '<STR_LIT>' <EOL> class TranslateNode ( Node ) : <EOL> def __init__ ( self , value , noop ) : <EOL> self . value = Variable ( value ) <EOL> self . noop = noop <EOL> def render ( self , context ) : <EOL> value = self . value . resolve ( context ) <EOL> if self . noop : <EOL> return value <EOL> else : <EOL> return translation . ugettext ( value ) <EOL> class BlockTranslateNode ( Node ) : <EOL> def __init__ ( self , extra_context , singular , plural = None , countervar = None , <EOL> counter = None ) : <EOL> self . extra_context = extra_context <EOL> self . singular = singular <EOL> self . plural = plural <EOL> self . countervar = countervar <EOL> self . counter = counter <EOL> def render_token_list ( self , tokens ) : <EOL> result = [ ] <EOL> vars = [ ] <EOL> for token in tokens : <EOL> if token . token_type == TOKEN_TEXT : <EOL> result . append ( token . contents ) <EOL> elif token . token_type == TOKEN_VAR : <EOL> result . append ( u'<STR_LIT>' % token . contents ) <EOL> vars . append ( token . contents ) <EOL> return '<STR_LIT>' . join ( result ) , vars <EOL> def render ( self , context ) : <EOL> tmp_context = { } <EOL> for var , val in self . extra_context . items ( ) : <EOL> tmp_context [ var ] = val . render ( context ) <EOL> context . update ( tmp_context ) <EOL> singular , vars = self . render_token_list ( self . singular ) <EOL> if self . plural and self . countervar and self . counter : <EOL> count = self . counter . resolve ( context ) <EOL> context [ self . countervar ] = count <EOL> plural , vars = self . render_token_list ( self . plural ) <EOL> result = translation . ungettext ( singular , plural , count ) <EOL> else : <EOL> result = translation . ugettext ( singular ) <EOL> result = re . sub ( u'<STR_LIT>' , u'<STR_LIT>' , result ) <EOL> data = dict ( [ ( v , force_unicode ( context [ v ] ) ) for v in vars ] ) <EOL> context . pop ( ) <EOL> return result % data <EOL> def do_get_available_languages ( parser , token ) : <EOL> """<STR_LIT>""" <EOL> args = token . contents . split ( ) <EOL> if len ( args ) != <NUM_LIT:3> or args [ <NUM_LIT:1> ] != '<STR_LIT>' : <EOL> raise TemplateSyntaxError , "<STR_LIT>" % args <EOL> return GetAvailableLanguagesNode ( args [ <NUM_LIT:2> ] ) <EOL> def do_get_current_language ( parser , token ) : <EOL> """<STR_LIT>""" <EOL> args = token . contents . split ( ) <EOL> if len ( args ) != <NUM_LIT:3> or args [ <NUM_LIT:1> ] != '<STR_LIT>' : <EOL> raise TemplateSyntaxError , "<STR_LIT>" % args <EOL> return GetCurrentLanguageNode ( args [ <NUM_LIT:2> ] ) <EOL> def do_get_current_language_bidi ( parser , token ) : <EOL> """<STR_LIT>""" <EOL> args = token . contents . split ( ) <EOL> if len ( args ) != <NUM_LIT:3> or args [ <NUM_LIT:1> ] != '<STR_LIT>' : <EOL> raise TemplateSyntaxError , "<STR_LIT>" % args <EOL> return GetCurrentLanguageBidiNode ( args [ <NUM_LIT:2> ] ) <EOL> def do_translate ( parser , token ) : <EOL> """<STR_LIT>""" <EOL> class TranslateParser ( TokenParser ) : <EOL> def top ( self ) : <EOL> value = self . value ( ) <EOL> if self . more ( ) : <EOL> if self . tag ( ) == '<STR_LIT>' : <EOL> noop = True <EOL> else : <EOL> raise TemplateSyntaxError , "<STR_LIT>" <EOL> else : <EOL> noop = False <EOL> return ( value , noop ) <EOL> value , noop = TranslateParser ( token . contents ) . top ( ) <EOL> return TranslateNode ( value , noop ) <EOL> def do_block_translate ( parser , token ) : <EOL> """<STR_LIT>""" <EOL> class BlockTranslateParser ( TokenParser ) : <EOL> def top ( self ) : <EOL> countervar = None <EOL> counter = None <EOL> extra_context = { } <EOL> while self . more ( ) : <EOL> tag = self . tag ( ) <EOL> if tag == '<STR_LIT>' or tag == '<STR_LIT>' : <EOL> value = self . value ( ) <EOL> if self . tag ( ) != '<STR_LIT>' : <EOL> raise TemplateSyntaxError , "<STR_LIT>" <EOL> extra_context [ self . tag ( ) ] = VariableNode ( <EOL> parser . compile_filter ( value ) ) <EOL> elif tag == '<STR_LIT:count>' : <EOL> counter = parser . compile_filter ( self . value ( ) ) <EOL> if self . tag ( ) != '<STR_LIT>' : <EOL> raise TemplateSyntaxError , "<STR_LIT>" <EOL> countervar = self . tag ( ) <EOL> else : <EOL> raise TemplateSyntaxError , "<STR_LIT>" % tag <EOL> return ( countervar , counter , extra_context ) <EOL> countervar , counter , extra_context = BlockTranslateParser ( token . contents ) . top ( ) <EOL> singular = [ ] <EOL> plural = [ ] <EOL> while parser . tokens : <EOL> token = parser . next_token ( ) <EOL> if token . token_type in ( TOKEN_VAR , TOKEN_TEXT ) : <EOL> singular . append ( token ) <EOL> else : <EOL> break <EOL> if countervar and counter : <EOL> if token . contents . strip ( ) != '<STR_LIT>' : <EOL> raise TemplateSyntaxError , "<STR_LIT>" <EOL> while parser . tokens : <EOL> token = parser . next_token ( ) <EOL> if token . token_type in ( TOKEN_VAR , TOKEN_TEXT ) : <EOL> plural . append ( token ) <EOL> else : <EOL> break <EOL> if token . contents . strip ( ) != '<STR_LIT>' : <EOL> raise TemplateSyntaxError , "<STR_LIT>" % token . contents <EOL> return BlockTranslateNode ( extra_context , singular , plural , countervar , <EOL> counter ) <EOL> register . tag ( '<STR_LIT>' , do_get_available_languages ) <EOL> register . tag ( '<STR_LIT>' , do_get_current_language ) <EOL> register . tag ( '<STR_LIT>' , do_get_current_language_bidi ) <EOL> register . tag ( '<STR_LIT>' , do_translate ) <EOL> register . tag ( '<STR_LIT>' , do_block_translate ) </s>
