Login

Middleware to reload translation for each request

Author:
asksol
Posted:
August 28, 2009
Language:
Python
Version:
1.1
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. Template tag - list punctuation for a list of items by shapiromatron 9 months, 3 weeks ago
  2. JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 10 months ago
  3. Serializer factory with Django Rest Framework by julio 1 year, 4 months ago
  4. Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 5 months ago
  5. Help text hyperlinks by sa2812 1 year, 6 months ago

Comments

Please login first before commenting.