Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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()

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.

like image 276
pcv Avatar asked Nov 06 '10 21:11

pcv


1 Answers

You can use polib for that: https://bitbucket.org/izi/polib/wiki/Home

Something along those (untested) lines of code:

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)

This will give True when an active translation is available. 'e.translated()' alone returns True for both, fuzzy and/or obsolete phrases, too.

like image 129
Simon Steinberger Avatar answered Nov 15 '22 05:11

Simon Steinberger