Ticket #3970: 118n-tags_3970.patch

File 118n-tags_3970.patch, 3.2 KB (added by axiak@…, 17 years ago)

Alteration to templatetags/i18n.py for as keyword

  • templatetags/i18n.py

     
    3131        return ''
    3232
    3333class TranslateNode(Node):
    34     def __init__(self, value, noop):
    35         self.value = value
    36         self.noop = noop
     34    def __init__(self, value, noop, as_var):
     35        self.value  = value
     36        self.noop   = noop
     37        self.as_var = as_var
    3738
    3839    def render(self, context):
    3940        value = resolve_variable(self.value, context)
    40         if self.noop:
     41        if not self.noop:
     42            value = translation.gettext(value)
     43
     44        if self.as_var != False:
     45            context[self.as_var] = value
     46            return ''
     47        else:
    4148            return value
    42         else:
    43             return translation.gettext(value)
    4449
     50
    4551class BlockTranslateNode(Node):
    4652    def __init__(self, extra_context, singular, plural=None, countervar=None, counter=None):
    4753        self.extra_context = extra_context
     
    158164    This will just try to translate the contents of
    159165    the variable ``variable``. Make sure that the string
    160166    in there is something that is in the .po file.
     167
     168    Additionally, one can use the 'as' keyword to specify
     169    whether or not something should go into a context variable
     170    instead. For example you can do::
     171
     172        {% trans "this is a string" as translated_text %}
     173        {{ translated_text|truncatewords:4 }}
     174
     175    Or, if you wish to still use noop::
     176   
     177        {% trans "this is a string" as translated_text noop %}
     178        {{ translated_text|truncatewords:4 }}
     179       
    161180    """
    162181    class TranslateParser(TokenParser):
    163182        def top(self):
    164183            value = self.value()
    165184            if self.more():
    166                 if self.tag() == 'noop':
    167                     noop = True
     185                next_tag = self.tag()
     186
     187                if next_tag.lower() not in ('noop','as'):
     188                    raise TemplateSyntaxError, "Invalid parameters for 'trans'"
     189               
     190                if next_tag.lower() == 'as':
     191                    # we're using the 'as' syntax
     192                    if not self.more():
     193                        raise TemplateSyntaxError, "Invalid parameters for 'trans'"
     194                    as_var = self.tag()
     195
     196                    if self.more():
     197                        if self.tag().lower() == 'noop':
     198                            noop = True
     199                        else:
     200                            raise TemplateSyntaxError, "Invalid parameters for 'trans'"
     201
    168202                else:
    169                     raise TemplateSyntaxError, "only option for 'trans' is 'noop'"
     203                    # no as keyword, thus we must be using 'noop'
     204                    as_var = False
     205                    noop   = True
     206                   
    170207            else:
    171                 noop = False
    172             return (value, noop)
    173     value, noop = TranslateParser(token.contents).top()
    174     return TranslateNode(value, noop)
     208                as_var = False
     209                noop   = False
     210            return (value, noop, as_var)
     211    value, noop, as_var = TranslateParser(token.contents).top()
     212    return TranslateNode(value, noop, as_var)
    175213
    176214def do_block_translate(parser, token):
    177215    """
Back to Top