summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJulien Phalip <jphalip@yahoo.fr>2012-05-29 22:47:08 -0700
committerJulien Phalip <jphalip@yahoo.fr>2012-05-29 22:47:08 -0700
commit4b28ea37e52e18720c1c2a6ad611dee986126dd5 (patch)
treef6851d5ca3037d8676e3c37157f8935c38a330c3
parent28e424532fdeaf05053e1b531eeacbfa831af1fb (diff)
parent432339a72c20f78abf1999892a39c71b5d853174 (diff)
Merge pull request #94 from jphalip/tickets/18393-blocktrans-valueerror
Fixed #18393 -- Prevented blocktrans to crash when a variable name is badly formatted.
-rw-r--r--django/templatetags/i18n.py2
-rw-r--r--tests/regressiontests/i18n/other/locale/fr/LC_MESSAGES/django.mobin454 -> 528 bytes
-rw-r--r--tests/regressiontests/i18n/other/locale/fr/LC_MESSAGES/django.po6
-rw-r--r--tests/regressiontests/i18n/tests.py16
4 files changed, 22 insertions, 2 deletions
diff --git a/django/templatetags/i18n.py b/django/templatetags/i18n.py
index ca79f41edc..e090781080 100644
--- a/django/templatetags/i18n.py
+++ b/django/templatetags/i18n.py
@@ -148,7 +148,7 @@ class BlockTranslateNode(Node):
context.pop()
try:
result = result % data
- except KeyError:
+ except (KeyError, ValueError):
with translation.override(None):
result = self.render(context)
return result
diff --git a/tests/regressiontests/i18n/other/locale/fr/LC_MESSAGES/django.mo b/tests/regressiontests/i18n/other/locale/fr/LC_MESSAGES/django.mo
index f0a2179767..478338bc88 100644
--- a/tests/regressiontests/i18n/other/locale/fr/LC_MESSAGES/django.mo
+++ b/tests/regressiontests/i18n/other/locale/fr/LC_MESSAGES/django.mo
Binary files differ
diff --git a/tests/regressiontests/i18n/other/locale/fr/LC_MESSAGES/django.po b/tests/regressiontests/i18n/other/locale/fr/LC_MESSAGES/django.po
index 8e9b9e1034..dafb6139ae 100644
--- a/tests/regressiontests/i18n/other/locale/fr/LC_MESSAGES/django.po
+++ b/tests/regressiontests/i18n/other/locale/fr/LC_MESSAGES/django.po
@@ -17,5 +17,11 @@ msgstr ""
"Plural-Forms: nplurals=2; plural=(n != 1)\n"
#: template.html:3
+# Note: Intentional: variable name is translated.
msgid "My name is %(person)s."
msgstr "Mon nom est %(personne)s."
+
+#: template.html:3
+# Note: Intentional: the variable name is badly formatted (missing 's' at the end)
+msgid "My other name is %(person)s."
+msgstr "Mon autre nom est %(person)." \ No newline at end of file
diff --git a/tests/regressiontests/i18n/tests.py b/tests/regressiontests/i18n/tests.py
index 5e95ee36ec..78e41321af 100644
--- a/tests/regressiontests/i18n/tests.py
+++ b/tests/regressiontests/i18n/tests.py
@@ -270,10 +270,11 @@ class TranslationTests(TestCase):
self.assertEqual(to_language('sr_Lat'), 'sr-lat')
@override_settings(LOCALE_PATHS=(os.path.join(here, 'other', 'locale'),))
- def test_bad_placeholder(self):
+ def test_bad_placeholder_1(self):
"""
Error in translation file should not crash template rendering
(%(person)s is translated as %(personne)s in fr.po)
+ Refs #16516.
"""
from django.template import Template, Context
with translation.override('fr'):
@@ -281,6 +282,19 @@ class TranslationTests(TestCase):
rendered = t.render(Context({'person': 'James'}))
self.assertEqual(rendered, 'My name is James.')
+ @override_settings(LOCALE_PATHS=(os.path.join(here, 'other', 'locale'),))
+ def test_bad_placeholder_2(self):
+ """
+ Error in translation file should not crash template rendering
+ (%(person) misses a 's' in fr.po, causing the string formatting to fail)
+ Refs #18393.
+ """
+ from django.template import Template, Context
+ with translation.override('fr'):
+ t = Template('{% load i18n %}{% blocktrans %}My other name is {{ person }}.{% endblocktrans %}')
+ rendered = t.render(Context({'person': 'James'}))
+ self.assertEqual(rendered, 'My other name is James.')
+
class FormattingTests(TestCase):