__copyright__ = '2008, Marshall T. Vandegrift <llasram@gmail.com>'
+import os
- import cStringIO
+ import io
from gettext import GNUTranslations
from calibre.utils.localization import get_lc_messages_path
-from zipfile import ZipFile
- with ZipFile(P('localization/locales.zip',
- allow_user_override=False), 'r') as zf:
- try:
-- buf = cStringIO.StringIO(zf.read(mpath + '/messages.mo'))
-- except:
+- buf = io.BytesIO(zf.read(mpath + '/messages.mo'))
+- except Exception:
- pass
- else:
- trans = GNUTranslations(buf)
- for lang in available_translations():
- mpath = get_lc_messages_path(lang)
- if mpath is not None:
-- buf = cStringIO.StringIO(zf.read(mpath + '/messages.mo'))
+- buf = io.BytesIO(zf.read(mpath + '/messages.mo'))
- yield lang, GNUTranslations(buf)
+ for lang in available_translations():
+ mpath = get_lc_messages_path(lang)
lcdata = {
u'abday': (u'Sun', u'Mon', u'Tue', u'Wed', u'Thu', u'Fri', u'Sat'),
-@@ -145,21 +145,20 @@
- buf = cStringIO.StringIO(buf.getvalue())
+@@ -134,20 +134,11 @@
+
+
+ def get_single_translator(mpath, which='messages'):
+- from zipfile import ZipFile
+- with ZipFile(P('localization/locales.zip', allow_user_override=False), 'r') as zf:
+- path = '{}/{}.mo'.format(mpath, which)
+- data = zf.read(path)
+- buf = io.BytesIO(data)
+ try:
++ buf = open(os.path.join(mpath, '/%s.mo' % which), 'rb')
+ return GNUTranslations(buf)
+ except Exception as e:
+- import traceback
+- traceback.print_exc()
+- import hashlib
+- sig = hashlib.sha1(data).hexdigest()
+- raise ValueError('Failed to load translations for: {} (size: {} and signature: {}) with error: {}'.format(
+- path, len(data), sig, e))
++ pass # No translations for this lang
+
+
+ def get_iso639_translator(lang):
+@@ -145,22 +145,21 @@
+ buf = io.BytesIO(buf.getvalue())
if mpath is not None:
- from zipfile import ZipFile
- with ZipFile(P('localization/locales.zip',
- allow_user_override=False), 'r') as zf:
- if buf is None:
-- buf = cStringIO.StringIO(zf.read(mpath + '/messages.mo'))
+- buf = io.BytesIO(zf.read(mpath + '/messages.mo'))
+ if buf is None:
+ try:
+ buf = open(os.path.join(mpath, 'calibre.mo'), 'rb')
mpath = 'de'
- isof = mpath + '/iso639.mo'
try:
-- iso639 = cStringIO.StringIO(zf.read(isof))
+- iso639 = io.BytesIO(zf.read(isof))
+ iso639 = open(os.path.join(mpath, 'calibre_iso639.mo'), 'rb')
except:
pass # No iso639 translations for this lang
if buf is not None:
+ from calibre.utils.serialize import msgpack_loads
try:
-- lcdata = cPickle.loads(zf.read(mpath + '/lcdata.pickle'))
-+ lcdata = cPickle.loads(open(os.path.join(mpath, 'calibre_lcdata.pickle')))
+- lcdata = msgpack_loads(zf.read(mpath + '/lcdata.calibre_msgpack'))
++ lcdata = msgpack_loads(open(os.path.join(mpath, '/lcdata.calibre_msgpack')))
except:
pass # No lcdata