Last Updated: February 25, 2016
·
569
· nabucosound

Proper translation while rendering template

Sometimes you do not render templates from views (with the request object as part of the context). For instance, if you render emails from a method or function elsewhere. To properly pass the user's LANGUAGE_CODE context variable to your template renderization:

def _render_tmpl(self, template):
    from django.utils import translation
    ctxt['LANGUAGE_CODE'] = translation.get_language()
    return render_to_string(template, ctxt)