| 1 |
"""Jinja2's i18n functionality is not exactly the same as Django's. |
| 2 |
In particular, the tags names and their syntax are different: |
| 3 |
|
| 4 |
1. The Django ``trans`` tag is replaced by a _() global. |
| 5 |
2. The Django ``blocktrans`` tag is called ``trans``. |
| 6 |
|
| 7 |
(1) isn't an issue, since the whole ``makemessages`` process is based on |
| 8 |
converting the template tags to ``_()`` calls. However, (2) means that |
| 9 |
those Jinja2 ``trans`` tags will not be picked up my Django's |
| 10 |
``makemessage`` command. |
| 11 |
|
| 12 |
There aren't any nice solutions here. While Jinja2's i18n extension does |
| 13 |
come with extraction capabilities built in, the code behind ``makemessages`` |
| 14 |
unfortunately isn't extensible, so we can: |
| 15 |
|
| 16 |
* Duplicate the command + code behind it. |
| 17 |
* Offer a separate command for Jinja2 extraction. |
| 18 |
* Try to get Django to offer hooks into makemessages(). |
| 19 |
* Monkey-patch. |
| 20 |
|
| 21 |
We are currently doing that last thing. It turns out there we are lucky |
| 22 |
for once: It's simply a matter of extending two regular expressions. |
| 23 |
Credit for the approach goes to: |
| 24 |
http://stackoverflow.com/questions/2090717/getting-translation-strings-for-jinja2-templates-integrated-with-django-1-x |
| 25 |
""" |
| 26 |
|
| 27 |
import re |
| 28 |
|
| 29 |
from django.core.management.commands import makemessages |
| 30 |
from django.template import engines |
| 31 |
from django.template.base import BLOCK_TAG_START, BLOCK_TAG_END |
| 32 |
from django.utils.translation import template as trans_real |
| 33 |
|
| 34 |
|
| 35 |
strip_whitespace_right = re.compile(fr"({BLOCK_TAG_START}-?\s*(trans|pluralize).*?-{BLOCK_TAG_END})\s+", re.U) |
| 36 |
strip_whitespace_left = re.compile(fr"\s+({BLOCK_TAG_START}-\s*(endtrans|pluralize).*?-?{BLOCK_TAG_END})", re.U) |
| 37 |
|
| 38 |
|
| 39 |
def strip_whitespaces(src): |
| 40 |
src = strip_whitespace_left.sub(r'\1', src) |
| 41 |
src = strip_whitespace_right.sub(r'\1', src) |
| 42 |
return src |
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
trans_block_re = re.compile( |
| 48 |
fr"({BLOCK_TAG_START}-?\s*trans)(?!\s+(?:no)?trimmed)" |
| 49 |
fr"(.*?{BLOCK_TAG_END}.*?{BLOCK_TAG_START}-?\s*?endtrans\s*?-?{BLOCK_TAG_END})", |
| 50 |
re.U | re.DOTALL |
| 51 |
) |
| 52 |
|
| 53 |
|
| 54 |
def apply_i18n_trimmed_policy(src, engine): |
| 55 |
|
| 56 |
i18n_trim_policy = engine.get("OPTIONS", {}).get("policies", {}).get("ext.i18n.trimmed") |
| 57 |
if not i18n_trim_policy: |
| 58 |
return src |
| 59 |
return trans_block_re.sub(r"\1 trimmed \2", src) |
| 60 |
|
| 61 |
|
| 62 |
class Command(makemessages.Command): |
| 63 |
|
| 64 |
def add_arguments(self, parser): |
| 65 |
super().add_arguments(parser) |
| 66 |
parser.add_argument('--jinja2-engine-name', default=None, dest='jinja_engine') |
| 67 |
|
| 68 |
def _get_default_jinja_template_engine(self): |
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
supported_import_paths = ["django_jinja.backend.Jinja2", "django_jinja.jinja2.Jinja2"] |
| 74 |
return [e for e in engines.templates.values() if e["BACKEND"] in supported_import_paths][0] |
| 75 |
|
| 76 |
def handle(self, *args, **options): |
| 77 |
old_endblock_re = trans_real.endblock_re |
| 78 |
old_block_re = trans_real.block_re |
| 79 |
old_constant_re = trans_real.constant_re |
| 80 |
|
| 81 |
old_templatize = trans_real.templatize |
| 82 |
|
| 83 |
|
| 84 |
trans_real.endblock_re = re.compile( |
| 85 |
trans_real.endblock_re.pattern + '|' + r"""^-?\s*endtrans\s*-?$""") |
| 86 |
trans_real.block_re = re.compile( |
| 87 |
trans_real.block_re.pattern + '|' + r"""^-?\s*trans(?:\s+(?:no)?trimmed)?(?:\s+(?!'|")(?=.*?=.*?)|\s*-?$)""") |
| 88 |
trans_real.plural_re = re.compile( |
| 89 |
trans_real.plural_re.pattern + '|' + r"""^-?\s*pluralize(?:\s+.+|-?$)""") |
| 90 |
trans_real.constant_re = re.compile(r""".*?_\(((?:".*?(?<!\\)")|(?:'.*?(?<!\\)')).*?\)""") |
| 91 |
|
| 92 |
if options['jinja_engine']: |
| 93 |
jinja_engine = engines[options['jinja_engine']] |
| 94 |
else: |
| 95 |
jinja_engine = self._get_default_jinja_template_engine() |
| 96 |
|
| 97 |
def my_templatize(src, origin=None, **kwargs): |
| 98 |
new_src = strip_whitespaces(src) |
| 99 |
new_src = apply_i18n_trimmed_policy(new_src, jinja_engine) |
| 100 |
return old_templatize(new_src, origin, **kwargs) |
| 101 |
|
| 102 |
trans_real.templatize = my_templatize |
| 103 |
|
| 104 |
try: |
| 105 |
super().handle(*args, **options) |
| 106 |
finally: |
| 107 |
trans_real.endblock_re = old_endblock_re |
| 108 |
trans_real.block_re = old_block_re |
| 109 |
trans_real.templatize = old_templatize |
| 110 |
trans_real.constant_re = old_constant_re |