question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

jinja2.exceptions.UndefinedError: 'gettext' is undefined

See original GitHub issue

I’m using Jinja2 2.7.3 inderectly through pelican and I get this error with templates using {% trans% } directive. So be sure it’s not related to pelican, i’ve created this small test file (https://gist.github.com/njouanin/36292368ad15f9b87ba0). When I run it with python interpreter (3.4.1) I get the following exception :

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/nico/Brassage/Beerfactory/env-site/lib/python3.4/site-packages/jinja2/environment.py", line 969, in render
    return self.environment.handle_exception(exc_info, True)
  File "/Users/nico/Brassage/Beerfactory/env-site/lib/python3.4/site-packages/jinja2/environment.py",     line 742, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "/Users/nico/Brassage/Beerfactory/env-site/lib/python3.4/site-packages/jinja2/_compat.py", line 36, in reraise
    raise value.with_traceback(tb)
  File "theme/templates/base.html", line 50, in <module>
    {% block header_left_section %}
  File "theme/templates/base.html", line 60, in <module>
    <h2 id="site-description">{{ gettext('Brasser libre !') }}</h2><!-- #site-description -->
jinja2.exceptions.UndefinedError: 'gettext' is undefined

I’ve googled this issue which appear to be related to gettext library which is missing. Here importing gettext module in python works fine, so I guess it is installed. Here is my environnement : MacOs 10.9 Python 3.4.1 Jinja2 : 2.7.3

Issue Analytics

  • State:closed
  • Created 9 years ago
  • Comments:10 (3 by maintainers)

github_iconTop GitHub Comments

6reactions
mitsuhikocommented, Jan 8, 2018

I’m going to close this issue. This happens if the i18n extension is installed but no gettext function is provided. This is a configuration problem of whatever integrated Jinja into a project.

0reactions
davidismcommented, Dec 26, 2021

You (or the library that’s configuring Jinja for you) need to install the gettext functions after enabling the i18n extension. This is shown in the docs https://jinja.palletsprojects.com/en/3.0.x/extensions/#i18n-extension

translations = get_gettext_translations()
env = Environment(extensions=["jinja2.ext.i18n"])
env.install_gettext_translations(translations, newstyle=True)
Read more comments on GitHub >

github_iconTop Results From Across the Web

Django+Jinja2+i18n - 'gettext' is undefined - Stack Overflow
Here is my solution after many tries. The 'jinja2.ext.i18n' doesn't install gettext automatically, so you need to add it first to the ...
Read more >
Jinja2 i18n problem: UndefinedError: 'gettext' is undefined
Hi there, I tried a very simple test to check the Jinja2 i18n, here is the code (it is pretty much the same...
Read more >
jinja2.exceptions.UndefinedError: 'gettext' is undefined-django
Here is my solution after many tries. The 'jinja2.ext.i18n' doesn't install gettext automatically, so you need to add it first to the Environment...
Read more >
2. i18n Extension — Jinja2 API - GitHub Pages
If the i18n extension is enabled Jinja2 provides a trans statement that marks the wrapped string as translatable and calls gettext . After...
Read more >
jupyterlab/jupyterlab - Gitter
endtrans %}<br> jinja2.exceptions.UndefinedError: 'gettext' is undefined. Steven Silvester. @blink1073. @timkpaine, I'm not familiar how to use the trans ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found