Django translations and gettext: The deprecation of the % (string interpolation) operator

烂漫一生 提交于 2019-12-05 10:02:57

You could use it safely. For example

ugettext_lazy('{foo}').format(foo='bar')

The translation program xgettext, which is used by Django, does not care about the content to be translated. It just searches .py file for keywords such as ugettext_lazy or _ to collect the translatable strings(refs the manual of xgettext and Django code)

Furthermore, the .format() method above is a wrapper provided by the proxy object, like:

>>> ugettext_lazy(u'{foo}').format
<bound method __proxy__.__wrapper__ of <django.utils.functional.__proxy__ object at 0x102f19050>>

The invoking of the above .format() would get u'{foo}' to be translated to some unicode value, then call value.format with actual arguments. You could see that the translation and value.format happen in different stages.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!