Cache template environments and gettexts so we don't have to reproduce
them on every request.
This commit is contained in:
parent
e9279f2137
commit
f99f61c65c
@ -25,6 +25,7 @@ import urllib
|
|||||||
from math import ceil
|
from math import ceil
|
||||||
import copy
|
import copy
|
||||||
|
|
||||||
|
from babel.localedata import exists
|
||||||
import jinja2
|
import jinja2
|
||||||
import translitcodec
|
import translitcodec
|
||||||
from paste.deploy.loadwsgi import NicerConfigParser
|
from paste.deploy.loadwsgi import NicerConfigParser
|
||||||
@ -58,6 +59,9 @@ def get_jinja_loader(user_template_path=None):
|
|||||||
return jinja2.PackageLoader('mediagoblin', 'templates')
|
return jinja2.PackageLoader('mediagoblin', 'templates')
|
||||||
|
|
||||||
|
|
||||||
|
SETUP_JINJA_ENVS = {}
|
||||||
|
|
||||||
|
|
||||||
def get_jinja_env(template_loader, locale):
|
def get_jinja_env(template_loader, locale):
|
||||||
"""
|
"""
|
||||||
Set up the Jinja environment,
|
Set up the Jinja environment,
|
||||||
@ -67,6 +71,11 @@ def get_jinja_env(template_loader, locale):
|
|||||||
"""
|
"""
|
||||||
setup_gettext(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(
|
template_env = jinja2.Environment(
|
||||||
loader=template_loader, autoescape=True,
|
loader=template_loader, autoescape=True,
|
||||||
extensions=['jinja2.ext.i18n'])
|
extensions=['jinja2.ext.i18n'])
|
||||||
@ -75,6 +84,9 @@ def get_jinja_env(template_loader, locale):
|
|||||||
mgoblin_globals.translations.gettext,
|
mgoblin_globals.translations.gettext,
|
||||||
mgoblin_globals.translations.ngettext)
|
mgoblin_globals.translations.ngettext)
|
||||||
|
|
||||||
|
if exists(locale):
|
||||||
|
SETUP_JINJA_ENVS[locale] = template_env
|
||||||
|
|
||||||
return template_env
|
return template_env
|
||||||
|
|
||||||
|
|
||||||
@ -330,6 +342,8 @@ def read_config_file(conf_file):
|
|||||||
return mgoblin_conf
|
return mgoblin_conf
|
||||||
|
|
||||||
|
|
||||||
|
SETUP_GETTEXTS = {}
|
||||||
|
|
||||||
def setup_gettext(locale):
|
def setup_gettext(locale):
|
||||||
"""
|
"""
|
||||||
Setup the gettext instance based on this 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
|
# TODO: fallback nicely on translations from pt_PT to pt if not
|
||||||
# available, etc.
|
# available, etc.
|
||||||
this_gettext = gettext.translation(
|
if SETUP_GETTEXTS.has_key(locale):
|
||||||
'mediagoblin', TRANSLATIONS_PATH, [locale], fallback=True)
|
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(
|
mgoblin_globals.setup_globals(
|
||||||
translations=this_gettext)
|
translations=this_gettext)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user