In Django, how to determine if translation for a given text is available?

pcv*_*pcv 7 python django internationalization

I would like to determine, if there's a translation to current language for a given string. I'd like to write something like:

if not translation_available("my string"):
    log_warning_somewhere()
Run Code Online (Sandbox Code Playgroud)

I didn't find anything appropriate. The ugettext function just returns the translation or the original string (if the translation isn't available) but without any option to determine if the translation is there or isn't.

Thanks.

Sim*_*ger 2

您可以使用 polib 来实现: https: //bitbucket.org/izi/polib/wiki/Home

这些(未经测试的)代码行中的一些内容:

import polib
po = polib.pofile('path/your_language.po')
text == 'Your text'
is_translated = any(e for e in po if e.msgid == text and (not e.translated() or 'fuzzy' in e.flags) and not e.obsolete)
Run Code Online (Sandbox Code Playgroud)

当活动翻译可用时,这将给出 True 。对于模糊和/或过时的短语,“e.translated()”单独返回 True。