3

以下のように定義されたrender_to_responseを介して、djangoでjinj2テンプレートを使用できます

django.confのインポート設定から
from django.core.exceptions import ImproperlyConfigured
django.http から HttpResponse をインポート
from django.template import TemplateDoesNotExist, Context
django.utilsインポート翻訳から
itertools インポート チェーンから
jinja2インポートFileSystemLoader、環境から
jinja2インポートノードから
from jinja2.ext import 拡張機能
django.confのインポート設定から

jinja_filters を jf としてインポート
トレースバックのインポート

django.utils.translation から gettext、ngettext をインポート

クラス DjangoTranslator(オブジェクト):

    def __init__(自己):
        self.gettext = gettext
        self.ngettext = ngettext

クラス DjangoEnvironment(環境):

    def get_translator (自己、コンテキスト):
        DjangoTranslator() を返す


template_dirs = getattr(設定、'TEMPLATE_DIRS')
default_mimetype = getattr(設定、「DEFAULT_CONTENT_TYPE」)
global_exts = getattr(設定, 'JINJA_EXTENSIONS', ())
env = DjangoEnvironment(autoescape=False, loader=FileSystemLoader(template_dirs, encoding="utf-8"), extensions=global_exts)
env.filters.update({'myescape':jf.myescape})

global_exts の「jinja2.ext.i18n」の場合:
        env.install_gettext_translations(翻訳)

def render_to_response(filename, context={}, context_instance=Context({}), mimetype=default_mimetype):
    テンプレート = env.get_template(ファイル名)
    context_instance.dicts の d の場合:
        context.update(d)
    context.update({'settings':settings})
    レンダリング = template.render(**context)
    return HttpResponse(レンダリング済み、mimetype=mimetype)

ただし、django に jinja2 テンプレートの翻訳文字列を抽出させることはできません。

以下の django/utils/translation/trans_real.py の行により、makemessages コマンドが templatize@trans_real.py を介して i18n 用の django テンプレートを解析できるようになるようです。

inline_re = re.compile(r"""^\s*trans\s+((?:".*?")|(?:'.*?'))\s*""")
block_re = re.compile(r"""^\s*blocktrans(?:\s+|$)""")
endblock_re = re.compile(r"""^\s*endblocktrans$""")
multiple_re = re.compile(r"""^\s*plural$""")
constant_re = re.compile(r"""_\(((?:".*?")|(?:'.*?'))\)""")

翻訳文字列を抽出するために jinja2 テンプレートでローカルで使用するために翻訳タグの正規表現を書き換えて makemessages.py を変更するよりも良い方法はありますか?

4

2 に答える 2

2

少し変更を加えて作成しました。基本的なレシピは次のとおりです。必要に応じて、さらに追加/変更する必要がある場合があります。

$ ~ > cp $DJANGO_PATH/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 水 1 月 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$""")
 multiple_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$""")

 デフ テンプレート (ソース):
     """
@@ -451,7 +454,7 @@
     for t in Lexer(src, None).tokenize():
         イントランスの場合:
             t.token_type == TOKEN_BLOCK の場合:
- endbmatch = endblock_re.match(t.contents)
+ endbmatch = jinja_endblock_re.match(t.contents)
                 pluralmatch =plural_re.match(t.contents)
                 endbmatch の場合:
                     複数形の場合:
@@ -485,7 +488,7 @@
         そうしないと:
             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)
                 一致する場合:
                     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 @@
     そうしないと:
         settings.configure(USE_I18N = True)

- django.utils.translation インポート テンプレート化から
+ myproject.utils.translation インポート テンプレートから

     if os.path.isdir(os.path.join('conf', 'locale')):
         localedir = os.path.abspath(os.path.join('conf', 'locale'))


次に、次のようにmakeメッセージを呼び出すと、トリックが実行されます

$ ~/myproject/ > ./manage.py mymakemessages -l $LANGUAGE -e .jinja -v 2

私のテンプレートの名前は templ_name.jinja です。上記のコマンドの .jinja を、テンプレート名に使用する拡張子に置き換える必要があります。

于 2010-01-20T03:39:10.597 に答える
1

このアプローチに基づいて、Coffin にこれのサポートを追加しました。

http://github.com/miracle2k/coffin/commit/ba1a8a510b05074731d383e0dc1f7c21c67ff728

于 2010-02-27T05:34:31.643 に答える