View Single Post
Old 03-04-2021, 09:46 PM   #5
kovidgoyal
creator of calibre
kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.
 
kovidgoyal's Avatar
 
Posts: 45,402
Karma: 27756918
Join Date: Oct 2006
Location: Mumbai, India
Device: Various
That's when there are no translations installed (english). Here you go:

Code:
def reverse_lookup(translated_string):
    if not hasattr(_.__self__, '_catalog'):
         return translated_string
    if not hasattr(reverse_lookup, 'cache'):
         reverse_lookup.cache = {v: k for k, v _.__self__._catalog.items()}
    ans = reverse_lookup.cache.get(translated_string, translated_string)
    if isinstance(ans, tuple):
          ans  = ans[1]
    return
this handles no translations and also plural forms. And yes the docs of gettext are useless for this, read the source of the gettext.py module instead, its pretty simple.
kovidgoyal is online now   Reply With Quote