Ticket #16516: 16516.diff

File 16516.diff, 3.8 KB (added by Claude Paroz, 13 years ago)

Fix KeyError, with tests

  • django/templatetags/i18n.py

    diff --git a/django/templatetags/i18n.py b/django/templatetags/i18n.py
    index c7202e2..ebb4d02 100644
    a b class BlockTranslateNode(Node):  
    122122        result = re.sub(u'%(?!\()', u'%%', result)
    123123        data = dict([(v, _render_value_in_context(context.get(v, ''), context)) for v in vars])
    124124        context.pop()
    125         return result % data
     125        try:
     126            result = result % data
     127        except KeyError:
     128            with translation.override('en'):
     129                result = self.render(context)
     130        return result
    126131
    127132
    128133class LanguageNode(Node):
  • new file tests/regressiontests/i18n/other/locale/fr/LC_MESSAGES/django.mo

    diff --git a/tests/regressiontests/i18n/other/locale/fr/LC_MESSAGES/django.mo b/tests/regressiontests/i18n/other/locale/fr/LC_MESSAGES/django.mo
    new file mode 100644
    index 0000000000000000000000000000000000000000..f0a21797676abbe377b4637c57fe40fce594a5b2
    GIT binary patch
    literal 454
    zcmYL_%}T>S5XYnFB}Wf};9)!nEjl%u3N71K#M(l^U@g)6+HBJ;$!^%)h<y;>!e{bX
    zoK`Ty{+JK`Gds-B$<bFpc1WBP*TgX~Bz86@PKg&{d=La{@@K?1#q{=l<Qhx1phYP`
    zIk;?9(mJEtt`p3y*~q2GS%z;^R7P{iHbUnHyma1$3t1WKF>$%d@S)BfriMc}e@=01
    zrSQ~&kHSk1al|5wVq~|#dc51cqC6rYF64*WO>T4yBZe`9nDK58qb}*5g!7nMp<N-o
    zu^h&Qs541*o!26lm`YJ{SXmgZ?0um+#^!fnGD{}S;;a*eW23#)G-_MXPF{Zco2n2>
    z4`8_#)=58lO{dszbh|HC(&9ufO{R3tq5rOYI4^1|3Y;2SIu5$pr*0Gv;4d7tG+c~;
    UwZkAW8nh{a?)86LEqB-b1z6B~F#rGn
  • new file tests/regressiontests/i18n/other/locale/fr/LC_MESSAGES/django.po

    literal 0
    HcmV?d00001
    
    diff --git a/tests/regressiontests/i18n/other/locale/fr/LC_MESSAGES/django.po b/tests/regressiontests/i18n/other/locale/fr/LC_MESSAGES/django.po
    new file mode 100644
    index 0000000..8e9b9e1
    - +  
     1# SOME DESCRIPTIVE TITLE.
     2# Copyright (C) YEAR THE PACKAGE'S COPYRIGHT HOLDER
     3# This file is distributed under the same license as the PACKAGE package.
     4# FIRST AUTHOR <EMAIL@ADDRESS>, YEAR.
     5#
     6msgid ""
     7msgstr ""
     8"Project-Id-Version: django tests\n"
     9"Report-Msgid-Bugs-To: \n"
     10"POT-Creation-Date: 2010-02-14 17:33+0100\n"
     11"PO-Revision-Date: 2011-01-21 21:37-0300\n"
     12"Last-Translator: Claude\n"
     13"Language-Team: fr <fr@li.org>\n"
     14"MIME-Version: 1.0\n"
     15"Content-Type: text/plain; charset=UTF-8\n"
     16"Content-Transfer-Encoding: 8bit\n"
     17"Plural-Forms: nplurals=2; plural=(n != 1)\n"
     18
     19#: template.html:3
     20msgid "My name is %(person)s."
     21msgstr "Mon nom est %(personne)s."
  • tests/regressiontests/i18n/tests.py

    diff --git a/tests/regressiontests/i18n/tests.py b/tests/regressiontests/i18n/tests.py
    index 0b955ad..e58c89c 100644
    a b from threading import local  
    99from django.conf import settings
    1010from django.template import Template, Context
    1111from django.test import TestCase, RequestFactory
     12from django.test.utils import override_settings
    1213from django.utils.formats import (get_format, date_format, time_format,
    1314    localize, localize_input, iter_format_modules, get_format_modules)
    1415from django.utils.importlib import import_module
    class TranslationTests(TestCase):  
    129130        self.assertEqual(to_language('en_US'), 'en-us')
    130131        self.assertEqual(to_language('sr_Lat'), 'sr-lat')
    131132
     133    @override_settings(
     134        LOCALE_PATHS = (
     135            os.path.join(os.path.dirname(os.path.abspath(__file__)), 'other', 'locale'),
     136        ),
     137    )
     138    def test_bad_placeholder(self):
     139        """
     140        Error in translation file should not crash template rendering
     141        (%(person)s is translated as %(personne)s in fr.po)
     142        """
     143        from django.template import Template, Context
     144        with translation.override('fr'):
     145            t = Template('{% load i18n %}{% blocktrans %}My name is {{ person }}.{% endblocktrans %}')
     146            c = Context({'person': u"James"})
     147            rendered = t.render(c)
     148            self.assertEqual(rendered, u"My name is James.")
     149
    132150
    133151class FormattingTests(TestCase):
    134152
Back to Top