<commit_before>.. _jinja-extensions:

Extensions
==========

Jinja2 supports extensions that can add extra filters, tests, globals or even
extend the parser.  The main motivation of extensions is to move often used
code into a reusable class like adding support for internationalization.


Adding Extensions
-----------------

Extensions are added to the Jinja2 environment at creation time.  Once the
environment is created additional extensions cannot be added.  To add an
extension pass a list of extension classes or import paths to the
`extensions` parameter of the :class:`Environment` constructor.  The following
example creates a Jinja2 environment with the i18n extension loaded::

    jinja_env = Environment(extensions=['jinja2.ext.i18n'])


.. _i18n-extension:

i18n Extension
--------------

**Import name:** `jinja2.ext.i18n`

Jinja2 currently comes with one extension, the i18n extension.  It can be
used in combination with `gettext`_ or `babel`_.  If the i18n extension is
enabled Jinja2 provides a `trans` statement that marks the wrapped string as
translatable and calls `gettext`.

After enabling, dummy `_` function that forwards calls to `gettext` is added
to the environment globals.  An internationalized application then has to
provide a `gettext` function and optionally an `ngettext` function into the
namespace, either globally or for each rendering.

Environment Methods
~~~~~~~~~~~~~~~~~~~<commit_msg>trim obsolete sentence re: i18n extension<commit_after>.. _jinja-extensions:

Extensions
==========

Jinja2 supports extensions that can add extra filters, tests, globals or even
extend the parser.  The main motivation of extensions is to move often used
code into a reusable class like adding support for internationalization.


Adding Extensions
-----------------

Extensions are added to the Jinja2 environment at creation time.  Once the
environment is created additional extensions cannot be added.  To add an
extension pass a list of extension classes or import paths to the
`extensions` parameter of the :class:`Environment` constructor.  The following
example creates a Jinja2 environment with the i18n extension loaded::

    jinja_env = Environment(extensions=['jinja2.ext.i18n'])


.. _i18n-extension:

i18n Extension
--------------

**Import name:** `jinja2.ext.i18n`

The i18n extension can be used in combination with `gettext`_ or `babel`_.  If 
the i18n extension is enabled Jinja2 provides a `trans` statement that marks 
the wrapped string as translatable and calls `gettext`.

After enabling, dummy `_` function that forwards calls to `gettext` is added
to the environment globals.  An internationalized application then has to
provide a `gettext` function and optionally an `ngettext` function into the
namespace, either globally or for each rendering.

Environment Methods
~~~~~~~~~~~~~~~~~~~
{'fraction_non_alphanumeric': 0.09036568213783404, 'fraction_numerical': 0.015119549929676512, 'mean_word_length': 4.419047619047619, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7418357', 'n_tokens_mistral': 758, 'n_tokens_neox': 702, 'n_words': 364}