Register Guidelines E-Books Today's Posts Search

Go Back   MobileRead Forums > E-Book Software > Calibre > Development

Notices

Reply
 
Thread Tools Search this Thread
Old 03-04-2021, 08:48 AM   #1
capink
Wizard
capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.
 
Posts: 1,092
Karma: 1948136
Join Date: Aug 2015
Device: Kindle
Problems with translated strings

Thers is a problem reported by BetterRed in regards to menu items in the Favourites Menu plugin. The problem is that when a user switches to a different language, the menu item text is changed to a translated string in that language, and the Favourites Menu plugin cannot find the menu item because it relies on it having the same name.

This problem would also affect the action chains plugin as it shares code with the Favourites Menu plugin. I was able to solve the problem, but only partially by using this:

Code:
paths = [paths[0]] + [_(x) for x in paths[1:]]
This only solves the problem if the user configures the Favourites Menu plugin in English and switches to another language. However, it does not help with the reverse situation when a user configures the plugin in a language other than English and then switches languages.

The solution to this, would be to get the original English untranslated string (msgid) from the translated string (msgstr), and persist the msgid in the json file instead of the msgstr. I don't know how to get the msgid from the msgstr. Is this possible? Can someone help with this or any other alternative solutions?
capink is offline   Reply With Quote
Old 03-04-2021, 11:53 AM   #2
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: 43,860
Karma: 22666666
Join Date: Oct 2006
Location: Mumbai, India
Device: Various
well gettext doesnt have a way to do reverse lookups, but the gettext database format is pretty trivial so you should be able to write your own. You could use the source code of the msgunfmt utility as a starting point. You can fin the .mo files in resources/localization/locales.zip in a calibre install folder. And this is the file spec: https://www.gnu.org/software/gettext.../MO-Files.html
kovidgoyal is offline   Reply With Quote
Advert
Old 03-04-2021, 12:01 PM   #3
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: 43,860
Karma: 22666666
Join Date: Oct 2006
Location: Mumbai, India
Device: Various
Actually its a lot easier than that.

Code:

def reverse_lookup(translated_string):
    if not hasattr(reverse_lookup, 'cache'):
         reverse_lookup.cache = {v: k for k, v _.__self__._catalog.items()}
    return reverse_lookup.cache.get(translated_string)
kovidgoyal is offline   Reply With Quote
Old 03-04-2021, 03:43 PM   #4
capink
Wizard
capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.
 
Posts: 1,092
Karma: 1948136
Join Date: Aug 2015
Device: Kindle
Thanks for the reply. I tried the above but I'm getting this error:

Code:
AttributeError: 'NullTranslations' object has no attribute '_catalog'
My knowledge of gettext and translation is very limited. I tried to look for ways to correctly get the catalog object but I failed. The python gettext docs did not offer much help for me.
capink is offline   Reply With Quote
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: 43,860
Karma: 22666666
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 offline   Reply With Quote
Advert
Old 03-05-2021, 05:54 AM   #6
capink
Wizard
capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.
 
Posts: 1,092
Karma: 1948136
Join Date: Aug 2015
Device: Kindle
Thank you very much. It is working now.
capink is offline   Reply With Quote
Reply


Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Diacritical characters get 'translated' Dyslexia Library Management 4 10-11-2015 12:14 PM
Original language / translated from fxp33 ePub 6 09-03-2014 10:01 PM
Translated from: which metadata for the original language of a translated book? fxp33 ePub 5 08-12-2014 02:05 PM
translated literature zeppo General Discussions 6 12-09-2010 04:55 PM
The best translated Book you've ever read Greg Anos General Discussions 34 10-14-2010 09:44 AM


All times are GMT -4. The time now is 06:09 PM.


MobileRead.com is a privately owned, operated and funded community.