Register Guidelines E-Books Search Today's Posts Mark Forums Read

Go Back   MobileRead Forums > E-Book Software > Calibre

Notices

Reply
 
Thread Tools Search this Thread
Old 07-17-2011, 05:29 PM   #1
siebert
Developer
siebert has a complete set of Star Wars action figures.siebert has a complete set of Star Wars action figures.siebert has a complete set of Star Wars action figures.
 
Posts: 137
Karma: 280
Join Date: Nov 2010
Device: Kindle 3 (Keyboard) 3G / iPad 3 WiFi / Nexus 4 (Android)
Patch: Preserve language setting for mobi and epub files.

Due to a bug in calibre the language metadata field in saved or converted mobi and epub files is overwritten with the current GUI language (see https://bugs.launchpad.net/calibre/+bug/809771).

As the correct language is needed for dictionary lookups in the Kindle App, I created a hack which stores the language value in an identifier named 'language'.

This fixes the language field bug and also gives the user a way to control and change the value in the calibre GUI.

I'm rather sure that this patch depends on my previous patch found in http://www.mobileread.com/forums/sho...58&postcount=1, so please apply both when you're interested in this patch.

The patch was created against calibre 0.8.8.

Ciao,
Steffen

Code:
diff --git a/src/calibre/ebooks/metadata/epub.py b/src/calibre/ebooks/metadata/epub.py
index 27fa94e..d46c160 100644
--- a/src/calibre/ebooks/metadata/epub.py
+++ b/src/calibre/ebooks/metadata/epub.py
@@ -186,6 +186,8 @@ def get_metadata(stream, extract_cover=True):
         except:
             import traceback
             traceback.print_exc()
+    if mi.language is not None and not mi.has_identifier('language'):
+        mi.set_identifier('language', mi.language)
     mi.timestamp = None
     return mi
 
diff --git a/src/calibre/ebooks/metadata/mobi.py b/src/calibre/ebooks/metadata/mobi.py
index fe4950c..98a3d50 100644
--- a/src/calibre/ebooks/metadata/mobi.py
+++ b/src/calibre/ebooks/metadata/mobi.py
@@ -404,6 +404,8 @@ class MetadataUpdater(object):
         if getattr(self, 'exth', None) is None:
             raise MobiError('No existing EXTH record. Cannot update metadata.')
 
+        if mi.has_identifier('language'):
+            mi.language = mi.get_identifiers()['language']
         if not mi.is_null('language'):
             self.record0[92:96] = iana2mobi(mi.language)
         self.create_exth(exth=exth, new_title=mi.title)
diff --git a/src/calibre/ebooks/metadata/opf2.py b/src/calibre/ebooks/metadata/opf2.py
index 186821b..9dc1630 100644
--- a/src/calibre/ebooks/metadata/opf2.py
+++ b/src/calibre/ebooks/metadata/opf2.py
@@ -1044,7 +1044,10 @@ class OPF(object): # {{{
             val = getattr(mi, attr, None)
             if val is not None and val != [] and val != (None, None):
                 setattr(self, attr, val)
-        lang = getattr(mi, 'language', None)
+        if mi.has_identifier('language'):
+            lang = mi.get_identifiers()['language']
+        else:
+            lang = getattr(mi, 'language', None)
         if lang and lang != 'und':
             self.language = lang
         temp = self.to_book_metadata()
@@ -1281,7 +1284,10 @@ def metadata_to_opf(mi, as_string=True):
             '[http://calibre-ebook.com]'
 
     if not mi.language:
-        mi.language = 'UND'
+        if mi.has_identifier('language'):
+            mi.language = mi.get_identifiers()['language']
+        else:
+            mi.language = 'UND'
 
     root = etree.fromstring(textwrap.dedent(
     '''
@@ -1331,7 +1337,11 @@ def metadata_to_opf(mi, as_string=True):
         factory(DC('identifier'), val, scheme=icu_upper(key))
     if mi.rights:
         factory(DC('rights'), mi.rights)
-    factory(DC('language'), mi.language if mi.language and mi.language.lower()
+    if mi.has_identifier('language'):
+        lang = mi.get_identifiers()['language']
+    else:
+        lang = mi.language
+    factory(DC('language'), lang if lang and lang.lower()
             != 'und' else get_lang().replace('_', '-'))
     if mi.tags:
         for tag in mi.tags:
diff --git a/src/calibre/ebooks/mobi/reader.py b/src/calibre/ebooks/mobi/reader.py
index 1173b84..9c04009 100644
--- a/src/calibre/ebooks/mobi/reader.py
+++ b/src/calibre/ebooks/mobi/reader.py
@@ -1000,4 +1000,6 @@ def get_metadata(stream):
         obuf = cStringIO.StringIO()
         im.convert('RGB').save(obuf, format='JPEG')
         mi.cover_data = ('jpg', obuf.getvalue())
+    if mi.language is not None and not mi.has_identifier('language'):
+        mi.set_identifier('language', mi.language)
     return mi
diff --git a/src/calibre/ebooks/oeb/reader.py b/src/calibre/ebooks/oeb/reader.py
index 422252f..dc558df 100644
--- a/src/calibre/ebooks/oeb/reader.py
+++ b/src/calibre/ebooks/oeb/reader.py
@@ -131,7 +131,10 @@ class OEBReader(object):
         stream = cStringIO.StringIO(etree.tostring(opf))
         mi = OPF(stream).to_book_metadata()
         if not mi.language:
-            mi.language = get_lang().replace('_', '-')
+            if mi.has_identifier('language'):
+                mi.language = mi.get_identifiers()['language']
+            else:
+                mi.language = get_lang().replace('_', '-')
         self.oeb.metadata.add('language', mi.language)
         if not mi.book_producer:
             mi.book_producer = '%(a)s (%(v)s) [http://%(a)s-ebook.com]'%\
diff --git a/src/calibre/ebooks/oeb/transforms/metadata.py b/src/calibre/ebooks/oeb/transforms/metadata.py
index 3980c21..a75ae5a 100644
--- a/src/calibre/ebooks/oeb/transforms/metadata.py
+++ b/src/calibre/ebooks/oeb/transforms/metadata.py
@@ -60,7 +60,14 @@ def meta_info_to_oeb_metadata(mi, m, log, override_input_metadata=False):
             m.add('identifier', mi.isbn, scheme='ISBN')
     elif override_input_metadata:
         m.filter('identifier', lambda x: x.scheme.lower() == 'isbn')
-    if not mi.is_null('language'):
+    has = False
+    for x in m.identifier:
+        if x.scheme.lower() == 'language':
+            m.clear('language')
+            m.add('language', x.content)
+            has = True
+            break
+    if not has and not mi.is_null('language'):
         m.clear('language')
         m.add('language', mi.language)
     if not mi.is_null('series_index'):

Last edited by siebert; 07-17-2011 at 05:45 PM.
siebert is offline   Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Patch: Use real ASIN instead of UUID in mobi files to show correct cover in KindleApp siebert Calibre 4 02-24-2012 09:13 AM
eBook language setting. Vizzy Calibre 5 11-01-2010 02:03 PM
Language metadata in ePub files HarryT Calibre 24 07-19-2010 11:24 PM
Thanks! Language setting is working sjohnson717 Calibre 1 06-26-2010 12:00 PM
preserve images on converting to MOBI joselitux Calibre 7 05-28-2010 06:49 AM


All times are GMT -4. The time now is 04:40 AM.


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