From 5b8c59305382e1453c8d73aa2282225964c78248 Mon Sep 17 00:00:00 2001 From: Ramiro Morales Date: Wed, 26 Jan 2011 15:44:11 +0000 Subject: [PATCH] [1.2.X] Fixed #15157 -- Modified evaluation of literals surrounded by '_(' and ')' in templates to be done at render time instead of at compile time by using ugettext_lazy. Thanks Jonathan S for the report. Backport of [15327] from trunk git-svn-id: http://code.djangoproject.com/svn/django/branches/releases/1.2.X@15328 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- django/template/__init__.py | 4 +- tests/regressiontests/i18n/tests.py | 134 +++++++++++++++++++++++++++- 2 files changed, 135 insertions(+), 3 deletions(-) diff --git a/django/template/__init__.py b/django/template/__init__.py index c3167861fd..8c89c67b23 100644 --- a/django/template/__init__.py +++ b/django/template/__init__.py @@ -59,7 +59,7 @@ from django.utils.itercompat import is_iterable from django.utils.functional import curry, Promise from django.utils.text import smart_split, unescape_string_literal, get_text_list from django.utils.encoding import smart_unicode, force_unicode, smart_str -from django.utils.translation import ugettext as _ +from django.utils.translation import ugettext_lazy from django.utils.safestring import SafeData, EscapeData, mark_safe, mark_for_escaping from django.utils.formats import localize from django.utils.html import escape @@ -698,7 +698,7 @@ class Variable(object): # We're dealing with a literal, so it's already been "resolved" value = self.literal if self.translate: - return _(value) + return ugettext_lazy(value) return value def __repr__(self): diff --git a/tests/regressiontests/i18n/tests.py b/tests/regressiontests/i18n/tests.py index f0ab434712..e9c4566227 100644 --- a/tests/regressiontests/i18n/tests.py +++ b/tests/regressiontests/i18n/tests.py @@ -11,7 +11,8 @@ from django.test import TestCase from django.utils.formats import get_format, date_format, time_format, localize, localize_input, iter_format_modules from django.utils.numberformat import format as nformat from django.utils.safestring import mark_safe, SafeString, SafeUnicode -from django.utils.translation import ugettext, ugettext_lazy, activate, deactivate, gettext_lazy, to_locale +from django.utils.translation import (ugettext, ugettext_lazy, activate, + deactivate, gettext_lazy, to_locale, get_language) from django.utils.importlib import import_module @@ -678,3 +679,134 @@ class TestModels(TestCase): c.save() c.name = SafeString(u'Iñtërnâtiônàlizætiøn1'.encode('utf-8')) c.save() + + +class MultipleLocaleActivationTests(TestCase): + """ + Tests for template rendering behavior when multiple locales are activated + during the lifetime of the same process. + """ + def setUp(self): + self._old_language = get_language() + + def tearDown(self): + activate(self._old_language) + + def test_single_locale_activation(self): + """ + Simple baseline behavior with one locale for all the supported i18n constructs. + """ + activate('fr') + self.assertEqual(Template("{{ _('Yes') }}").render(Context({})), 'Oui') + self.assertEqual(Template("{% load i18n %}{% trans 'Yes' %}").render(Context({})), 'Oui') + self.assertEqual(Template("{% load i18n %}{% blocktrans %}Yes{% endblocktrans %}").render(Context({})), 'Oui') + + # Literal marked up with _() in a filter expression + + def test_multiple_locale_filter(self): + activate('de') + t = Template("{% load i18n %}{{ 0|yesno:_('yes,no,maybe') }}") + activate(self._old_language) + activate('nl') + self.assertEqual(t.render(Context({})), 'nee') + + def test_multiple_locale_filter_deactivate(self): + activate('de') + t = Template("{% load i18n %}{{ 0|yesno:_('yes,no,maybe') }}") + deactivate() + activate('nl') + self.assertEqual(t.render(Context({})), 'nee') + + def test_multiple_locale_filter_direct_switch(self): + activate('de') + t = Template("{% load i18n %}{{ 0|yesno:_('yes,no,maybe') }}") + activate('nl') + self.assertEqual(t.render(Context({})), 'nee') + + # Literal marked up with _() + + def test_multiple_locale(self): + activate('de') + t = Template("{{ _('No') }}") + activate(self._old_language) + activate('nl') + self.assertEqual(t.render(Context({})), 'Nee') + + def test_multiple_locale_deactivate(self): + activate('de') + t = Template("{{ _('No') }}") + deactivate() + activate('nl') + self.assertEqual(t.render(Context({})), 'Nee') + + def test_multiple_locale_direct_switch(self): + activate('de') + t = Template("{{ _('No') }}") + activate('nl') + self.assertEqual(t.render(Context({})), 'Nee') + + # Literal marked up with _(), loading the i18n template tag library + + def test_multiple_locale_loadi18n(self): + activate('de') + t = Template("{% load i18n %}{{ _('No') }}") + activate(self._old_language) + activate('nl') + self.assertEqual(t.render(Context({})), 'Nee') + + def test_multiple_locale_loadi18n_deactivate(self): + activate('de') + t = Template("{% load i18n %}{{ _('No') }}") + deactivate() + activate('nl') + self.assertEqual(t.render(Context({})), 'Nee') + + def test_multiple_locale_loadi18n_direct_switch(self): + activate('de') + t = Template("{% load i18n %}{{ _('No') }}") + activate('nl') + self.assertEqual(t.render(Context({})), 'Nee') + + # trans i18n tag + + def test_multiple_locale_trans(self): + activate('de') + t = Template("{% load i18n %}{% trans 'No' %}") + activate(self._old_language) + activate('nl') + self.assertEqual(t.render(Context({})), 'Nee') + + def test_multiple_locale_deactivate_trans(self): + activate('de') + t = Template("{% load i18n %}{% trans 'No' %}") + deactivate() + activate('nl') + self.assertEqual(t.render(Context({})), 'Nee') + + def test_multiple_locale_direct_switch_trans(self): + activate('de') + t = Template("{% load i18n %}{% trans 'No' %}") + activate('nl') + self.assertEqual(t.render(Context({})), 'Nee') + + # blocktrans i18n tag + + def test_multiple_locale_btrans(self): + activate('de') + t = Template("{% load i18n %}{% blocktrans %}No{% endblocktrans %}") + activate(self._old_language) + activate('nl') + self.assertEqual(t.render(Context({})), 'Nee') + + def test_multiple_locale_deactivate_btrans(self): + activate('de') + t = Template("{% load i18n %}{% blocktrans %}No{% endblocktrans %}") + deactivate() + activate('nl') + self.assertEqual(t.render(Context({})), 'Nee') + + def test_multiple_locale_direct_switch_btrans(self): + activate('de') + t = Template("{% load i18n %}{% blocktrans %}No{% endblocktrans %}") + activate('nl') + self.assertEqual(t.render(Context({})), 'Nee')