diff --git a/src/calibre/ebooks/metadata/epub.py b/src/calibre/ebooks/metadata/epub.py index 27fa94e217..30fe53f1a2 100644 --- a/src/calibre/ebooks/metadata/epub.py +++ b/src/calibre/ebooks/metadata/epub.py @@ -16,6 +16,7 @@ from calibre.ebooks.metadata.opf2 import OPF from calibre.ptempfile import TemporaryDirectory, PersistentTemporaryFile from calibre import CurrentDir, walk from calibre.constants import isosx +from calibre.utils.localization import lang_as_iso639_1 class EPubException(Exception): pass @@ -231,6 +232,15 @@ def set_metadata(stream, mi, apply_null=False, update_timestamp=False): for x in ('guide', 'toc', 'manifest', 'spine'): setattr(mi, x, None) + if mi.languages: + langs = [] + for lc in mi.languages: + lc2 = lang_as_iso639_1(lc) + if lc2: lc = lc2 + langs.append(lc) + mi.languages = langs + + reader.opf.smart_update(mi) if apply_null: if not getattr(mi, 'series', None): diff --git a/src/calibre/ebooks/oeb/output.py b/src/calibre/ebooks/oeb/output.py index 816fe71abb..38ac2495fd 100644 --- a/src/calibre/ebooks/oeb/output.py +++ b/src/calibre/ebooks/oeb/output.py @@ -43,6 +43,7 @@ class OEBOutput(OutputFormatPlugin): except: self.log.exception('Something went wrong while trying to' ' workaround Pocketbook cover bug, ignoring') + self.migrate_lang_code(root) raw = etree.tostring(root, pretty_print=True, encoding='utf-8', xml_declaration=True) if key == OPF_MIME: @@ -104,3 +105,12 @@ class OEBOutput(OutputFormatPlugin): p.remove(m) p.insert(0, m) # }}} + + def migrate_lang_code(self, root): # {{{ + from calibre.utils.localization import lang_as_iso639_1 + for lang in root.xpath('//*[local-name() = "language"]'): + clc = lang_as_iso639_1(lang.text) + if clc: + lang.text = clc + # }}} +