Author: jezdez
Date: 2011-09-08 06:24:41 -0700 (Thu, 08 Sep 2011)
New Revision: 16723

Added:
   django/trunk/tests/regressiontests/i18n/other/locale/fr/
   django/trunk/tests/regressiontests/i18n/other/locale/fr/LC_MESSAGES/
   django/trunk/tests/regressiontests/i18n/other/locale/fr/LC_MESSAGES/django.mo
   django/trunk/tests/regressiontests/i18n/other/locale/fr/LC_MESSAGES/django.po
Modified:
   django/trunk/django/templatetags/i18n.py
   django/trunk/django/utils/translation/__init__.py
   django/trunk/docs/ref/utils.txt
   django/trunk/docs/topics/i18n/internationalization.txt
   django/trunk/tests/regressiontests/i18n/tests.py
Log:
Fixed #16516 -- Relaxed the blocktrans rendering a little by falling back to 
the default language if resolving one of the arguments fails, raising a 
KeyError. Thanks, Claude Paroz and Aymeric Augustin.

Modified: django/trunk/django/templatetags/i18n.py
===================================================================
--- django/trunk/django/templatetags/i18n.py    2011-09-04 22:51:53 UTC (rev 
16722)
+++ django/trunk/django/templatetags/i18n.py    2011-09-08 13:24:41 UTC (rev 
16723)
@@ -128,7 +128,12 @@
         result = re.sub(u'%(?!\()', u'%%', result)
         data = dict([(v, _render_value_in_context(context.get(v, ''), 
context)) for v in vars])
         context.pop()
-        return result % data
+        try:
+            result = result % data
+        except KeyError:
+            with translation.override(None):
+                result = self.render(context)
+        return result
 
 
 class LanguageNode(Node):

Modified: django/trunk/django/utils/translation/__init__.py
===================================================================
--- django/trunk/django/utils/translation/__init__.py   2011-09-04 22:51:53 UTC 
(rev 16722)
+++ django/trunk/django/utils/translation/__init__.py   2011-09-08 13:24:41 UTC 
(rev 16723)
@@ -115,7 +115,10 @@
         self.old_language = get_language()
 
     def __enter__(self):
-        activate(self.language)
+        if self.language is not None:
+            activate(self.language)
+        else:
+            deactivate_all()
 
     def __exit__(self, exc_type, exc_value, traceback):
         if self.deactivate:

Modified: django/trunk/docs/ref/utils.txt
===================================================================
--- django/trunk/docs/ref/utils.txt     2011-09-04 22:51:53 UTC (rev 16722)
+++ django/trunk/docs/ref/utils.txt     2011-09-08 13:24:41 UTC (rev 16723)
@@ -532,15 +532,18 @@
     useful when we want delayed translations to appear as the original string
     for some reason.
 
-.. function:: override(language)
+.. function:: override(language, deactivate=False)
 
     .. versionadded:: 1.4
 
     A Python context manager that uses
     :func:`django.utils.translation.activate` to fetch the translation object
     for a given language, installing it as the translation object for the
-    current thread and deinstalls it again on exit with
-    :func:`django.utils.translation.deactivate`.
+    current thread and reinstall the previous active language on exit.
+    Optionally it can simply deinstall the temporary translation on exit with
+    :func:`django.utils.translation.deactivate` if the deactivate argument is
+    True. If you pass None as the language argument, a NullTranslations()
+    instance is installed while the context is active.
 
 .. function:: get_language()
 

Modified: django/trunk/docs/topics/i18n/internationalization.txt
===================================================================
--- django/trunk/docs/topics/i18n/internationalization.txt      2011-09-04 
22:51:53 UTC (rev 16722)
+++ django/trunk/docs/topics/i18n/internationalization.txt      2011-09-08 
13:24:41 UTC (rev 16723)
@@ -512,6 +512,13 @@
 .. note:: The previous more verbose format is still supported:
    ``{% blocktrans with book|title as book_t and author|title as author_t %}``
 
+.. versionchanged:: 1.4
+
+If resolving one of the block arguments fails, blocktrans will fall back to
+the default language by deactivating the currently active language
+temporarily with the :func:`~django.utils.translation.deactivate_all`
+function.
+
 This tag also provides for pluralization. To use it:
 
     * Designate and bind a counter value with the name ``count``. This value 
will

Added: 
django/trunk/tests/regressiontests/i18n/other/locale/fr/LC_MESSAGES/django.mo
===================================================================
--- 
django/trunk/tests/regressiontests/i18n/other/locale/fr/LC_MESSAGES/django.mo   
                            (rev 0)
+++ 
django/trunk/tests/regressiontests/i18n/other/locale/fr/LC_MESSAGES/django.mo   
    2011-09-08 13:24:41 UTC (rev 16723)
@@ -0,0 +1,11 @@
+Þ  •            ,       <       P       Q   C   h       ¬                      
  My name is %(person)s. Project-Id-Version: django tests
+Report-Msgid-Bugs-To: 
+POT-Creation-Date: 2010-02-14 17:33+0100
+PO-Revision-Date: 2011-01-21 21:37-0300
+Last-Translator: Claude
+Language-Team: fr <[email protected]>
+MIME-Version: 1.0
+Content-Type: text/plain; charset=UTF-8
+Content-Transfer-Encoding: 8bit
+Plural-Forms: nplurals=2; plural=(n != 1)
+ Mon nom est %(personne)s. 
\ No newline at end of file

Added: 
django/trunk/tests/regressiontests/i18n/other/locale/fr/LC_MESSAGES/django.po
===================================================================
--- 
django/trunk/tests/regressiontests/i18n/other/locale/fr/LC_MESSAGES/django.po   
                            (rev 0)
+++ 
django/trunk/tests/regressiontests/i18n/other/locale/fr/LC_MESSAGES/django.po   
    2011-09-08 13:24:41 UTC (rev 16723)
@@ -0,0 +1,21 @@
+# SOME DESCRIPTIVE TITLE.
+# Copyright (C) YEAR THE PACKAGE'S COPYRIGHT HOLDER
+# This file is distributed under the same license as the PACKAGE package.
+# FIRST AUTHOR <EMAIL@ADDRESS>, YEAR.
+#
+msgid ""
+msgstr ""
+"Project-Id-Version: django tests\n"
+"Report-Msgid-Bugs-To: \n"
+"POT-Creation-Date: 2010-02-14 17:33+0100\n"
+"PO-Revision-Date: 2011-01-21 21:37-0300\n"
+"Last-Translator: Claude\n"
+"Language-Team: fr <[email protected]>\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"
+
+#: template.html:3
+msgid "My name is %(person)s."
+msgstr "Mon nom est %(personne)s."

Modified: django/trunk/tests/regressiontests/i18n/tests.py
===================================================================
--- django/trunk/tests/regressiontests/i18n/tests.py    2011-09-04 22:51:53 UTC 
(rev 16722)
+++ django/trunk/tests/regressiontests/i18n/tests.py    2011-09-08 13:24:41 UTC 
(rev 16723)
@@ -9,6 +9,7 @@
 from django.conf import settings
 from django.template import Template, Context
 from django.test import TestCase, RequestFactory
+from django.test.utils import override_settings
 from django.utils.formats import (get_format, date_format, time_format,
     localize, localize_input, iter_format_modules, get_format_modules)
 from django.utils.importlib import import_module
@@ -27,6 +28,8 @@
 from patterns.tests import *
 from test_warnings import DeprecationWarningTests
 
+here = os.path.dirname(os.path.abspath(__file__))
+
 class TranslationTests(TestCase):
 
     def test_override(self):
@@ -34,6 +37,9 @@
         with translation.override('pl'):
             self.assertEqual(get_language(), 'pl')
         self.assertEqual(get_language(), 'de')
+        with translation.override(None):
+            self.assertEqual(get_language(), settings.LANGUAGE_CODE)
+        self.assertEqual(get_language(), 'de')
         deactivate()
 
     def test_lazy_objects(self):
@@ -67,7 +73,7 @@
     def test_pgettext(self):
         # Reset translation catalog to include other/locale/de
         extended_locale_paths = settings.LOCALE_PATHS + (
-            os.path.join(os.path.dirname(os.path.abspath(__file__)), 'other', 
'locale'),
+            os.path.join(here, 'other', 'locale'),
         )
         with self.settings(LOCALE_PATHS=extended_locale_paths):
             from django.utils.translation import trans_real
@@ -129,7 +135,19 @@
         self.assertEqual(to_language('en_US'), 'en-us')
         self.assertEqual(to_language('sr_Lat'), 'sr-lat')
 
+    @override_settings(LOCALE_PATHS=(os.path.join(here, 'other', 'locale'),))
+    def test_bad_placeholder(self):
+        """
+        Error in translation file should not crash template rendering
+        (%(person)s is translated as %(personne)s in fr.po)
+        """
+        from django.template import Template, Context
+        with translation.override('fr'):
+            t = Template('{% load i18n %}{% blocktrans %}My name is {{ person 
}}.{% endblocktrans %}')
+            rendered = t.render(Context({'person': 'James'}))
+            self.assertEqual(rendered, 'My name is James.')
 
+
 class FormattingTests(TestCase):
 
     def setUp(self):
@@ -636,7 +654,7 @@
 
     def setUp(self):
         self.old_locale_paths = settings.LOCALE_PATHS
-        settings.LOCALE_PATHS += 
(os.path.join(os.path.dirname(os.path.abspath(__file__)), 'other', 'locale'),)
+        settings.LOCALE_PATHS += (os.path.join(here, 'other', 'locale'),)
         super(LocalePathsResolutionOrderI18NTests, self).setUp()
 
     def tearDown(self):

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/django-updates?hl=en.

Reply via email to