| IRIS YANG | 3121357 | 2020-08-18 13:17:02 +0000 | [diff] [blame] | 1 | Integration |
| 2 | =========== |
| 3 | |
| 4 | .. _babel-integration: |
| 5 | |
| 6 | Babel |
| 7 | ----- |
| 8 | |
| 9 | Jinja provides support for extracting gettext messages from templates |
| 10 | via 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 | |
| 14 | Gettext messages are extracted from both ``trans`` tags and code |
| 15 | expressions. |
| 16 | |
| 17 | To extract gettext messages from templates, the project needs a Jinja |
| 18 | section in its Babel extraction method `mapping file`_: |
| 19 | |
| 20 | .. sourcecode:: ini |
| 21 | |
| 22 | [jinja2: **/templates/**.html] |
| 23 | encoding = utf-8 |
| 24 | |
| 25 | The syntax related options of the :class:`Environment` are also |
| 26 | available as configuration values in the mapping file. For example, to |
| 27 | tell 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 |
| 37 | list of import paths as the ``extensions`` value. The i18n extension is |
| 38 | added automatically. |
| 39 | |
| 40 | Template syntax errors are ignored by default. The assumption is that |
| 41 | tests will catch syntax errors in templates. If you don't want to ignore |
| 42 | errors, 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 | |
| 48 | Pylons |
| 49 | ------ |
| 50 | |
| 51 | It's easy to integrate Jinja into a `Pylons`_ application. |
| 52 | |
| 53 | The template engine is configured in ``config/environment.py``. The |
| 54 | configuration 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 | |
| 63 | After that you can render Jinja templates by using the ``render_jinja`` |
| 64 | function from the ``pylons.templating`` module. |
| 65 | |
| 66 | Additionally it's a good idea to set the Pylons ``c`` object to strict |
| 67 | mode. By default attribute access on missing attributes on the ``c`` |
| 68 | object returns an empty string and not an undefined object. To change |
| 69 | this add this to ``config/environment.py``: |
| 70 | |
| 71 | .. code-block:: python |
| 72 | |
| 73 | config['pylons.strict_c'] = True |
| 74 | |
| 75 | .. _Pylons: https://pylonshq.com/ |