I guess, we found a bug, not sure if it has been reported earlier and
whether this is the right place to report.

When a template contains translations in variables, like
{{ _("text") }}, the text seems to be translated already during the
creation of the Template object. Every following call of
Template.render() will output the translation which was made earlier.
(Possibly in the wrong language.)

Templates are cached in 'django.template.loaders.cached.Loader', so
this bug makes constructions like  {{ _("text")|upper }} impossible.


=============Test code: =============

from django.utils import translation
from django.template import Template, Context


def language(language_code):
    class with_block(object):
        def __enter__(self):
            self._old_language = translation.get_language()
            translation.activate(language_code)
        def __exit__(self, *args):
            translation.activate(self._old_language)
    return
with_block()


with language('en'): # Output 'Yes'
     Template("{{ _('Yes') }}").render(Context({}))


with language('fr'): # Output 'Oui'
     Template("{{ _('Yes') }}").render(Context({}))


with language('nl'): # Output 'Ja'
     Template("{{ _('Yes') }}").render(Context({}))


with language('nl'): # create template, while environment language is
dutch
    t = Template("{{ _('Yes') }}")

with language('fr'): # Render in French -> output is still Dutch
('Ja')
    t.render(Context({}))


============= Output =============

Python 2.6.5 (r265:79063, Apr 16 2010, 13:09:56)
[GCC 4.4.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> from django.utils import translation
>>> from django.template import Template, Context
>>>
>>>
>>> def language(language_code):
...     class with_block(object):
...         def __enter__(self):
...             self._old_language = translation.get_language()
...             translation.activate(language_code)
...         def __exit__(self, *args):
...             translation.activate(self._old_language)
...     return
with_block()
...
>>>
>>> with language('en'): # Output 'Yes'
...      Template("{{ _('Yes') }}").render(Context({}))
...
u'Yes'
>>>
>>> with language('fr'): # Output 'Oui'
...      Template("{{ _('Yes') }}").render(Context({}))
...
u'Oui'
>>>
>>> with language('nl'): # Output 'Ja'
...      Template("{{ _('Yes') }}").render(Context({}))
...
u'Ja'
>>>
>>> with language('nl'): # create template, while environment language is dutch
...     t = Template("{{ _('Yes') }}")
...
>>> with language('fr'): # Render in French -> output is still Dutch ('Ja')
...     t.render(Context({}))
...
u'Ja'

-- 
You received this message because you are subscribed to the Google Groups 
"Django developers" 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-developers?hl=en.

Reply via email to