From ff7556c4ec01395fe084746009af32ce91b2109f Mon Sep 17 00:00:00 2001 From: Ramiro Morales Date: Sun, 11 Dec 2011 00:07:06 +0000 Subject: [PATCH] Fixed #11240 -- Made makemessages i18n command escape % symbols in literals passed to the trans tag. This avoids problems with unintended automatic detection, marking and validation of Python string formatting specifiers performed by xgettext(1)/msgfmt(1) that stem from the fact that under the hood makemessages converts templates to Python code before passing them to xgettext. This also makes it consistent with its behavior on literals passed to the blocktrans tag. Thanks Jannis and claude for reviewing and feedback. git-svn-id: http://code.djangoproject.com/svn/django/trunk@17190 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- django/utils/translation/trans_real.py | 2 + .../i18n/commands/compilation.py | 63 +++++- .../i18n/commands/extraction.py | 183 +++++++++++------- .../commands/locale/fr/LC_MESSAGES/django.po | 71 +++++++ .../i18n/commands/templates/test.html | 11 +- tests/regressiontests/i18n/tests.py | 3 +- 6 files changed, 253 insertions(+), 80 deletions(-) create mode 100644 tests/regressiontests/i18n/commands/locale/fr/LC_MESSAGES/django.po diff --git a/django/utils/translation/trans_real.py b/django/utils/translation/trans_real.py index f43cfd9705..3addf1ddfb 100644 --- a/django/utils/translation/trans_real.py +++ b/django/utils/translation/trans_real.py @@ -439,6 +439,7 @@ block_re = re.compile(r"""^\s*blocktrans(\s+.*context\s+(?:"[^"]*?")|(?:'[^']*?' endblock_re = re.compile(r"""^\s*endblocktrans$""") plural_re = re.compile(r"""^\s*plural$""") constant_re = re.compile(r"""_\(((?:".*?")|(?:'.*?'))\)""") +one_percent_re = re.compile(r"""(?, YEAR. +# +msgid "" +msgstr "" +"Project-Id-Version: PACKAGE VERSION\n" +"Report-Msgid-Bugs-To: \n" +"POT-Creation-Date: 2011-12-04 04:59-0600\n" +"PO-Revision-Date: 2011-12-10 19:12-0300\n" +"Last-Translator: FULL NAME \n" +"Language-Team: LANGUAGE \n" +"Language: fr\n" +"MIME-Version: 1.0\n" +"Content-Type: text/plain; charset=UTF-8\n" +"Content-Transfer-Encoding: 8bit\n" +"Plural-Forms: nplurals=2; plural=(n != 1)\n" + +#. Translators: Django template comment for translators +#: templates/test.html:9 +#, python-format +msgid "I think that 100%% is more that 50%% of anything." +msgstr "" + +#: templates/test.html:10 +#, python-format +msgid "I think that 100%% is more that 50%% of %(obj)s." +msgstr "" + +#: templates/test.html:70 +#, python-format +msgid "Literal with a percent symbol at the end %%" +msgstr "" + +#: templates/test.html:71 +#, python-format +msgid "Literal with a percent %% symbol in the middle" +msgstr "" + +#: templates/test.html:72 +#, python-format +msgid "Completed 50%% of all the tasks" +msgstr "" + +#: templates/test.html:73 +#, python-format +msgctxt "ctx0" +msgid "Completed 99%% of all the tasks" +msgstr "" + +#: templates/test.html:74 +#, python-format +msgid "Shouldn't double escape this sequence: %% (two percent signs)" +msgstr "" + +#: templates/test.html:75 +#, python-format +msgctxt "ctx1" +msgid "Shouldn't double escape this sequence %% either" +msgstr "" + +#: templates/test.html:76 +#, python-format +msgid "Looks like a str fmt spec %%s but shouldn't be interpreted as such" +msgstr "Translation of the above string" + +#: templates/test.html:77 +#, python-format +msgid "Looks like a str fmt spec %% o but shouldn't be interpreted as such" +msgstr "Translation contains %% for the above string" diff --git a/tests/regressiontests/i18n/commands/templates/test.html b/tests/regressiontests/i18n/commands/templates/test.html index 24fc708621..6f794af789 100644 --- a/tests/regressiontests/i18n/commands/templates/test.html +++ b/tests/regressiontests/i18n/commands/templates/test.html @@ -65,4 +65,13 @@ continued here.{% endcomment %} {% blocktrans context "Special blocktrans context #1" %}Translatable literal #8a{% endblocktrans %} {% blocktrans count 2 context "Special blocktrans context #2" %}Translatable literal #8b-singular{% plural %}Translatable literal #8b-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 %} \ No newline at end of file +{% blocktrans with a=1 context "Special blocktrans context #4" %}Translatable literal #8d {{ a }}{% endblocktrans %} + +{% trans "Literal with a percent symbol at the end %" %} +{% trans "Literal with a percent % symbol in the middle" %} +{% trans "Completed 50% of all the tasks" %} +{% trans "Completed 99% of all the tasks" context "ctx0" %} +{% trans "Shouldn't double escape this sequence: %% (two percent signs)" %} +{% trans "Shouldn't double escape this sequence %% either" context "ctx1" %} +{% trans "Looks like a str fmt spec %s but shouldn't be interpreted as such" %} +{% trans "Looks like a str fmt spec % o but shouldn't be interpreted as such" %} diff --git a/tests/regressiontests/i18n/tests.py b/tests/regressiontests/i18n/tests.py index 33b16886cb..cc17d74315 100644 --- a/tests/regressiontests/i18n/tests.py +++ b/tests/regressiontests/i18n/tests.py @@ -31,7 +31,8 @@ if can_run_extraction_tests: CopyPluralFormsExtractorTests, NoWrapExtractorTests, NoLocationExtractorTests) if can_run_compilation_tests: - from .commands.compilation import MessageCompilationTests, PoFileTests + from .commands.compilation import (PoFileTests, PoFileContentsTests, + PercentRenderingTests) from .contenttypes.tests import ContentTypeTests from .forms import I18nForm, SelectDateForm, SelectDateWidget, CompanyForm from .models import Company, TestModel