From 4cde1c91e7f48000a7b78995e257e362a43615fe Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Sun, 19 Jul 2009 10:46:33 -0600 Subject: [PATCH] Fix #2861 (changes in mobi metadata reset the language to NEUTRAL) --- src/calibre/ebooks/mobi/langcodes.py | 23 ++++++++++++++++++++++- src/calibre/ebooks/mobi/reader.py | 8 ++++++-- 2 files changed, 28 insertions(+), 3 deletions(-) diff --git a/src/calibre/ebooks/mobi/langcodes.py b/src/calibre/ebooks/mobi/langcodes.py index da0b3ef62e..5d085906df 100644 --- a/src/calibre/ebooks/mobi/langcodes.py +++ b/src/calibre/ebooks/mobi/langcodes.py @@ -5,6 +5,9 @@ __docformat__ = 'restructuredtext en' from struct import pack +lang_codes = { + } + main_language = { 0 : "NEUTRAL", 54 : "AFRIKAANS", @@ -314,7 +317,7 @@ def iana2mobi(icode): if lang in IANA_MOBI: langdict = IANA_MOBI[lang] break - + mcode = langdict[None] while len(subtags) > 0: subtag = subtags.pop(0) @@ -326,3 +329,21 @@ def iana2mobi(icode): mcode = langdict[subtag] break return pack('>HBB', 0, mcode[1], mcode[0]) + +def mobi2iana(langcode, sublangcode): + prefix = suffix = None + for code, d in IANA_MOBI.items(): + for subcode, t in d.items(): + cc, cl = t + if cc == langcode: + prefix = code + if cl == sublangcode: + suffix = subcode.lower() if subcode else None + break + if prefix is not None: + break + if prefix is None: + return 'und' + if suffix is None: + return prefix + return prefix + '-' + suffix diff --git a/src/calibre/ebooks/mobi/reader.py b/src/calibre/ebooks/mobi/reader.py index ac7619cbb6..516a388a63 100644 --- a/src/calibre/ebooks/mobi/reader.py +++ b/src/calibre/ebooks/mobi/reader.py @@ -27,7 +27,7 @@ from calibre.ebooks import DRMError from calibre.ebooks.chardet import ENCODING_PATS from calibre.ebooks.mobi import MobiError from calibre.ebooks.mobi.huffcdic import HuffReader -from calibre.ebooks.mobi.langcodes import main_language, sub_language +from calibre.ebooks.mobi.langcodes import main_language, sub_language, mobi2iana from calibre.ebooks.compression.palmdoc import decompress_doc from calibre.ebooks.metadata import MetaInformation from calibre.ebooks.metadata.opf2 import OPFCreator, OPF @@ -163,7 +163,11 @@ class BookHeader(object): if self.exth_flag & 0x40: self.exth = EXTHHeader(raw[16 + self.length:], self.codec, self.title) self.exth.mi.uid = self.unique_id - self.exth.mi.language = self.language + try: + self.exth.mi.language = mobi2iana(langid, sublangid) + except: + import traceback + traceback.print_exc() class MetadataHeader(BookHeader):