Merge remote-tracking branch 'refs/remotes/gandaro/369-thread-gettext'

This commit is contained in:
Christopher Allan Webber 2012-07-26 11:09:52 -05:00
commit a04c07376d
3 changed files with 11 additions and 8 deletions

View File

@ -19,6 +19,7 @@ In some places, we need to access the database, public_store, queue_store
import gettext import gettext
import pkg_resources import pkg_resources
import threading
############################# #############################
@ -41,8 +42,11 @@ queue_store = None
# A WorkBenchManager # A WorkBenchManager
workbench_manager = None workbench_manager = None
# A thread-local scope
thread_scope = threading.local()
# gettext # gettext
translations = gettext.find( thread_scope.translations = gettext.find(
'mediagoblin', 'mediagoblin',
pkg_resources.resource_filename( pkg_resources.resource_filename(
'mediagoblin', 'translations'), ['en']) 'mediagoblin', 'translations'), ['en'])

View File

@ -49,8 +49,8 @@ def get_jinja_env(template_loader, locale):
extensions=['jinja2.ext.i18n', 'jinja2.ext.autoescape']) extensions=['jinja2.ext.i18n', 'jinja2.ext.autoescape'])
template_env.install_gettext_callables( template_env.install_gettext_callables(
mg_globals.translations.ugettext, mg_globals.thread_scope.translations.ugettext,
mg_globals.translations.ungettext) mg_globals.thread_scope.translations.ungettext)
# All templates will know how to ... # All templates will know how to ...
# ... fetch all waiting messages and remove them from the queue # ... fetch all waiting messages and remove them from the queue

View File

@ -100,7 +100,7 @@ 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.
if SETUP_GETTEXTS.has_key(locale): if locale in SETUP_GETTEXTS:
this_gettext = SETUP_GETTEXTS[locale] this_gettext = SETUP_GETTEXTS[locale]
else: else:
this_gettext = gettext.translation( this_gettext = gettext.translation(
@ -108,8 +108,7 @@ def setup_gettext(locale):
if exists(locale): if exists(locale):
SETUP_GETTEXTS[locale] = this_gettext SETUP_GETTEXTS[locale] = this_gettext
mg_globals.setup_globals( mg_globals.thread_scope.translations = this_gettext
translations=this_gettext)
# Force en to be setup before anything else so that # Force en to be setup before anything else so that
@ -124,7 +123,7 @@ def pass_to_ugettext(*args, **kwargs):
The reason we can't have a global ugettext method is because The reason we can't have a global ugettext method is because
mg_globals gets swapped out by the application per-request. mg_globals gets swapped out by the application per-request.
""" """
return mg_globals.translations.ugettext( return mg_globals.thread_scope.translations.ugettext(
*args, **kwargs) *args, **kwargs)
@ -146,7 +145,7 @@ def pass_to_ngettext(*args, **kwargs):
The reason we can't have a global ngettext method is because The reason we can't have a global ngettext method is because
mg_globals gets swapped out by the application per-request. mg_globals gets swapped out by the application per-request.
""" """
return mg_globals.translations.ngettext( return mg_globals.thread_scope.translations.ngettext(
*args, **kwargs) *args, **kwargs)