In my Django v1.6.5 project running on Python v2.7.x, I have a model that returns its configuration as a string. I need the returned string as a gettext_lazy object, so I can evaluate it in any language that is required later.
from __future__ import unicode_literals
from django.utils.translation import ugettext_lazy as _, string_concat
...
class MyModel(models.Model):
key = models.CharField(...)
value = models.CharField(...)
@property
def config_str(self):
return _('some configuration')
In these scenarios, this works great:
- Static string: (see above) - it works!
- String concatenation:
return string_concat(self.key, _(' equals '), self.value)- works!
What doesn't work, uses gettext_lazy with placeholders, a la:
return _('"%(key)s" equals "%(value)s"' % {key: self.key, value: self.value})
or using the .format () mechanism:
return _('"{key}" equals "{value}"').format(key=self.key, value=self.value)
When I do this, my .po file contains:
msgid ""%(key)s" equals "%(value)s"" or
msgid "«{key}» equals «{value}»"
but even when I fill this Eg .:
msgstr "«%(key)s» est égal à «%(value)s»" or
msgstr "«{key}» est égal à «{value}»"
, , , . , , EN . , "foo" "bar". , EN, FR (). , . , , , gettext "foo" "bar" () , - .
, () :
return _('"{key}" equals "{value}"'.format(key=self.key, value=self.value))
, , . =/
string_concat(), , .
, placeholders gettext_lazy.
. django: , a), , b) gettext, gettext_lazy.