T.lazy 在 web2py 中做了什么?
我正在学习web2py。我阅读了示例开源代码。在一个应用程序 (storpy) 中,程序员在模型文件 db.py
中重复使用 T.lazy
,如下所示:
...
Field('comment', 'text'),
Field('cover', 'upload', autodelete=True))
T.lazy = False
db.dvds.title.requires = [IS_NOT_EMPTY(error_message=T('Missing data') + '!'), IS_NOT_IN_DB(db, 'dvds.title', error_message=T('Already in the database') + '!')]
...
T.lazy = True
Why does the Programmer set T.lazy
首先到 False
然后再到 True
?
I am studying web2py. I read example open source code. In one application (storpy), the programmer uses T.lazy
repeatedly inside the models file db.py
such as this:
...
Field('comment', 'text'),
Field('cover', 'upload', autodelete=True))
T.lazy = False
db.dvds.title.requires = [IS_NOT_EMPTY(error_message=T('Missing data') + '!'), IS_NOT_IN_DB(db, 'dvds.title', error_message=T('Already in the database') + '!')]
...
T.lazy = True
Why does the programmer set T.lazy
first to False
then to True
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
默认情况下,
T()
是惰性的——当您调用它时,它实际上并不执行转换,而是返回一个lazyT 对象,该对象在视图中序列化之前不会被转换。如果设置T.lazy=False
,将强制立即翻译,因此调用T('some string')
将返回实际翻译后的字符串,而不是lazyT 对象。请注意,从即将发布的版本开始,您将能够执行以下操作,而不必将
T.lazy
切换为False
和True
>T('some string',lazy=False) 强制立即翻译单个调用。强制立即翻译的其他方法是str(T('some string'))
或T('some string').xml()
--str( )
序列化lazyT 对象(.xml()
只是调用str()
)。By default,
T()
is lazy -- when you call it, it doesn't actually do the translation but instead returns a lazyT object, which isn't translated until serialized in a view. If you setT.lazy=False
, that will force immediate translation, so callingT('some string')
will return the actual translated string instead of a lazyT object.Note, starting with the upcoming release, instead of having to toggle
T.lazy
toFalse
andTrue
, you will be able to doT('some string', lazy=False)
to force an immediate translation for a single call. Other ways to force immediate translation arestr(T('some string'))
orT('some string').xml()
--str()
serializes the lazyT object (and.xml()
simply callsstr()
).