mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-09 03:04:10 -04:00
Dynamic translations!:
- Dynamic access to any of the extant translation sets - Integration into OEBBook to provide book-language translations - Integration with HTML TOC to provide language-appropriate title - oeb2mobi/any2mobi option to provide HTML TOC from the command-line
This commit is contained in:
parent
2857ff00fb
commit
f34034884a
@ -515,6 +515,9 @@ def add_mobi_options(parser):
|
||||
group.add_option(
|
||||
'-r', '--rescale-images', default=False, action='store_true',
|
||||
help=_('Modify images to meet Palm device size limitations.'))
|
||||
group.add_option(
|
||||
'--toc-title', default=None, action='store',
|
||||
help=_('Title for any generated in-line table of contents.'))
|
||||
parser.add_option_group(group)
|
||||
group = OptionGroup(parser, _('Profiles'), _('Device renderer profiles. '
|
||||
'Affects conversion of default font sizes and rasterization '
|
||||
@ -558,7 +561,7 @@ def oeb2mobi(opts, inpath):
|
||||
imagemax = PALM_MAX_IMAGE_SIZE if opts.rescale_images else None
|
||||
context = Context(source, dest)
|
||||
oeb = OEBBook(inpath, logger=logger)
|
||||
tocadder = HTMLTOCAdder()
|
||||
tocadder = HTMLTOCAdder(title=opts.toc_title)
|
||||
tocadder.transform(oeb, context)
|
||||
mangler = CaseMangler()
|
||||
mangler.transform(oeb, context)
|
||||
|
@ -20,6 +20,7 @@ import copy
|
||||
from lxml import etree
|
||||
from lxml import html
|
||||
from calibre import LoggingInterface
|
||||
from calibre.translations.dynamic import translate
|
||||
|
||||
XML_PARSER = etree.XMLParser(recover=True)
|
||||
XML_NS = 'http://www.w3.org/XML/1998/namespace'
|
||||
@ -973,6 +974,11 @@ class OEBBook(object):
|
||||
self._toc_from_opf(opf)
|
||||
self._ensure_cover_image()
|
||||
|
||||
def translate(self, text):
|
||||
lang = str(self.metadata.language[0])
|
||||
lang = lang.split('-', 1)[0].lower()
|
||||
return translate(lang, text)
|
||||
|
||||
def to_opf1(self):
|
||||
package = etree.Element('package',
|
||||
attrib={'unique-identifier': self.uid.id})
|
||||
|
@ -44,13 +44,15 @@ body > .calibre_toc_block {
|
||||
}
|
||||
|
||||
class HTMLTOCAdder(object):
|
||||
def __init__(self, style='nested'):
|
||||
def __init__(self, title=None, style='nested'):
|
||||
self.title = title
|
||||
self.style = style
|
||||
|
||||
def transform(self, oeb, context):
|
||||
if 'toc' in oeb.guide:
|
||||
return
|
||||
oeb.logger.info('Generating in-line TOC...')
|
||||
title = self.title or oeb.translate('Table of Contents')
|
||||
style = self.style
|
||||
if style not in STYLE_CSS:
|
||||
oeb.logger.error('Unknown TOC style %r' % style)
|
||||
@ -61,15 +63,15 @@ class HTMLTOCAdder(object):
|
||||
contents = element(None, XHTML('html'), nsmap={None: XHTML_NS},
|
||||
attrib={XML('lang'): language})
|
||||
head = element(contents, XHTML('head'))
|
||||
title = element(head, XHTML('title'))
|
||||
title.text = 'Table of Contents'
|
||||
htitle = element(head, XHTML('title'))
|
||||
htitle.text = title
|
||||
element(head, XHTML('link'), rel='stylesheet', type=CSS_MIME,
|
||||
href=css_href)
|
||||
body = element(contents, XHTML('body'),
|
||||
attrib={'class': 'calibre_toc'})
|
||||
h1 = element(body, XHTML('h1'),
|
||||
attrib={'class': 'calibre_toc_header'})
|
||||
h1.text = 'Table of Contents'
|
||||
h1.text = title
|
||||
self.add_toc_level(body, oeb.toc)
|
||||
id, href = oeb.manifest.generate('contents', 'contents.xhtml')
|
||||
item = oeb.manifest.add(id, href, XHTML_MIME, data=contents)
|
||||
|
27
src/calibre/translations/dynamic.py
Normal file
27
src/calibre/translations/dynamic.py
Normal file
@ -0,0 +1,27 @@
|
||||
'''
|
||||
Dynamic language lookup of translations for user-visible strings.
|
||||
'''
|
||||
|
||||
__license__ = 'GPL v3'
|
||||
__copyright__ = '2008, Marshall T. Vandegrift <llasram@gmail.com>'
|
||||
|
||||
import sys
|
||||
from cStringIO import StringIO
|
||||
from gettext import GNUTranslations, NullTranslations
|
||||
from calibre.translations.compiled import translations
|
||||
|
||||
__all__ = ['translate']
|
||||
|
||||
_CACHE = {}
|
||||
|
||||
def translate(lang, text):
|
||||
trans = None
|
||||
if lang in _CACHE:
|
||||
trans = _CACHE[lang]
|
||||
elif lang in translations:
|
||||
buf = StringIO(translations[lang])
|
||||
trans = GNUTranslations(buf)
|
||||
_CACHE[lang] = trans
|
||||
if trans is None:
|
||||
return _(text)
|
||||
return trans.ugettext(text)
|
Loading…
x
Reference in New Issue
Block a user