Skip to content

Commit a2ad1c9

Browse files
authored
Merge pull request #28 from transifex/underscore-django-template
Tackles a variety of issues raised while applying the migration script to txc
2 parents 238493f + 1b21b7f commit a2ad1c9

3 files changed

Lines changed: 26 additions & 8 deletions

File tree

tests/native/django/test_tools/test_migrations/test_templatetags.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
{% endblocktrans %}
3939
4040
<a href="{{ url }}">Text</a>
41-
{% trans some_other_var %}
41+
{{ _(some_other_var) }}
4242
{% trans some_other_var|some_filter %}
4343
{% trans "try as" as try %}
4444
{% trans "try with <xml>xml</xml>" %}

transifex/native/django/tools/migrations/templatetags.py

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@
66

77
from __future__ import unicode_literals
88

9-
from django.template.base import (TOKEN_COMMENT, TOKEN_TEXT, TOKEN_VAR,
10-
TRANSLATOR_COMMENT_MARK, DebugLexer, Parser)
9+
from django.template.base import (TOKEN_BLOCK, TOKEN_COMMENT, TOKEN_TEXT,
10+
TOKEN_VAR, TRANSLATOR_COMMENT_MARK,
11+
DebugLexer, Parser)
1112
from django.template.defaulttags import token_kwargs
1213
from django.templatetags.i18n import do_block_translate, do_translate
1314
from django.utils.encoding import force_text
@@ -212,7 +213,6 @@ def build_migration(self, src, filename=None, charset='utf-8'):
212213
# the object as given.
213214
# Without the override, a KeyError would be raised inside the parser.
214215
parser.find_filter = find_filter_identity
215-
216216
# Create a migration object for this template; we'll add stuff to it
217217
# as we go
218218
migration = FileMigration(filename, src)
@@ -280,7 +280,19 @@ def _parse_token(self, token, parser, original_string):
280280

281281
# A variable was found; copy as is
282282
elif token.token_type == TOKEN_VAR:
283-
return StringMigration(original_string, original_string), None
283+
# that's a special case we need to take care of:
284+
# {{ _("Are you sure you want to remove the ($(collaborator_count)) selected collaborators?")|escapejs }}
285+
if token.contents.startswith('_('):
286+
token.token_type = TOKEN_BLOCK
287+
clos_par_pos = 0
288+
for i, j in enumerate(token.contents):
289+
if j == ')':
290+
clos_par_pos = i
291+
token.contents = ('trans ' +
292+
token.contents[2:clos_par_pos] +
293+
token.contents[clos_par_pos + 1:])
294+
else:
295+
return StringMigration(original_string, original_string), None
284296

285297
return self._parse_block(token, parser, original_string)
286298

transifex/native/django/utils/templates.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,12 @@
2121
COPY_AS_IS = object()
2222

2323

24+
# hack to make the identity function
25+
# signature compatible to all template filters
26+
def identity(obj, var1=None, var2=None, var3=None, var4=None, var5=None): # pragma n ocover
27+
return obj # pragma no cover
28+
29+
2430
def find_filter_identity(filter_name):
2531
"""Return a filter that does no filtering, i.e always returns all items.
2632
@@ -31,7 +37,7 @@ def find_filter_identity(filter_name):
3137
:return: a callable that represents a filter that does not filtering
3238
:rtype: callable
3339
"""
34-
return lambda obj: obj
40+
return identity
3541

3642

3743
def tnode_to_source_string(tnode):
@@ -85,11 +91,11 @@ def extract_transifex_template_strings(src, origin=None, charset='utf-8'):
8591
token.split_contents()[0] in ('t', 'ut')):
8692
tnode = do_t(parser, token)
8793
source_string = tnode_to_source_string(tnode)
94+
if source_string is None:
95+
continue
8896
if token.lineno and origin:
8997
source_string.occurrences = [
9098
"{}:{}".format(origin, token.lineno)]
91-
if source_string is None:
92-
continue
9399

94100
strings.append(source_string)
95101

0 commit comments

Comments
 (0)