The following code is not working
from django.utils.translation import gettext_lazy as _
stringtest=_("First string")
stringtest= stringtest + _(" Second string")
print stringtest
I get the following exception:
cannot concatenate 'str' and '__proxy__' objects
Is it really impossible to append a "translated" string to himself ?
You cannot concatenate your two string but creating a new one (which is already the case with your +
operation anyway, since string are immutable):
from django.utils.translation import gettext_lazy as _
stringtest = _("First string")
stringtest = "%s %s" % (stringtest, _(" Second string"))
print stringtest
The problem is gettext_lazy
returns a proxy object, since it is usually used to translate string in class definition (in models attribute for exemple) and is not designed to be used in view code, like you are doing right now. The proxy object has a method to translate it to a str
object BUT it is not a string.
If you don't really need this _lazy
specificity, you can just use gettext
in your views, which returns plain string:
>>> from django.utils.translation import gettext as _
>>> _("str1 ") + _("str2")
'str1 str2'