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

I would like to determine if there is a translation into the current language for a given string. I would like to write something like:

if not translation_available("my string"): log_warning_somewhere() 

I did not find anything suitable. The ugettext function simply returns the translation or the source string (if the translation is not available), but without any option to determine if the translation exists or not.

Thanks.

+7
python django internationalization
source share
3 answers

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

Something on these (unverified) 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 ()' only returns True for both fuzzy and / or outdated phrases.

+2
source share

Since, as you say, ugettext will return the original string if the translation is not available, can you just compare the return value with the original to see if they are identical?

0
source share
 def translation_available(msg): return ugettext(msg) == msg 
0
source share

All Articles