pelican-themes
pelican-themes copied to clipboard
pelican-bootstrap3: UndefinedError: 'gettext' is undefined
Hi,
I tried using this pelican theme but it displays
CRITICAL: UndefinedError: 'gettext' is undefined
and fails to produce html files. I tried using a different theme and it works without a problem.
I am using pelican 3.6.3.
Sorry, I wrote my post at a wrong place...
- You did not mention which theme you are talking about.
- Current version of Pelican is 3.7, so I suggest trying that before anything else.
Sorry for the confusion; I thought that there is a separate issue page for each theme. The above error message comes out when I use pelican-bootstrap3.
Just merged several commits related to that theme, so I suggest using git-bisect to determine which Git IDs work and which do not.
Thanks for your help, I tried git-bisect between the first commit and the current commit and git says b6ab2ad418b634eda366026855be0574703281d7 is the first bad commit.
Hmm, I am not sure if I used git-bisect in a correct way; I tried checking a few commits by hand and the latest commit that works is be36234f9f7fb4633d7e2eee89833839d7cbf1eb.
By the way, I cannot use pelican 3.7, somehow render_math
plug-in does not work with pelican 3.7.
I ran into this today as well after swapping over to the pelican-themes repo. I'm running this on the 3.7.0
release (just upgraded today) and am getting the same error. Originally it was UndefinedError: '_' is undefined
, which was an issue because I didn't have python-gettext
installed. After installing that and adding JINJA_EXTENSIONS = ['jinja2.ext.i18n']
it now notes that gettext
is missing instead. Note that with 3.7.0
JINJA_EXTENSIONS
is deprecated, so it moved to to JINJA_ENVIRONMENT
which I imagine is breaking this with how it interacts and what it is expecting maybe? I tried setting JINJA_ENVIRONMENT = {'extensions': 'jinja2.ext.i18n'}
and a few other things but that didn't work at all and threw errors related to the namespace.
I rolled the theme repo back to https://github.com/getpelican/pelican-themes/commit/be36234f9f7fb4633d7e2eee89833839d7cbf1eb#diff-0eb6cb930365747af1fe070650593b8e (the commit prior to the translation one) and it works fine.
@chan-y-park Try installing python-gettext
and see if that fixes the error for you on 3.6.x
.
The translation feature was developed using pelican 3.6.3 and it used to work for me. Sorry for the inconvenience. I just upgraded to pelican 3.7.0 and got the error about JINJA-EXTENSIONS
being outdated. My next step was to use
JINJA_ENVIRONMENT = {'extensions': ['jinja2.ext.i18n']}
.
Then I still got an error KeyError: 'JINJA_EXTENSIONS'
when using the i18n_subsites
plugin. So I disabled this plugin, and now I also get UndefinedError: 'gettext' is undefined
. I have python-getttext
installed, so somehow it is not being found/loaded.
Obviously gettext
needs to be installed into the jinja environment using a call to
jinja2.Environment.install_gettext_callables
as documented here.
However, I do not know how to access the environment that is created with configuration option JINJA_ENVIRONMENT = {'extensions': ['jinja2.ext.i18n']}
.
@jranke No worries it's not like you hatched a devious plot to break it on this release! I tried something similar to what you did (setting the ENVIRONMENT
) variable and it didn't seem to work. I think if we can determine how the mapping works between the JINJA_ENVIRONMENT
setting and the old JINJA_EXTENSIONS
we can probably resolve the error, I'm just not familiar enough with the internals to know what that mapping looks like.
Edit: Looks like you had the same thought I did!
@gravyboat Thanks for the heads-up! This translation stuff seems not to be used so much in pelican themes...
For what it's worth, I just downgraded pelican to 3.6.3 (from Debian jessie-backports), went back to the previous configuration using JINJA_EXTENSIONS
and pelican-bootstrap3
from current master works with this setting. So I wonder why @chan-y-park had this problem with pelican 3.6.3.
It seems to me that this needs to be taken care of in pelican itself, so gettext (and maybe newgettext) can be installed via the pelican config file.
Obviously gettext needs to be installed into the jinja environment using a call to jinja2.Environment.install_gettext_callables as documented here.
However, I do not know how to access the environment that is created with configuration option JINJA_ENVIRONMENT = {'extensions': ['jinja2.ext.i18n']}.
This seems to be the relevant question. Any thoughts, @rjames86?
@justinmayer how was this handled previously? Looks like this isn't a new problem: https://github.com/pallets/jinja/issues/363
In my quick testing in local, adding this to generators.py
fixed the problem, but I don't have enough context on the i18n use cases to know if this covers everything:
# On line 71 in generators.py
import gettext
self.env.install_gettext_translations(gettext)
Thanks for having a look. I thought it would need to be install_gettext_callables
, as mentioned above? But I have not done any tests and have no context either...
Thanks for having a look. I thought it would need to be install_gettext_callables, as mentioned above But I have not done any tests and have no context either...
That also very well could be. Does anyone else in this thread use the i18n extension that could verify using either one fixes everything? Pelican successfully builds my site when using install_gettext_translations
, but I don't know if if solves issues further downstream in the templates.
Just for completeness, I tested this on both 3.6.3 and 3.7 and the error is the same. I'm confident that this is not related to #1999. However, happy to figure out a good fix for this going forward.
Maybe it is just the jinja version that's causing the difference. I can't test right now, but I have a website using i18n, so I hope to be able to test your proposed fix in the next couple of days.
Dear all, thank you very much for your efforts in trying to resolve this issue!
@jranke I doubled-checked with pelican 3.6.3 + current master and I do get the error I mentioned in the first place, then I checked out be36234f9f7fb4633d7e2eee89833839d7cbf1eb and it works fine.
I am running this on a conda environment, so let me post it here just in case there is any dependency I am missing.
List of Python packages
# packages in environment at /home/chan/anaconda2/envs/python2: # _nb_ext_conf 0.3.0 py27_0 anaconda-client 1.5.1 py27_0 backports 1.0 py27_0 backports-abc 0.4
Let me help here:
The setting change with 3.7 (JINJA_ENVIRONMENT
vs JINJA_EXTENSIONS
) will not cause a break as defined here.
The issue is that, this "translation" feature was developed with the assumption that i18n_subsites
plugin will be present, because that does the installation for you. This interdependence isn't mentioned anywhere (I'm guessing, while developing @jranke wasn't aware of it either).
The problem arises when you don't have the plugin. It doesn't matter what version (3.6 or 3.7) you use. You'll get the error. And with 3.7, you can't use the plugin right now since it needs to be updated to the new setting.
Thanks for assisting with this, @avaris. Much appreciated.
@jranke: I wasn't aware of the introduction of a new dependency when merging your PR. Would you please consider preparing a new pull request that makes your I18N features optional? As you can imagine, not everyone needs that functionality, and it should probably be an opt-in feature.
Thanks @justinmayer and @avaris. At least that validates my assumptions. My change would have no affect, but I didn't know how the i18n stuff worked.
@avaris and @justinmayer: I see it differently: Removing the setting JINJA_EXTENSIONS
from the pelican config broke the i18n_subsites plugin.
@justinmayer Sorry, I tried to communicate this in my last comment before the merge https://github.com/getpelican/pelican-themes/pull/441#issuecomment-266811378
@jranke please take a look at #1999. You'll see that there is no fundamental difference between what was being done with JINJA_EXTENSIONS
vs the new JINJA_ENVIRONMENT
. The same kwargs are being passed into the environment as before as you can see here: https://github.com/getpelican/pelican/pull/1999/files#diff-1a5861ddd428e9eae60b911254e077b0
@rjames86 yes, but... settings['JINJA_EXTENSIONS']
is gone, which the i18n_subsites plugin uses:
https://github.com/getpelican/pelican-plugins/blob/master/i18n_subsites/i18n_subsites.py#L273
@jranke so going back to the original suggestion, this doesn't necessitate a change to core Pelican. There needs to be an update to the i18n_subsites plugin to pull extensions from the correct location
generator.settings['JINJA_ENVIRONMENT']['extensions']
See settings.py
@rjames86 agreed - should this be conditional on generator.settings['JINJA_EXTENSIONS']
being present? I am not good at python, could you prepare a PR for the i18n_subsites
plugin?