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?
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.lazytoFalseandTrue, 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()).