diff options
author | Christopher Allan Webber <cwebber@dustycloud.org> | 2011-06-04 16:44:22 -0500 |
---|---|---|
committer | Christopher Allan Webber <cwebber@dustycloud.org> | 2011-06-04 16:44:22 -0500 |
commit | f99f61c65c7937910d7170f7f4a43c18e083a010 (patch) | |
tree | c346c964d5b73fe98a503170e0e90e2c70d11ab4 /mediagoblin/util.py | |
parent | e9279f21376feb5c43675c31b6f25e9fabac2ac6 (diff) | |
download | mediagoblin-f99f61c65c7937910d7170f7f4a43c18e083a010.tar.lz mediagoblin-f99f61c65c7937910d7170f7f4a43c18e083a010.tar.xz mediagoblin-f99f61c65c7937910d7170f7f4a43c18e083a010.zip |
Cache template environments and gettexts so we don't have to reproduce
them on every request.
Diffstat (limited to 'mediagoblin/util.py')
-rw-r--r-- | mediagoblin/util.py | 23 |
1 files changed, 21 insertions, 2 deletions
diff --git a/mediagoblin/util.py b/mediagoblin/util.py index 41f8a92a..3649b6c3 100644 --- a/mediagoblin/util.py +++ b/mediagoblin/util.py @@ -25,6 +25,7 @@ import urllib from math import ceil import copy +from babel.localedata import exists import jinja2 import translitcodec from paste.deploy.loadwsgi import NicerConfigParser @@ -58,6 +59,9 @@ def get_jinja_loader(user_template_path=None): return jinja2.PackageLoader('mediagoblin', 'templates') +SETUP_JINJA_ENVS = {} + + def get_jinja_env(template_loader, locale): """ Set up the Jinja environment, @@ -67,6 +71,11 @@ def get_jinja_env(template_loader, locale): """ setup_gettext(locale) + # If we have a jinja environment set up with this locale, just + # return that one. + if SETUP_JINJA_ENVS.has_key(locale): + return SETUP_JINJA_ENVS[locale] + template_env = jinja2.Environment( loader=template_loader, autoescape=True, extensions=['jinja2.ext.i18n']) @@ -75,6 +84,9 @@ def get_jinja_env(template_loader, locale): mgoblin_globals.translations.gettext, mgoblin_globals.translations.ngettext) + if exists(locale): + SETUP_JINJA_ENVS[locale] = template_env + return template_env @@ -330,6 +342,8 @@ def read_config_file(conf_file): return mgoblin_conf +SETUP_GETTEXTS = {} + def setup_gettext(locale): """ Setup the gettext instance based on this locale @@ -340,8 +354,13 @@ def setup_gettext(locale): # TODO: fallback nicely on translations from pt_PT to pt if not # available, etc. - this_gettext = gettext.translation( - 'mediagoblin', TRANSLATIONS_PATH, [locale], fallback=True) + if SETUP_GETTEXTS.has_key(locale): + this_gettext = SETUP_GETTEXTS[locale] + else: + this_gettext = gettext.translation( + 'mediagoblin', TRANSLATIONS_PATH, [locale], fallback=True) + if exists(locale): + SETUP_GETTEXTS[locale] = this_gettext mgoblin_globals.setup_globals( translations=this_gettext) |