Django: How to tell if translation is activated?

django.utils.translation.get_language()

returns the default locale if translation is not activated. Is there a way to find out if the transfer is activated (via translation.activate()

) or not?

+2


source to share


3 answers


Terribly hacked, 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

      

+2


source


Depends on application and architecture ...

The hack provided by Ignacio should work, but what will you run on a non-activated thread?



I would use Ignacio's solution + add Queue visible by all threads monkeypatch trans_real.activate and set the attribute on the queue.

0


source


Always check the source code for a question like this, it's faster than posting online!

Django does black magic behind the scenes and uses its dispatcher to simulate disabled translations.

The best way to do it:

import setttings
assert settings.USE_i18N == True

      

-2


source







All Articles