Login

Middleware to reload translation for each request

Author:
asksol
Posted:
August 28, 2009
Language:
Python
Version:
1.1
Tags:
middleware i18n cache reload translation reset rosetta
Score:
1 (after 1 ratings)

This will reload the translation file for every request if you add it as a middleware in settings.MIDDLEWARE_CLASSES.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
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

More like this

  1. GlobalRequest middleware by myq 2 years, 6 months ago
  2. logging tastypie middleware by lalutium 6 months, 3 weeks ago
  3. Set language via HTTP GET Parameter by schmidsi 5 years, 2 months ago
  4. Request time logging middleware by mpasternacki 5 years, 5 months ago
  5. Subdomain Middleware by xhenxhe 6 years, 7 months ago

Comments

Please login first before commenting.