From 9aaeec337e217109208672d8fe47eeb49ca492b5 Mon Sep 17 00:00:00 2001 From: Mattias Loverot Date: Wed, 24 Aug 2016 18:18:17 +0200 Subject: Fixed #26866 -- Added format_lazy function Added format_lazy function to django.utils.text module. Useful when dealing with relative complex lazy string concatenations (e.g. in urls.py when translating urls in regular expressions). --- tests/utils_tests/test_text.py | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) (limited to 'tests/utils_tests') diff --git a/tests/utils_tests/test_text.py b/tests/utils_tests/test_text.py index df4c055503..1ce993bdb2 100644 --- a/tests/utils_tests/test_text.py +++ b/tests/utils_tests/test_text.py @@ -6,7 +6,8 @@ import json from django.test import SimpleTestCase from django.utils import six, text from django.utils.functional import lazystr -from django.utils.translation import override +from django.utils.text import format_lazy +from django.utils.translation import override, ugettext_lazy IS_WIDE_BUILD = (len('\U0001F4A9') == 1) @@ -225,3 +226,24 @@ class TestUtilsText(SimpleTestCase): out = text.compress_sequence(seq) compressed_length = len(b''.join(out)) self.assertTrue(compressed_length < actual_length) + + def test_format_lazy(self): + self.assertEqual('django/test', format_lazy('{}/{}', 'django', lazystr('test'))) + self.assertEqual('django/test', format_lazy('{0}/{1}', *('django', 'test'))) + self.assertEqual('django/test', format_lazy('{a}/{b}', **{'a': 'django', 'b': 'test'})) + self.assertEqual('django/test', format_lazy('{a[0]}/{a[1]}', a=('django', 'test'))) + + t = {} + s = format_lazy('{0[a]}-{p[a]}', t, p=t) + t['a'] = lazystr('django') + self.assertEqual('django-django', s) + t['a'] = 'update' + self.assertEqual('update-update', s) + + # The format string can be lazy. (string comes from contrib.admin) + s = format_lazy( + ugettext_lazy("Added {name} \"{object}\"."), + name='article', object='My first try', + ) + with override('fr'): + self.assertEqual('article «\xa0My first try\xa0» ajouté.', s) -- cgit v1.3