<s> from django import template <EOL> from django . template import Node , Token , TemplateSyntaxError <EOL> from django . template import resolve_variable <EOL> from django . template . defaultfilters import stringfilter <EOL> from django . templatetags import future <EOL> from localeurl import utils <EOL> register = template . Library ( ) <EOL> def chlocale ( url , locale ) : <EOL> """<STR_LIT>""" <EOL> _ , path = utils . strip_script_prefix ( url ) <EOL> _ , path = utils . strip_path ( path ) <EOL> return utils . locale_url ( path , locale ) <EOL> chlocale = stringfilter ( chlocale ) <EOL> register . filter ( '<STR_LIT>' , chlocale ) <EOL> def rmlocale ( url ) : <EOL> """<STR_LIT>""" <EOL> script_prefix , path = utils . strip_script_prefix ( url ) <EOL> _ , path = utils . strip_path ( path ) <EOL> return '<STR_LIT>' . join ( [ script_prefix , path ] ) <EOL> rmlocale = stringfilter ( rmlocale ) <EOL> register . filter ( '<STR_LIT>' , rmlocale ) <EOL> def locale_url ( parser , token ) : <EOL> """<STR_LIT>""" <EOL> bits = token . split_contents ( ) <EOL> if len ( bits ) < <NUM_LIT:3> : <EOL> raise TemplateSyntaxError ( "<STR_LIT>" <EOL> "<STR_LIT>" % bits [ <NUM_LIT:0> ] ) <EOL> urltoken = Token ( token . token_type , bits [ <NUM_LIT:0> ] + '<STR_LIT:U+0020>' + '<STR_LIT:U+0020>' . join ( bits [ <NUM_LIT:2> : ] ) ) <EOL> urlnode = future . url ( parser , urltoken ) <EOL> return LocaleURLNode ( bits [ <NUM_LIT:1> ] , urlnode ) <EOL> class LocaleURLNode ( Node ) : <EOL> def __init__ ( self , locale , urlnode ) : <EOL> self . locale = locale <EOL> self . urlnode = urlnode <EOL> def render ( self , context ) : <EOL> locale = resolve_variable ( self . locale , context ) <EOL> if utils . supported_language ( locale ) is None : <EOL> raise ValueError ( "<STR_LIT>" % locale ) <EOL> path = self . urlnode . render ( context ) <EOL> if self . urlnode . asvar : <EOL> self . urlnode . render ( context ) <EOL> context [ self . urlnode . asvar ] = chlocale ( context [ self . urlnode . asvar ] , <EOL> locale ) <EOL> return '<STR_LIT>' <EOL> else : <EOL> return chlocale ( path , locale ) <EOL> register . tag ( '<STR_LIT>' , locale_url ) </s>
