Skip to content

Commit

Permalink
Merge pull request #165 from paperlessreceipts/i18n_extension-setting
Browse files Browse the repository at this point in the history
Add jinja2.i18n_extension configuration setting
  • Loading branch information
mmerickel authored Mar 27, 2022
2 parents 246691a + fe4fb6d commit 6c37556
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 6 deletions.
5 changes: 5 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
unreleased
==========

- Add ``jinja2.i18n_extension`` configuration setting.

2.9.2 (2022-03-19)
==================

Expand Down
19 changes: 15 additions & 4 deletions docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -325,8 +325,9 @@ Now ``foo.email`` will be rendered using the ``mail.jinja2.*`` settings.
Internalization (i18n)
----------------------

When :term:`pyramid_jinja2` is included in a Pyramid application,
:ref:`jinja2.ext.i18n <jinja2:i18n-extension>` is automatically activated.
When :term:`pyramid_jinja2` is included in a Pyramid application, either
:ref:`jinja2.ext.i18n <jinja2:i18n-extension>` or the extension configured by
``jinja2.i18n_extension`` is automatically activated.

Be sure to configure ``jinja2.i18n.domain`` according to ``setup.cfg`` domain
settings. By default, ``jinja2.i18n.domain`` is set to the name of the
Expand Down Expand Up @@ -470,8 +471,18 @@ jinja2.extensions
-----------------

A list of extension objects, or a newline-delimited set of dotted import
locations, where each line represents an extension. :ref:`jinja2.ext.i18n
<jinja2:i18n-extension>` is automatically activated.
locations, where each line represents an extension. Either :ref:`jinja2.ext.i18n
<jinja2:i18n-extension>` or the i18n extension configured using
``jinja2.i18n_extension`` is automatically activated.


.. _setting_jinja2_i18n_extension:

jinja2.i18n_extension
---------------------

The name of the i18n extension to activate. Defaults to
:ref:`jinja2.ext.i18n <jinja2:i18n-extension>`.


.. _setting_jinja2_i18n_domain:
Expand Down
5 changes: 3 additions & 2 deletions src/pyramid_jinja2/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,9 @@ def sget(name, default=None):

# get jinja2 extensions
extensions = parse_multiline(sget("extensions", ""))
if "jinja2.ext.i18n" not in extensions:
extensions.append("jinja2.ext.i18n")
i18n_extension = sget("i18n_extension", "jinja2.ext.i18n")
if i18n_extension not in extensions:
extensions.append(i18n_extension)
opts["extensions"] = extensions

# get jinja2 bytecode caching settings and set up bytecaching
Expand Down
11 changes: 11 additions & 0 deletions tests/test_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,17 @@ def test_default_undefined(self):
opts = self._callFUT(settings, "j2.")
self.assertEqual(opts["undefined"], Undefined)

def test_default_extensions(self):
opts = self._callFUT({}, "j2.")
self.assertEqual(opts["extensions"], ["jinja2.ext.i18n"])

def test_override_i18n_extension(self):
settings = {
"j2.i18n_extension": "test.TestI18NExtension",
}
opts = self._callFUT(settings, "j2.")
self.assertEqual(opts["extensions"], ["test.TestI18NExtension"])


# This is just a fake top level name that we can pass into maybe_dotted that
# will resolve.
Expand Down

0 comments on commit 6c37556

Please sign in to comment.