03-04-2021, 08:48 AM | #1 |
Wizard
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:]] 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? |
03-04-2021, 11:53 AM | #2 |
creator of calibre
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
|
Advert | |
|
03-04-2021, 12:01 PM | #3 |
creator of calibre
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) |
03-04-2021, 03:43 PM | #4 |
Wizard
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' |
03-04-2021, 09:46 PM | #5 |
creator of calibre
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 |
Advert | |
|
03-05-2021, 05:54 AM | #6 |
Wizard
Posts: 1,092
Karma: 1948136
Join Date: Aug 2015
Device: Kindle
|
Thank you very much. It is working now.
|
|
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 |