mirror of
https://github.com/django/django.git
synced 2025-10-23 21:59:11 +00:00
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).
This commit is contained in:
committed by
Claude Paroz
parent
cf2cd4053f
commit
9aaeec337e
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user