Issue
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.
Solution
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.
Answered By - Simon Steinberger
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.