Custom Template Tag - No Translate

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
from django.utils import translation

def force_no_translation(parser, token):
    nodelist = parser.parse(('endnotrans',))
    parser.delete_first_token()
    return NoTransNode(nodelist)

class NoTransNode(template.Node):
    
    def __init__(self, nodelist):
        self.nodelist = nodelist
        
    def render(self, context):
        language = translation.get_language()
        translation.deactivate()
        output = self.nodelist.render(context)
        translation.activate(language)
        return output

register.tag('notrans', force_no_translation)

More like this

  1. Auto HTML Linebreak filter by punteney 5 years, 1 month ago
  2. Complex Formsets, Redux by smagala 3 years, 2 months ago
  3. autotranslate po files using google translate by dnordberg 4 years, 8 months ago
  4. Template Tag for Random Selection of Any Line by drhoden 2 years, 10 months ago
  5. Generic object_detail view with multiple named URL filters by cotton 1 year, 5 months ago

Comments

mk (on April 15, 2010):

Shouldn't you use language = translation.get_language() on line 14, so you don't require the request in the context?

#

robcharlwood (on April 15, 2010):

That's quite a good point actually MK,

I am sure I did it like this for a reason but I think it was project specific and so I can update this snippet once I have tested it.

Regards,

Rob

#

(Forgotten your password?)