Opened 14 years ago
Last modified 11 years ago
#14174 new New feature
Support for string interpolation in lazy translation
Reported by: | Piotr Czachur | Owned by: | nobody |
---|---|---|---|
Component: | Internationalization | Version: | 1.2 |
Severity: | Normal | Keywords: | |
Cc: | bronger@…, Bouke Haarsma | Triage Stage: | Accepted |
Has patch: | no | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
ugettext_lazy('Hello %s') % 'Sid'
is immediately evaluated to unicode:
def __mod__(self, rhs): if self._delegate_str: return str(self) % rhs elif self._delegate_unicode: return unicode(self) % rhs else: raise AssertionError('__mod__ not supported for non-string types')
My proposition is that mod() could return lazy again, and store params given after %, so they can be used in final conversion to unicode:
unicode(self) % params
Change History (11)
comment:1 by , 14 years ago
comment:2 by , 14 years ago
Hi!
Let users decide.
If they want to pass mutable which can later change, and they want translation to be always up-to-date with this value, they just pass mutable param:
>>> lst = [1,2,3] >>> s = ugettext_lazy('Hello %s') % (lst,) >>> unicode(s) 'Hello [1,2,3]' >>> lst.append(4) >>> unicode(s) 'Hello [1,2,3,4]'
... or if they want to see value with its state during creation of lazy:
>>> lst = [1,2,3] >>> s = ugettext_lazy('Hello %s') % (lst[:],) >>> unicode(s) 'Hello [1,2,3]' >>> lst.append(4) >>> unicode(s) 'Hello [1,2,3]'
I'm not sure about backwards compatibility though... please decide.
comment:3 by , 14 years ago
Component: | Translations → Internationalization |
---|---|
Triage Stage: | Unreviewed → Design decision needed |
comment:4 by , 14 years ago
Cc: | added |
---|
comment:5 by , 14 years ago
Severity: | → Normal |
---|---|
Type: | → New feature |
comment:8 by , 12 years ago
Triage Stage: | Design decision needed → Accepted |
---|
comment:9 by , 12 years ago
This is related to #19160 which was recently fixed.
Specifically the patch for that ticket allowed using string interpolation on lazily translated objects.
comment:10 by , 11 years ago
Not that besides having a lazy __mod__
, we'd also want a lazy __add__
. So we could write s = ugettext_lazy('Hello,') + ' ' + ugettext_lazy('World')
. (Note that the example is imperfect; as those two strings should be a single msgid
, but you get the idea.)
I've played around with this ticket for a while and came up with this: https://gist.github.com/Bouke/7303909. Although it roughly appears to work; I don't like the implementation. I'd rather use lazy()
and not re-implement __add__
and __mod__
. Somehow only the methods that return a string after some operation should be overriden.
comment:11 by , 11 years ago
Cc: | added |
---|
You need to extend on your proposal a bit. What should happen in this case:
Would you want to deepcopy all arguments to %?
I'm not convinced this ticket is worth the trouble though.