class ResetTranslationMiddleware(object):
def process_request(self, request):
reset_translation_cache()
def reset_translation_cache():
from django.utils import translation
from django.utils.translation import trans_real
from django.utils.thread_support import currentThread
from django.conf import settings
import gettext
if settings.USE_I18N:
try:
# Reset gettext.GNUTranslation cache.
gettext._translations = {}
# Reset Django by-language translation cache.
trans_real._translations = {}
# Delete Django current language translation cache.
trans_real._default = None
# Delete translation cache for the current thread,
# and re-activate the currently selected language (if any)
prev = trans_real._active.pop(currentThread(), None)
if prev:
translation.activate(prev.language())
except AttributeError:
pass
Comments