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. Browser-native date input field by kytta 4 weeks, 1 day ago
  2. Generate and render HTML Table by LLyaudet 1 month, 1 week ago
  3. My firs Snippets by GutemaG 1 month, 1 week ago
  4. FileField having auto upload_to path by junaidmgithub 2 months, 2 weeks ago
  5. LazyPrimaryKeyRelatedField by LLyaudet 2 months, 3 weeks ago

Comments

Please login first before commenting.