Lazy evaluation and the gettext/ngettext functions
Lazy evaluation is an evaluation strategy that delays the evaluation of an expression until its value is needed, that is, it is a call-by-need mechanism. In our application, there can be several instances of texts that are evaluated later while rendering the template. It usually happens when we have texts that are marked as translatable outside the request context, so we defer the evaluation of these until they are actually needed.
Getting ready
Let's start with the application from the previous recipe. Now, we want the labels in the product- and category-creation forms to show the translated values.
How to do it…
To mark all the field labels in the product and category forms as translatable, we will make the following changes to models.py
:
from flask.ext.babel import _ class NameForm(Form): name = TextField(_('Name'), validators=[InputRequired()]) class ProductForm(NameForm): price = DecimalField(_('Price...