Ticket #12320: mark_template_comments.diff

File mark_template_comments.diff, 4.9 KB (added by Claude Paroz, 14 years ago)

Translator comments in Django templates

  • django/template/__init__.py

    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 = '{{'  
    8282VARIABLE_TAG_END = '}}'
    8383COMMENT_TAG_START = '{#'
    8484COMMENT_TAG_END = '#}'
     85TRANSLATOR_COMMENT_MARK = 'L10n'
    8586SINGLE_BRACE_START = '{'
    8687SINGLE_BRACE_END = '}'
    8788
    class Lexer(object):  
    237238            elif token_string.startswith(BLOCK_TAG_START):
    238239                token = Token(TOKEN_BLOCK, token_string[len(BLOCK_TAG_START):-len(BLOCK_TAG_END)].strip())
    239240            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)
    241245        else:
    242246            token = Token(TOKEN_TEXT, token_string)
    243247        return token
  • django/utils/translation/trans_real.py

    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):  
    427427    does so by translating the Django translation tags into standard gettext
    428428    function invocations.
    429429    """
    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
    431431    out = StringIO()
    432432    intrans = False
    433433    inplural = False
    434434    singular = []
    435435    plural = []
     436    incomment = False
     437    comment = []
    436438    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:
    438447            if t.token_type == TOKEN_BLOCK:
    439448                endbmatch = endblock_re.match(t.contents)
    440449                pluralmatch = plural_re.match(t.contents)
    def templatize(src):  
    488497                elif cmatches:
    489498                    for cmatch in cmatches:
    490499                        out.write(' _(%s) ' % cmatch)
     500                elif t.contents == 'comment':
     501                    incomment = True
    491502                else:
    492503                    out.write(blankout(t.contents, 'B'))
    493504            elif t.token_type == TOKEN_VAR:
    def templatize(src):  
    500511                        out.write(' %s ' % p.split(':',1)[1])
    501512                    else:
    502513                        out.write(blankout(p, 'F'))
     514            elif t.token_type == TOKEN_COMMENT:
     515                out.write(' # %s' % t.contents)
    503516            else:
    504517                out.write(blankout(t.contents, 'X'))
    505518    return out.getvalue()
  • tests/regressiontests/i18n/commands/extraction.py

    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):  
    4747        po_contents = open(self.PO_FILE, 'r').read()
    4848        self.assert_('#. L10n: This comment should be extracted' in po_contents)
    4949        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)
    5053
    5154    def test_templatize(self):
    5255        os.chdir(self.test_dir)
  • tests/regressiontests/i18n/commands/templates/test.html

    diff --git a/tests/regressiontests/i18n/commands/templates/test.html b/tests/regressiontests/i18n/commands/templates/test.html
    index e1c3eee..ca2f952 100644
    a b  
    11{% load i18n %}
     2{% comment %} L10n: Django comment block for translators {% endcomment %}
    23{% trans "This literal should be included." %}
    34{% 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 %}
Back to Top