Code

Ticket #16516: 16516.diff

File 16516.diff, 3.8 KB (added by claudep, 3 years ago)

Fix KeyError, with tests

Line 
1diff --git a/django/templatetags/i18n.py b/django/templatetags/i18n.py
2index c7202e2..ebb4d02 100644
3--- a/django/templatetags/i18n.py
4+++ b/django/templatetags/i18n.py
5@@ -122,7 +122,12 @@ class BlockTranslateNode(Node):
6         result = re.sub(u'%(?!\()', u'%%', result)
7         data = dict([(v, _render_value_in_context(context.get(v, ''), context)) for v in vars])
8         context.pop()
9-        return result % data
10+        try:
11+            result = result % data
12+        except KeyError:
13+            with translation.override('en'):
14+                result = self.render(context)
15+        return result
16 
17 
18 class LanguageNode(Node):
19diff --git a/tests/regressiontests/i18n/other/locale/fr/LC_MESSAGES/django.mo b/tests/regressiontests/i18n/other/locale/fr/LC_MESSAGES/django.mo
20new file mode 100644
21index 0000000000000000000000000000000000000000..f0a21797676abbe377b4637c57fe40fce594a5b2
22GIT binary patch
23literal 454
24zcmYL_%}T>S5XYnFB}Wf};9)!nEjl%u3N71K#M(l^U@g)6+HBJ;$!^%)h<y;>!e{bX
25zoK`Ty{+JK`Gds-B$<bFpc1WBP*TgX~Bz86@PKg&{d=La{@@K?1#q{=l<Qhx1phYP`
26zIk;?9(mJEtt`p3y*~q2GS%z;^R7P{iHbUnHyma1$3t1WKF>$%d@S)BfriMc}e@=01
27zrSQ~&kHSk1al|5wVq~|#dc51cqC6rYF64*WO>T4yBZe`9nDK58qb}*5g!7nMp<N-o
28zu^h&Qs541*o!26lm`YJ{SXmgZ?0um+#^!fnGD{}S;;a*eW23#)G-_MXPF{Zco2n2>
29z4`8_#)=58lO{dszbh|HC(&9ufO{R3tq5rOYI4^1|3Y;2SIu5$pr*0Gv;4d7tG+c~;
30UwZkAW8nh{a?)86LEqB-b1z6B~F#rGn
31
32literal 0
33HcmV?d00001
34
35diff --git a/tests/regressiontests/i18n/other/locale/fr/LC_MESSAGES/django.po b/tests/regressiontests/i18n/other/locale/fr/LC_MESSAGES/django.po
36new file mode 100644
37index 0000000..8e9b9e1
38--- /dev/null
39+++ b/tests/regressiontests/i18n/other/locale/fr/LC_MESSAGES/django.po
40@@ -0,0 +1,21 @@
41+# SOME DESCRIPTIVE TITLE.
42+# Copyright (C) YEAR THE PACKAGE'S COPYRIGHT HOLDER
43+# This file is distributed under the same license as the PACKAGE package.
44+# FIRST AUTHOR <EMAIL@ADDRESS>, YEAR.
45+#
46+msgid ""
47+msgstr ""
48+"Project-Id-Version: django tests\n"
49+"Report-Msgid-Bugs-To: \n"
50+"POT-Creation-Date: 2010-02-14 17:33+0100\n"
51+"PO-Revision-Date: 2011-01-21 21:37-0300\n"
52+"Last-Translator: Claude\n"
53+"Language-Team: fr <fr@li.org>\n"
54+"MIME-Version: 1.0\n"
55+"Content-Type: text/plain; charset=UTF-8\n"
56+"Content-Transfer-Encoding: 8bit\n"
57+"Plural-Forms: nplurals=2; plural=(n != 1)\n"
58+
59+#: template.html:3
60+msgid "My name is %(person)s."
61+msgstr "Mon nom est %(personne)s."
62diff --git a/tests/regressiontests/i18n/tests.py b/tests/regressiontests/i18n/tests.py
63index 0b955ad..e58c89c 100644
64--- a/tests/regressiontests/i18n/tests.py
65+++ b/tests/regressiontests/i18n/tests.py
66@@ -9,6 +9,7 @@ from threading import local
67 from django.conf import settings
68 from django.template import Template, Context
69 from django.test import TestCase, RequestFactory
70+from django.test.utils import override_settings
71 from django.utils.formats import (get_format, date_format, time_format,
72     localize, localize_input, iter_format_modules, get_format_modules)
73 from django.utils.importlib import import_module
74@@ -129,6 +130,23 @@ class TranslationTests(TestCase):
75         self.assertEqual(to_language('en_US'), 'en-us')
76         self.assertEqual(to_language('sr_Lat'), 'sr-lat')
77 
78+    @override_settings(
79+        LOCALE_PATHS = (
80+            os.path.join(os.path.dirname(os.path.abspath(__file__)), 'other', 'locale'),
81+        ),
82+    )
83+    def test_bad_placeholder(self):
84+        """
85+        Error in translation file should not crash template rendering
86+        (%(person)s is translated as %(personne)s in fr.po)
87+        """
88+        from django.template import Template, Context
89+        with translation.override('fr'):
90+            t = Template('{% load i18n %}{% blocktrans %}My name is {{ person }}.{% endblocktrans %}')
91+            c = Context({'person': u"James"})
92+            rendered = t.render(c)
93+            self.assertEqual(rendered, u"My name is James.")
94+
95 
96 class FormattingTests(TestCase):
97 
98--
991.7.2.5
100