ugettext and ugettext_lazy(ugettext 和 ugettext_lazy)
问题描述
您能解释一下 ugettext 和 ugettext_lazy 之间的主要区别吗?
Could you explain what principal difference between ugettext and ugettext_lazy?
当我尝试时
return HttpResponse(ugettext_lazy("Hello"))
我什么也没看到,但是
return HttpResponse(ugettext("Hello"))
正在工作.
为什么?
推荐答案
ugettext 用于加载字符串的翻译现在.ugettext_lazy 返回一个最终可以变成字符串的对象.如果在设置正确的语言环境之前评估了 ugettext_lazy 调用,则需要这样做.
ugettext is used to load a translation of a string right now. ugettext_lazy returns an object that can eventually be turned into a string. You need that if the ugettext_lazy call is evaluated before the proper locale has been set.
ugettext_lazy 可用于使用 Unicode 对象的地方.仔细检查您的 HTML 输出,它可能如下所示:
ugettext_lazy can be used where you use a Unicode object. Double-check your HTML output, it might look like this:
<django.utils.functional...>
并且浏览器将其全部忽略为无法识别的标签.
and the browser is ignoring it all as an unrecognized tag.
在这种情况下,您不需要惰性翻译,因为您会立即使用该字符串.如果您出于某种原因确实想继续使用 ugettext_lazy,请尝试以下操作:
You don't need a lazy translation in this case, because you are immediately using the string. If you really want to continue with ugettext_lazy for some reason, try this:
return HttpResponse(ugettext_lazy("Hello").encode('utf-8'))
有关详细信息,请参阅文档.
See the docs for more information.
这篇关于ugettext 和 ugettext_lazy的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:ugettext 和 ugettext_lazy
基础教程推荐
- Discord.py 缺少必需的参数 2022-01-01
- 将 x 轴刻度更改为自定义字符串 2022-01-01
- pyserial - 可以从线程 a 写入串行端口,是否阻塞从线程 b 读取? 2022-01-01
- 用 Python 编写 Fortran 无格式文件 2022-01-01
- 由Python将MP3转换为MIDI(类型错误:无法加载插件:mtg-Melodia:Melodia) 2022-01-01
- 与常规 dict 相比,Python manager.dict() 非常慢 2022-01-01
- 在 Celery 工作人员中捕获 Heroku SIGTERM 以优雅地关 2022-01-01
- 尝试制作WhatsApp机器人 2022-01-01
- 使用生成器和迭代器时 Python 多循环失败 2022-01-01
- numpy float:比算术运算中内置的慢 10 倍? 2022-01-01
