开发者

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开发者_如何学JAVA find out whether the translation is activated (via translation.activate()) or not?


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


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.


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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜