commit 762fd1acd1c752d31bdfb7ba6c258108e07495b8
Author: Claude Paroz <claude@2xlibre.net>
Date: Sun Nov 14 00:02:28 2010 +0100
Allow translator comments to be extracted from templates
diff --git a/django/template/__init__.py b/django/template/__init__.py
index 7440358..e1fdb7e 100644
a
|
b
|
VARIABLE_TAG_START = '{{'
|
82 | 82 | VARIABLE_TAG_END = '}}' |
83 | 83 | COMMENT_TAG_START = '{#' |
84 | 84 | COMMENT_TAG_END = '#}' |
| 85 | TRANSLATOR_COMMENT_MARK = 'L10n' |
85 | 86 | SINGLE_BRACE_START = '{' |
86 | 87 | SINGLE_BRACE_END = '}' |
87 | 88 | |
… |
… |
class Lexer(object):
|
237 | 238 | elif token_string.startswith(BLOCK_TAG_START): |
238 | 239 | token = Token(TOKEN_BLOCK, token_string[len(BLOCK_TAG_START):-len(BLOCK_TAG_END)].strip()) |
239 | 240 | elif token_string.startswith(COMMENT_TAG_START): |
240 | | token = Token(TOKEN_COMMENT, '') |
| 241 | content = '' |
| 242 | if token_string.find(TRANSLATOR_COMMENT_MARK): |
| 243 | content = token_string[len(COMMENT_TAG_START):-len(COMMENT_TAG_END)].strip() |
| 244 | token = Token(TOKEN_COMMENT, content) |
241 | 245 | else: |
242 | 246 | token = Token(TOKEN_TEXT, token_string) |
243 | 247 | return token |
diff --git a/django/utils/translation/trans_real.py b/django/utils/translation/trans_real.py
index 827337c..832acc7 100644
a
|
b
|
def templatize(src):
|
427 | 427 | does so by translating the Django translation tags into standard gettext |
428 | 428 | function invocations. |
429 | 429 | """ |
430 | | from django.template import Lexer, TOKEN_TEXT, TOKEN_VAR, TOKEN_BLOCK |
| 430 | from django.template import Lexer, TOKEN_TEXT, TOKEN_VAR, TOKEN_BLOCK, TOKEN_COMMENT |
431 | 431 | out = StringIO() |
432 | 432 | intrans = False |
433 | 433 | inplural = False |
434 | 434 | singular = [] |
435 | 435 | plural = [] |
| 436 | incomment = False |
| 437 | comment = [] |
436 | 438 | for t in Lexer(src, None).tokenize(): |
437 | | if intrans: |
| 439 | if incomment: |
| 440 | if t.token_type == TOKEN_BLOCK and t.contents == 'endcomment': |
| 441 | out.write(' # %s' % ''.join(comment)) |
| 442 | incomment = False |
| 443 | comment = [] |
| 444 | else: |
| 445 | comment.append(t.contents) |
| 446 | elif intrans: |
438 | 447 | if t.token_type == TOKEN_BLOCK: |
439 | 448 | endbmatch = endblock_re.match(t.contents) |
440 | 449 | pluralmatch = plural_re.match(t.contents) |
… |
… |
def templatize(src):
|
488 | 497 | elif cmatches: |
489 | 498 | for cmatch in cmatches: |
490 | 499 | out.write(' _(%s) ' % cmatch) |
| 500 | elif t.contents == 'comment': |
| 501 | incomment = True |
491 | 502 | else: |
492 | 503 | out.write(blankout(t.contents, 'B')) |
493 | 504 | elif t.token_type == TOKEN_VAR: |
… |
… |
def templatize(src):
|
500 | 511 | out.write(' %s ' % p.split(':',1)[1]) |
501 | 512 | else: |
502 | 513 | out.write(blankout(p, 'F')) |
| 514 | elif t.token_type == TOKEN_COMMENT: |
| 515 | out.write(' # %s' % t.contents) |
503 | 516 | else: |
504 | 517 | out.write(blankout(t.contents, 'X')) |
505 | 518 | return out.getvalue() |
diff --git a/tests/regressiontests/i18n/commands/extraction.py b/tests/regressiontests/i18n/commands/extraction.py
index 6695e9b..dfdd7c2 100644
a
|
b
|
class BasicExtractorTests(ExtractorTests):
|
47 | 47 | po_contents = open(self.PO_FILE, 'r').read() |
48 | 48 | self.assert_('#. L10n: This comment should be extracted' in po_contents) |
49 | 49 | self.assert_('This comment should not be extracted' not in po_contents) |
| 50 | # Comments in templates |
| 51 | self.assert_('#. L10n: Django template comment for translators' in po_contents) |
| 52 | self.assert_('#. L10n: Django comment block for translators' in po_contents) |
50 | 53 | |
51 | 54 | def test_templatize(self): |
52 | 55 | os.chdir(self.test_dir) |
diff --git a/tests/regressiontests/i18n/commands/templates/test.html b/tests/regressiontests/i18n/commands/templates/test.html
index e1c3eee..ca2f952 100644
a
|
b
|
|
1 | 1 | {% load i18n %} |
| 2 | {% comment %} L10n: Django comment block for translators {% endcomment %} |
2 | 3 | {% trans "This literal should be included." %} |
3 | 4 | {% trans "This literal should also be included wrapped or not wrapped depending on the use of the --no-wrap option." %} |
4 | | {% blocktrans %}I think that 100% is more that 50% of anything.{% endblocktrans %} |
5 | | {% blocktrans with 'txt' as obj %}I think that 100% is more that 50% of {{ obj }}.{% endblocktrans %} |
6 | | No newline at end of file |
| 5 | |
| 6 | {# L10n: Django template comment for translators #} |
| 7 | <p>{% blocktrans %}I think that 100% is more that 50% of anything.{% endblocktrans %}</p> |
| 8 | {% blocktrans with 'txt' as obj %}I think that 100% is more that 50% of {{ obj }}.{% endblocktrans %} |