views:

154

answers:

3

django.utils.translation.get_language() returns default locale if translation is not activated. Is there a way to find out whether the translation is activated (via translation.activate()) or not?

A: 

Always inspect source code for such question, it's faster than posting to Web!

Django does it's black magic behind the scene, and uses some kind of dispatcher to simulate disabled translations.

The best way for you to do is:

import setttings
assert settings.USE_i18N == True
iElectric
Always make sure you understand the question before posting an answer. I'm asking whether or not `translation.activate()` has been called.
muhuk
Also you shouldn't import settings directly. Preferred way is `from django.conf import settings`, see docs for more information. Please correct your code.
muhuk
+2  A: 

Horribly hacky, but should work in at least 1.1.1:

import django.utils.translation.trans_real as trans
from django.utils.thread_support import currentThread

def isactive():
  return currentThread() in trans._active
Ignacio Vazquez-Abrams
Hacky but not horribly so. +1
muhuk
A: 

Depends on application and architecture...

Hack provided by Ignacio should works, but what is you will run in non activated yet thread?

I would use Ignacio solution + add Queue visible by all threads, monkeypatch trans_real.activate function and set attribute in queue.

bluszcz