1Integration 2=========== 3 4.. _babel-integration: 5 6Babel 7----- 8 9Jinja provides support for extracting gettext messages from templates 10via a `Babel`_ extractor entry point called 11``jinja2.ext.babel_extract``. The support is implemented as part of the 12:ref:`i18n-extension` extension. 13 14Gettext messages are extracted from both ``trans`` tags and code 15expressions. 16 17To extract gettext messages from templates, the project needs a Jinja 18section in its Babel extraction method `mapping file`_: 19 20.. sourcecode:: ini 21 22 [jinja2: **/templates/**.html] 23 encoding = utf-8 24 25The syntax related options of the :class:`Environment` are also 26available as configuration values in the mapping file. For example, to 27tell the extractor that templates use ``%`` as 28``line_statement_prefix`` you can use this code: 29 30.. sourcecode:: ini 31 32 [jinja2: **/templates/**.html] 33 encoding = utf-8 34 line_statement_prefix = % 35 36:ref:`jinja-extensions` may also be defined by passing a comma separated 37list of import paths as the ``extensions`` value. The i18n extension is 38added automatically. 39 40Template syntax errors are ignored by default. The assumption is that 41tests will catch syntax errors in templates. If you don't want to ignore 42errors, add ``silent = false`` to the settings. 43 44.. _Babel: https://babel.readthedocs.io/ 45.. _mapping file: https://babel.readthedocs.io/en/latest/messages.html#extraction-method-mapping-and-configuration 46 47 48Pylons 49------ 50 51It's easy to integrate Jinja into a `Pylons`_ application. 52 53The template engine is configured in ``config/environment.py``. The 54configuration for Jinja looks something like this: 55 56.. code-block:: python 57 58 from jinja2 import Environment, PackageLoader 59 config['pylons.app_globals'].jinja_env = Environment( 60 loader=PackageLoader('yourapplication', 'templates') 61 ) 62 63After that you can render Jinja templates by using the ``render_jinja`` 64function from the ``pylons.templating`` module. 65 66Additionally it's a good idea to set the Pylons ``c`` object to strict 67mode. By default attribute access on missing attributes on the ``c`` 68object returns an empty string and not an undefined object. To change 69this add this to ``config/environment.py``: 70 71.. code-block:: python 72 73 config['pylons.strict_c'] = True 74 75.. _Pylons: https://pylonshq.com/ 76