Commit 16bb9c59 authored by Jannis Leidel's avatar Jannis Leidel
Browse files

Fixed #16516 -- Relaxed the blocktrans rendering a little by falling back to...

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.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@16723 bcc190cf-cafb-0310-a4f2-bffc1f526a37
parent 2b4341d5
Loading
Loading
Loading
Loading
+6 −1
Original line number Diff line number Diff line
@@ -128,7 +128,12 @@ class BlockTranslateNode(Node):
        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):
+4 −1
Original line number Diff line number Diff line
@@ -115,7 +115,10 @@ class override(object):
        self.old_language = get_language()

    def __enter__(self):
        if self.language is not None:
            activate(self.language)
        else:
            deactivate_all()

    def __exit__(self, exc_type, exc_value, traceback):
        if self.deactivate:
+6 −3
Original line number Diff line number Diff line
@@ -532,15 +532,18 @@ For a complete discussion on the usage of the following see the
    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()

+7 −0
Original line number Diff line number Diff line
@@ -512,6 +512,13 @@ You can use multiple expressions inside a single ``blocktrans`` tag::
.. 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
+454 B

File added.

No diff preview for this file type.

Loading