Fixed #15157 -- Modified evaluation of literals surrounded by _(' and ')' in templates to be evaluated at render time instead of at compile time by using ugettext_lazy. Thanks Jonathan S for the report.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@15327 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
parent
69a803a8a6
commit
193f14d862
|
@ -9,7 +9,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
|
||||
|
@ -655,7 +655,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):
|
||||
|
|
|
@ -15,7 +15,7 @@ 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, pgettext, npgettext, to_locale,
|
||||
get_language_info)
|
||||
get_language_info, get_language)
|
||||
from django.utils.unittest import TestCase
|
||||
|
||||
|
||||
|
@ -743,3 +743,134 @@ class TestLanguageInfo(TestCase):
|
|||
self.assertEqual(li['name_local'], u'Deutsch')
|
||||
self.assertEqual(li['name'], 'German')
|
||||
self.assertEqual(li['bidi'], False)
|
||||
|
||||
|
||||
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')
|
||||
|
|
Loading…
Reference in New Issue