mirror of
https://github.com/gryf/ebook-converter.git
synced 2026-02-27 04:35:58 +01:00
Here is the first batch of modules, which are needed for converting several formats to LRF. Some of the logic has been change, more cleanups will follow.
40 lines
1.2 KiB
Python
40 lines
1.2 KiB
Python
from __future__ import absolute_import, division, print_function, unicode_literals
|
|
'''
|
|
Dynamic language lookup of translations for user-visible strings.
|
|
'''
|
|
|
|
__license__ = 'GPL v3'
|
|
__copyright__ = '2008, Marshall T. Vandegrift <llasram@gmail.com>'
|
|
|
|
import io
|
|
from gettext import GNUTranslations
|
|
from ebook_converter.constants import ispy3
|
|
from ebook_converter.utils.localization import get_lc_messages_path
|
|
from zipfile import ZipFile
|
|
|
|
__all__ = ['translate']
|
|
|
|
_CACHE = {}
|
|
|
|
|
|
def translate(lang, text):
|
|
trans = None
|
|
if lang in _CACHE:
|
|
trans = _CACHE[lang]
|
|
else:
|
|
mpath = get_lc_messages_path(lang)
|
|
if mpath is not None:
|
|
with ZipFile(P('localization/locales.zip',
|
|
allow_user_override=False), 'r') as zf:
|
|
try:
|
|
buf = io.BytesIO(zf.read(mpath + '/messages.mo'))
|
|
except Exception:
|
|
pass
|
|
else:
|
|
trans = GNUTranslations(buf)
|
|
_CACHE[lang] = trans
|
|
if trans is None:
|
|
return getattr(__builtins__, '_', lambda x: x)(text)
|
|
f = getattr(trans, 'gettext' if ispy3 else 'ugettext')
|
|
return f(text)
|