I can use jinj2 templates with django via render_to_response defined as below
from django.conf import settings from django.core.exceptions import ImproperlyConfigured from django.http import HttpResponse from django.template import TemplateDoesNotExist, Context from django.utils import translation from itertools import chain from jinja2 import FileSystemLoader, Environment from jinja2 import nodes from jinja2.ext import Extension from django.conf import settings import jinja_filters as jf import traceback from django.utils.translation import gettext, ngettext class DjangoTranslator(object): def __init__(self): self.gettext = gettext self.ngettext = ngettext class DjangoEnvironment(Environment): def get_translator(self, context): return DjangoTranslator() template_dirs = getattr(settings, TEMPLATE_DIRS ) default_mimetype = getattr(settings, DEFAULT_CONTENT_TYPE ) global_exts = getattr(settings, JINJA_EXTENSIONS , ()) env = DjangoEnvironment(autoescape=False, loader=FileSystemLoader(template_dirs, encoding="utf-8"), extensions=global_exts) env.filters.update({ myescape :jf.myescape}) if jinja2.ext.i18n in global_exts: env.install_gettext_translations(translation) def render_to_response(filename, context={}, context_instance=Context({}), mimetype=default_mimetype): template = env.get_template(filename) for d in context_instance.dicts: context.update(d) context.update({ settings :settings}) rendered = template.render(**context) return HttpResponse(rendered, mimetype=mimetype)
but cannot make django extract translation strings for jinja2 templates.
seems that the lines below in django/utils/translation/trans_real.py make it possible for makemessages command to parse django templates for i18n via templatize@trans_real.py
inline_re = re.compile(r"""^s*transs+((?:".*?")|(?: .*? ))s*""") block_re = re.compile(r"""^s*blocktrans(?:s+|$)""") endblock_re = re.compile(r"""^s*endblocktrans$""") plural_re = re.compile(r"""^s*plural$""") constant_re = re.compile(r"""_(((?:".*?")|(?: .*? )))""")
is there a nicer way than modifying the makemessages.py by rewriting translation tags regexes for local use on jinja2 templates to extract translation strings?