Skip to content

Commit

Permalink
Made makemessages leave '%%' sequences untouched when extracting tr…
Browse files Browse the repository at this point in the history
…anslatable literals from blocktrans template tags.

This makes it consistent with behavior introduced when fixing #11240 in
processing of literal passed to the trans tag to avoid double escaping
(i.e. `'%%%%'` sequences in resulting PO files.)

Also, cleaned up tests changes from r17190 (removed commented out code and
implemented compatibility with Python 2.5.)

git-svn-id: http://code.djangoproject.com/svn/django/trunk@17192 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information
ramiro committed Dec 11, 2011
1 parent 12a20b6 commit f2e99ec
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 13 deletions.
2 changes: 1 addition & 1 deletion django/utils/translation/trans_real.py
Expand Up @@ -514,7 +514,7 @@ def templatize(src, origin=None):
else: else:
singular.append('%%(%s)s' % t.contents) singular.append('%%(%s)s' % t.contents)
elif t.token_type == TOKEN_TEXT: elif t.token_type == TOKEN_TEXT:
contents = t.contents.replace('%', '%%') contents = one_percent_re.sub('%%', t.contents)
if inplural: if inplural:
plural.append(contents) plural.append(contents)
else: else:
Expand Down
15 changes: 3 additions & 12 deletions tests/regressiontests/i18n/commands/extraction.py
@@ -1,4 +1,6 @@
# -*- encoding: utf-8 -*- # -*- encoding: utf-8 -*-
from __future__ import with_statement

import os import os
import re import re
import shutil import shutil
Expand Down Expand Up @@ -52,7 +54,6 @@ def test_comments_extractor(self):
os.chdir(self.test_dir) os.chdir(self.test_dir)
management.call_command('makemessages', locale=LOCALE, verbosity=0) management.call_command('makemessages', locale=LOCALE, verbosity=0)
self.assertTrue(os.path.exists(self.PO_FILE)) self.assertTrue(os.path.exists(self.PO_FILE))
#po_contents = open(self.PO_FILE, 'r').read()
with open(self.PO_FILE, 'r') as fp: with open(self.PO_FILE, 'r') as fp:
po_contents = fp.read() po_contents = fp.read()
self.assertTrue('#. Translators: This comment should be extracted' in po_contents) self.assertTrue('#. Translators: This comment should be extracted' in po_contents)
Expand Down Expand Up @@ -81,7 +82,6 @@ def test_templatize_trans_tag(self):
os.chdir(self.test_dir) os.chdir(self.test_dir)
management.call_command('makemessages', locale=LOCALE, verbosity=0) management.call_command('makemessages', locale=LOCALE, verbosity=0)
self.assertTrue(os.path.exists(self.PO_FILE)) self.assertTrue(os.path.exists(self.PO_FILE))
#po_contents = open(self.PO_FILE, 'r').read()
with open(self.PO_FILE, 'r') as fp: with open(self.PO_FILE, 'r') as fp:
po_contents = fp.read() po_contents = fp.read()
self.assertMsgId('Literal with a percent symbol at the end %%', po_contents) self.assertMsgId('Literal with a percent symbol at the end %%', po_contents)
Expand All @@ -98,11 +98,11 @@ def test_templatize_blocktrans_tag(self):
os.chdir(self.test_dir) os.chdir(self.test_dir)
management.call_command('makemessages', locale=LOCALE, verbosity=0) management.call_command('makemessages', locale=LOCALE, verbosity=0)
self.assertTrue(os.path.exists(self.PO_FILE)) self.assertTrue(os.path.exists(self.PO_FILE))
#po_contents = open(self.PO_FILE, 'r').read()
with open(self.PO_FILE, 'r') as fp: with open(self.PO_FILE, 'r') as fp:
po_contents = fp.read() po_contents = fp.read()
self.assertMsgId('I think that 100%% is more that 50%% of anything.', po_contents) self.assertMsgId('I think that 100%% is more that 50%% of anything.', po_contents)
self.assertMsgId('I think that 100%% is more that 50%% of %(obj)s.', po_contents) self.assertMsgId('I think that 100%% is more that 50%% of %(obj)s.', po_contents)
self.assertMsgId("Blocktrans extraction shouldn't double escape this: %%, a=%(a)s", po_contents)


def test_extraction_error(self): def test_extraction_error(self):
os.chdir(self.test_dir) os.chdir(self.test_dir)
Expand All @@ -128,7 +128,6 @@ def test_template_message_context_extractor(self):
os.chdir(self.test_dir) os.chdir(self.test_dir)
management.call_command('makemessages', locale=LOCALE, verbosity=0) management.call_command('makemessages', locale=LOCALE, verbosity=0)
self.assertTrue(os.path.exists(self.PO_FILE)) self.assertTrue(os.path.exists(self.PO_FILE))
#po_contents = open(self.PO_FILE, 'r').read()
with open(self.PO_FILE, 'r') as fp: with open(self.PO_FILE, 'r') as fp:
po_contents = fp.read() po_contents = fp.read()
# {% trans %} # {% trans %}
Expand Down Expand Up @@ -159,7 +158,6 @@ def test_javascript_literals(self):
os.chdir(self.test_dir) os.chdir(self.test_dir)
management.call_command('makemessages', domain='djangojs', locale=LOCALE, verbosity=0) management.call_command('makemessages', domain='djangojs', locale=LOCALE, verbosity=0)
self.assertTrue(os.path.exists(self.PO_FILE)) self.assertTrue(os.path.exists(self.PO_FILE))
#po_contents = open(self.PO_FILE, 'r').read()
with open(self.PO_FILE, 'r') as fp: with open(self.PO_FILE, 'r') as fp:
po_contents = fp.read() po_contents = fp.read()
self.assertMsgId('This literal should be included.', po_contents) self.assertMsgId('This literal should be included.', po_contents)
Expand All @@ -183,7 +181,6 @@ def test_ignore_option(self):
pattern1 = os.path.join('ignore_dir', '*') pattern1 = os.path.join('ignore_dir', '*')
management.call_command('makemessages', locale=LOCALE, verbosity=0, ignore_patterns=[pattern1]) management.call_command('makemessages', locale=LOCALE, verbosity=0, ignore_patterns=[pattern1])
self.assertTrue(os.path.exists(self.PO_FILE)) self.assertTrue(os.path.exists(self.PO_FILE))
#po_contents = open(self.PO_FILE, 'r').read()
with open(self.PO_FILE, 'r') as fp: with open(self.PO_FILE, 'r') as fp:
po_contents = fp.read() po_contents = fp.read()
self.assertMsgId('This literal should be included.', po_contents) self.assertMsgId('This literal should be included.', po_contents)
Expand Down Expand Up @@ -215,7 +212,6 @@ def test_symlink(self):
os.chdir(self.test_dir) os.chdir(self.test_dir)
management.call_command('makemessages', locale=LOCALE, verbosity=0, symlinks=True) management.call_command('makemessages', locale=LOCALE, verbosity=0, symlinks=True)
self.assertTrue(os.path.exists(self.PO_FILE)) self.assertTrue(os.path.exists(self.PO_FILE))
#po_contents = open(self.PO_FILE, 'r').read()
with open(self.PO_FILE, 'r') as fp: with open(self.PO_FILE, 'r') as fp:
po_contents = fp.read() po_contents = fp.read()
self.assertMsgId('This literal should be included.', po_contents) self.assertMsgId('This literal should be included.', po_contents)
Expand All @@ -228,7 +224,6 @@ def test_copy_plural_forms(self):
os.chdir(self.test_dir) os.chdir(self.test_dir)
management.call_command('makemessages', locale=LOCALE, verbosity=0) management.call_command('makemessages', locale=LOCALE, verbosity=0)
self.assertTrue(os.path.exists(self.PO_FILE)) self.assertTrue(os.path.exists(self.PO_FILE))
#po_contents = open(self.PO_FILE, 'r').read()
with open(self.PO_FILE, 'r') as fp: with open(self.PO_FILE, 'r') as fp:
po_contents = fp.read() po_contents = fp.read()
self.assertTrue('Plural-Forms: nplurals=2; plural=(n != 1)' in po_contents) self.assertTrue('Plural-Forms: nplurals=2; plural=(n != 1)' in po_contents)
Expand All @@ -240,7 +235,6 @@ def test_no_wrap_enabled(self):
os.chdir(self.test_dir) os.chdir(self.test_dir)
management.call_command('makemessages', locale=LOCALE, verbosity=0, no_wrap=True) management.call_command('makemessages', locale=LOCALE, verbosity=0, no_wrap=True)
self.assertTrue(os.path.exists(self.PO_FILE)) self.assertTrue(os.path.exists(self.PO_FILE))
#po_contents = open(self.PO_FILE, 'r').read()
with open(self.PO_FILE, 'r') as fp: with open(self.PO_FILE, 'r') as fp:
po_contents = fp.read() po_contents = fp.read()
self.assertMsgId('This literal should also be included wrapped or not wrapped depending on the use of the --no-wrap option.', po_contents) self.assertMsgId('This literal should also be included wrapped or not wrapped depending on the use of the --no-wrap option.', po_contents)
Expand All @@ -249,7 +243,6 @@ def test_no_wrap_disabled(self):
os.chdir(self.test_dir) os.chdir(self.test_dir)
management.call_command('makemessages', locale=LOCALE, verbosity=0, no_wrap=False) management.call_command('makemessages', locale=LOCALE, verbosity=0, no_wrap=False)
self.assertTrue(os.path.exists(self.PO_FILE)) self.assertTrue(os.path.exists(self.PO_FILE))
#po_contents = open(self.PO_FILE, 'r').read()
with open(self.PO_FILE, 'r') as fp: with open(self.PO_FILE, 'r') as fp:
po_contents = fp.read() po_contents = fp.read()
self.assertMsgId('""\n"This literal should also be included wrapped or not wrapped depending on the "\n"use of the --no-wrap option."', po_contents, use_quotes=False) self.assertMsgId('""\n"This literal should also be included wrapped or not wrapped depending on the "\n"use of the --no-wrap option."', po_contents, use_quotes=False)
Expand All @@ -261,7 +254,6 @@ def test_no_location_enabled(self):
os.chdir(self.test_dir) os.chdir(self.test_dir)
management.call_command('makemessages', locale=LOCALE, verbosity=0, no_location=True) management.call_command('makemessages', locale=LOCALE, verbosity=0, no_location=True)
self.assertTrue(os.path.exists(self.PO_FILE)) self.assertTrue(os.path.exists(self.PO_FILE))
#po_contents = open(self.PO_FILE, 'r').read()
with open(self.PO_FILE, 'r') as fp: with open(self.PO_FILE, 'r') as fp:
po_contents = fp.read() po_contents = fp.read()
self.assertFalse('#: templates/test.html:55' in po_contents) self.assertFalse('#: templates/test.html:55' in po_contents)
Expand All @@ -270,7 +262,6 @@ def test_no_location_disabled(self):
os.chdir(self.test_dir) os.chdir(self.test_dir)
management.call_command('makemessages', locale=LOCALE, verbosity=0, no_location=False) management.call_command('makemessages', locale=LOCALE, verbosity=0, no_location=False)
self.assertTrue(os.path.exists(self.PO_FILE)) self.assertTrue(os.path.exists(self.PO_FILE))
#po_contents = open(self.PO_FILE, 'r').read()
with open(self.PO_FILE, 'r') as fp: with open(self.PO_FILE, 'r') as fp:
po_contents = fp.read() po_contents = fp.read()
self.assertTrue('#: templates/test.html:55' in po_contents) self.assertTrue('#: templates/test.html:55' in po_contents)
2 changes: 2 additions & 0 deletions tests/regressiontests/i18n/commands/templates/test.html
Expand Up @@ -67,6 +67,8 @@
{% blocktrans context "Special blocktrans context #3" count 2 %}Translatable literal #8c-singular{% plural %}Translatable literal #8c-plural{% endblocktrans %} {% blocktrans context "Special blocktrans context #3" count 2 %}Translatable literal #8c-singular{% plural %}Translatable literal #8c-plural{% endblocktrans %}
{% blocktrans with a=1 context "Special blocktrans context #4" %}Translatable literal #8d {{ a }}{% endblocktrans %} {% blocktrans with a=1 context "Special blocktrans context #4" %}Translatable literal #8d {{ a }}{% endblocktrans %}


{% blocktrans with a=1 %}Blocktrans extraction shouldn't double escape this: %%, a={{ a }}{% endblocktrans %}

{% trans "Literal with a percent symbol at the end %" %} {% trans "Literal with a percent symbol at the end %" %}
{% trans "Literal with a percent % symbol in the middle" %} {% trans "Literal with a percent % symbol in the middle" %}
{% trans "Completed 50% of all the tasks" %} {% trans "Completed 50% of all the tasks" %}
Expand Down

0 comments on commit f2e99ec

Please sign in to comment.