为与django 1.x集成的jinja2模板获取翻译字符串?

为与django 1.x集成的jinja2模板获取翻译字符串?,django,internationalization,translation,jinja2,xgettext,Django,Internationalization,Translation,Jinja2,Xgettext,我可以通过如下定义的render_to_响应将jinj2模板与django一起使用 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

我可以通过如下定义的render_to_响应将jinj2模板与django一起使用

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) 从django.conf导入设置 从django.core.exceptions导入配置不正确 从django.http导入HttpResponse 从django.template导入TemplateDoesNotExist,上下文 从django.utils导入翻译 来自itertools进口链 从jinja2导入文件系统加载器,环境 从jinja2导入节点 从jinja2.ext导入扩展 从django.conf导入设置 将jinja_过滤器作为jf导入 导入回溯 从django.utils.translation导入gettext、ngettext 类DjangTranslator(对象): 定义初始化(自): self.gettext=gettext self.ngettext=ngettext DjangoEnvironment类(环境): def get_转换器(自身、上下文): 返回DjangTranslator() template\u dirs=getattr(设置,'template\u dirs') default\u mimetype=getattr(设置“default\u CONTENT\u TYPE”) 全局_exts=getattr(设置,'JINJA_EXTENSIONS',()) env=DjangoEnvironment(autoescape=False,loader=FileSystemLoader(template\u dirs,encoding=“utf-8”),extensions=global\u exts) update({'myescape':jf.myescape}) 如果全局_exts中的'jinja2.ext.i18n': 环境安装\u获取文本\u翻译(翻译) def render_to_响应(文件名,context={},context_实例=context({}),mimetype=default_mimetype): template=env.get_模板(文件名) 对于context_instance.dicts中的d: 上下文更新(d) 更新({'settings':settings}) 呈现=模板。呈现(**上下文) 返回HttpResponse(呈现,mimetype=mimetype) 但无法使django为jinja2模板提取翻译字符串

似乎django/utils/translation/trans_real.py中的以下行 使makemessages命令能够通过templatize@trans_real.py

inline_re = re.compile(r"""^\s*trans\s+((?:".*?")|(?:'.*?'))\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"""_\(((?:".*?")|(?:'.*?'))\)""") inline_re=re.compile(r“”“^\s*trans\s+((?:“*?”)|(?:“*?”)\s*”) block_re=re.compile(r“”^\s*blocktrans(?:\s+|$)“”) endblock_re=re.compile(r“^\s*endblocktrans$”) 复数\u re=re.compile(r“”^\s*复数$“”) 常数\u re=re.compile(r“”“\u\((((?:“*?”))|(?:“*?”)\))“”)
有没有比修改makemessages.py更好的方法,在jinja2模板上重写本地使用的翻译标记regex来提取翻译字符串

我做了一点修改。。基本配方如下:, 您可能需要添加/修改更多内容以满足您的需要

$ ~ > cp $DJANGO_PATH/utils/translation/ myproject/utils/ -a $~>cp$DJANGO\u路径/utils/translation/myproject/utils/-a 并进行以下修改:

$ ~ > diff $DJANGO_PATH/utils/translation/trans_real.py myproject/utils/translation/trans_real.py -u --- utils/translation/trans_real.py Wed Jan 20 05:07:46 2010 +++ myproject/utils/translation/trans_real.py Wed Jan 20 04:51:39 2010 @@ -435,6 +435,9 @@ endblock_re = re.compile(r"""^\s*endblocktrans$""") plural_re = re.compile(r"""^\s*plural$""") constant_re = re.compile(r"""_\(((?:".*?")|(?:'.*?'))\)""") +jinja_block_re = re.compile(r"""^\s*trans(?:\s+|$)""") +jinja_endblock_re = re.compile(r"""^\s*endtrans$""") def templatize(src): """ @@ -451,7 +454,7 @@ for t in Lexer(src, None).tokenize(): if intrans: if t.token_type == TOKEN_BLOCK: - endbmatch = endblock_re.match(t.contents) + endbmatch = jinja_endblock_re.match(t.contents) pluralmatch = plural_re.match(t.contents) if endbmatch: if inplural: @@ -485,7 +488,7 @@ else: if t.token_type == TOKEN_BLOCK: imatch = inline_re.match(t.contents) - bmatch = block_re.match(t.contents) + bmatch = jinja_block_re.match(t.contents) cmatches = constant_re.findall(t.contents) if imatch: g = imatch.group(1) $ ~ > cp $DJANGO_PATH/core/management/commands/makemessages.py myproject/myapp/management/commands/ $ ~/myproject/ > diff $DJANGO_PATH/core/management/commands/makemessages.py main/management/commands/makemessages.py -u --- /usr/lib/python2.5/site-packages/django/core/management/commands/makemessages.py Wed Jan 20 05:08:37 2010 +++ main/management/commands/makemessages.py Wed Jan 20 05:28:41 2010 @@ -56,7 +56,7 @@ else: settings.configure(USE_I18N = True) - from django.utils.translation import templatize + from myproject.utils.translation import templatize if os.path.isdir(os.path.join('conf', 'locale')): localedir = os.path.abspath(os.path.join('conf', 'locale')) $~>diff$DJANGO\u PATH/utils/translation/trans\u real.py myproject/utils/translation/trans\u real.py-u ---utils/translation/trans_real.py星期三2010年1月20日05:07:46 +++myproject/utils/translation/trans_real.py周三2010年1月20日04:51:39 @@ -435,6 +435,9 @@ endblock_re=re.compile(r“^\s*endblocktrans$”) 复数\u re=re.compile(r“”^\s*复数$“”) 常数\u re=re.compile(r“”“\u\((((?:“*?”))|(?:“*?”)\))“”) +jinja_block_re=re.compile(r“”“^\s*trans(?:\s+|$)”) +jinja_endblock_re=re.compile(r“”^\s*endtrans$”) def模板化(src): """ @@ -451,7 +454,7 @@ 对于Lexer中的t(src,None).tokenize(): 如果是内部网: 如果t.token\u type==token\u块: -endbmatch=endblock_.re.match(t.contents) +endbmatch=jinja\u endblock\u re.match(t.contents) 复数匹配=复数匹配(t.内容) 如果EndB匹配: 如果不清楚: @@ -485,7 +488,7 @@ 其他: 如果t.token\u type==token\u块: imatch=内联匹配(t.contents) -B匹配=块匹配(t内容) +bmatch=jinja\u块匹配(t.contents) cmatches=常数(t.含量) 如果imatch: g=imatch.group(1) $~>cp$DJANGO\u路径/core/management/commands/makemessages.py myproject/myapp/management/commands/ $~/myproject/>diff$DJANGO_PATH/core/management/commands/makemessages.py main/management/commands/makemessages.py-u ---/usr/lib/python2.5/site-packages/django/core/management/commands/makemessages.py星期三2010年1月20日05:08:37 +++main/management/commands/makemessages.py星期三2010年1月20日05:28:41 @@ -56,7 +56,7 @@ 其他: 配置(使用_I18N=True) -从django.utils.translation导入模板化 +从myproject.utils.translation导入模板化 如果os.path.isdir(os.path.join('conf','locale')): localedir=os.path.abspath(os.path.join('conf','locale')) 然后按如下方式调用makemessages就可以了

$ ~/myproject/ > ./manage.py mymakemessages -l $LANGUAGE -e .jinja -v 2 $~/myproject/>/manage.py mymakemessages-l$LANGUAGE-e.jinja-v2 我的模板名为temp_name.jinja,您需要替换.jinja
在上面的命令中,使用模板名称的任何扩展名。

基于此方法,我已向Coffin添加了对此的支持:


我还制作了一个软件包来简化django-jinja集成,支持翻译字符串。