Opened 8 years ago
Closed 8 years ago
#26866 closed New feature (fixed)
Lazy variant of string format
Reported by: | Mattias Loverot | Owned by: | nobody |
---|---|---|---|
Component: | Internationalization | Version: | dev |
Severity: | Normal | Keywords: | |
Cc: | Claude Paroz | Triage Stage: | Ready for checkin |
Has patch: | yes | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
I'd like to contribute with creating a lazy variant of str format method:
https://docs.python.org/3/library/stdtypes.html#str.format
Very similar to the string_concat function:
https://github.com/django/django/blob/master/django/utils/translation/__init__.py#L221-L227
This allows more advanced string formatting with lazy strings. My particular use case is urls.py where I don't want translators to get a chance to translate regular expressions - only the actual url piece. I ended up with:
Proposal:
from django.utils.translation import pgettext_lazy, string_format t = { 'mypages': pgettext_lazy('Url', 'mypages'), 'objects': pgettext_lazy('Url', 'objects') } urlpatterns = [ url(string_format('^{mypages}/{objects}/$', **t), views.ObjectView.as_view(), name='objects') ]
With string_concat:
url(string_concat('^', t['mypages'], '/', t['objects'], '/$'), views.ObjectView.as_view(), name='objects')
If you accept the proposal I'll make a pull request. Thanks.
Change History (15)
comment:1 by , 8 years ago
comment:2 by , 8 years ago
Yes, I'd say my proposed solution also covers the use case in #14174. The str.format method was very new when #14174 was created and the latest Django at that time certainly supported Python versions which did not contain str.format. Since Django nowadays requires 2.7+ I think it makes sense to implement a lazy version of string_format.
comment:5 by , 8 years ago
Forked and created a ticket_26866 branch:
https://github.com/lovmat/django/tree/ticket_26866
What do you think?
comment:7 by , 8 years ago
Some questions:
Should we name this function format_lazy
instead?
Is django.utils.translation
a better home than django.utils.text
(admitting the same question could arise for string_concat
)?
comment:8 by , 8 years ago
I agree django.utils.text is a better place. New attempt here:
https://github.com/lovmat/django/tree/ticket_26866_format_lazy
I also encountered an issue with my first implementation of format_lazy - it could not handle dictionaries properly since they were forced to text immediately. E.g.:
format_lazy('{0[a]}', {'a': 'django'})
Left todo: update docs.
What do you think?
comment:9 by , 8 years ago
Please make it a pull request so as we can comment on the patch.
Also please make the string_concat
move/renaming to another commit, even a separate ticket/patch in my opinion.
comment:11 by , 8 years ago
Has patch: | set |
---|---|
Patch needs improvement: | set |
comment:12 by , 8 years ago
Patch needs improvement: | unset |
---|
comment:13 by , 8 years ago
Patch needs improvement: | set |
---|
comment:14 by , 8 years ago
Patch needs improvement: | unset |
---|---|
Triage Stage: | Accepted → Ready for checkin |
Is it similar to #14174?