muhuk
muhuk

Reputation: 16095

Django: How to detect if translation is activated?

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?

Upvotes: 2

Views: 802

Answers (3)

bluszcz
bluszcz

Reputation: 4128

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.

Upvotes: 0

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 799150

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

Upvotes: 3

iElectric
iElectric

Reputation: 5819

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

Upvotes: -2

Related Questions