Implementing 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-when-needed mechanism. In our application, there can be several instances of texts that are evaluated later while rendering the template. This 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…
Follow these steps in order to implement the lazy evaluation of translations:
- To mark all the field labels in the product and category forms as translatable, make the following changes to
my_app/catalog/models.py
:from flask_babel import _
class NameForm(FlaskForm):
...