Example #1
0
 def render(self, context):
     self.filter_expression.var.translate = not self.noop
     if self.message_context:
         self.filter_expression.var.message_context = (
             self.message_context.resolve(context))
     output = self.filter_expression.resolve(context)
     value = _render_value_in_context(output, context)
     if self.asvar:
         context[self.asvar] = value
         return ''
     else:
         return value
Example #2
0
 def render(self, context):
     if self.message_context:
         message_context = self.message_context.resolve(context)
     else:
         message_context = None
     tmp_context = {}
     for var, val in self.extra_context.items():
         tmp_context[var] = val.resolve(context)
     # Update() works like a push(), so corresponding context.pop() is at
     # the end of function
     context.update(tmp_context)
     singular, vars = self.render_token_list(self.singular)
     # Escape all isolated '%'
     singular = re.sub('%(?!\()', '%%', singular)
     if self.plural and self.countervar and self.counter:
         count = self.counter.resolve(context)
         context[self.countervar] = count
         plural, plural_vars = self.render_token_list(self.plural)
         plural = re.sub('%(?!\()', '%%', plural)
         if message_context:
             result = translation.npgettext(message_context, singular,
                                            plural, count)
         else:
             result = translation.ungettext(singular, plural, count)
         vars.extend(plural_vars)
     else:
         if message_context:
             result = translation.pgettext(message_context, singular)
         else:
             result = translation.ugettext(singular)
     data = dict([(v, _render_value_in_context(context.get(v, ''), context)) for v in vars])
     context.pop()
     try:
         result = result % data
     except (KeyError, ValueError):
         with translation.override(None):
             result = self.render(context)
     return result