In Django, I'm happily using ugettext_lazy
to pospone the translation of a string only when its representation is needed.
The problem is that when I concatenate a lazy string to a normal string or when I use its methods (e.g. capitalize() ), the string is evaluated and I loose lazy translation.
E.g.
label = ugettext_lazy('my label') #This is lazy
label_concat = label + ' some other string' #'label_concat' contains transalted 'label'
label_cap = label.capitalize() #'label_cap' contains transalted 'label'
#Set language
...
print label #Translated
print label_cap #Not translated
I know that this is the normal behaviour of Django but I wonder if someone has resolved this issue.
gettext_lazy is a callable within the django. utils. translation module of the Django project.
Use the function django. utils. translation. gettext_noop() to mark a string as a translation string without translating it.
Lazy Translation utils. translation. ugettext_lazy() to translate strings lazily – when the value is accessed rather than when the ugettext_lazy() function is called. In this example, ugettext_lazy() stores a lazy reference to the string – not the actual translation.
For concatenating, you can use string_concat
(up to 1.10)/format_lazy
(from 1.11) which creates a lazy object
If you wish to implement lazy capitalize
, use django.utils.functional.lazy
decorator. See string_concat
implementation.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With