Store translations compressed in the calibre install dir. Also remove the broken librarything executable.

This commit is contained in:
Kovid Goyal 2011-06-04 22:25:46 -06:00
parent 3fb57e64f2
commit 4a8d33a455
4 changed files with 49 additions and 32 deletions

View File

@ -85,7 +85,7 @@ class Translations(POT):
def mo_file(self, po_file): def mo_file(self, po_file):
locale = os.path.splitext(os.path.basename(po_file))[0] locale = os.path.splitext(os.path.basename(po_file))[0]
return locale, os.path.join(self.DEST, locale, 'LC_MESSAGES', 'messages.mo') return locale, os.path.join(self.DEST, locale, 'messages.mo')
def run(self, opts): def run(self, opts):
@ -94,9 +94,8 @@ class Translations(POT):
base = os.path.dirname(dest) base = os.path.dirname(dest)
if not os.path.exists(base): if not os.path.exists(base):
os.makedirs(base) os.makedirs(base)
if self.newer(dest, f): self.info('\tCompiling translations for', locale)
self.info('\tCompiling translations for', locale) subprocess.check_call(['msgfmt', '-o', dest, f])
subprocess.check_call(['msgfmt', '-o', dest, f])
if locale in ('en_GB', 'nds', 'te', 'yi'): if locale in ('en_GB', 'nds', 'te', 'yi'):
continue continue
pycountry = self.j(sysconfig.get_python_lib(), 'pycountry', pycountry = self.j(sysconfig.get_python_lib(), 'pycountry',
@ -123,6 +122,16 @@ class Translations(POT):
shutil.copy2(f, dest) shutil.copy2(f, dest)
self.write_stats() self.write_stats()
self.freeze_locales()
def freeze_locales(self):
zf = self.DEST + '.zip'
from calibre import CurrentDir
from calibre.utils.zipfile import ZipFile, ZIP_DEFLATED
with ZipFile(zf, 'w', ZIP_DEFLATED) as zf:
with CurrentDir(self.DEST):
zf.add_dir('.')
shutil.rmtree(self.DEST)
@property @property
def stats(self): def stats(self):

View File

@ -23,7 +23,6 @@ entry_points = {
'calibre-server = calibre.library.server.main:main', 'calibre-server = calibre.library.server.main:main',
'lrf2lrs = calibre.ebooks.lrf.lrfparser:main', 'lrf2lrs = calibre.ebooks.lrf.lrfparser:main',
'lrs2lrf = calibre.ebooks.lrf.lrs.convert_from:main', 'lrs2lrf = calibre.ebooks.lrf.lrs.convert_from:main',
'librarything = calibre.ebooks.metadata.library_thing:main',
'calibre-debug = calibre.debug:main', 'calibre-debug = calibre.debug:main',
'calibredb = calibre.library.cli:main', 'calibredb = calibre.library.cli:main',
'calibre-parallel = calibre.utils.ipc.worker:main', 'calibre-parallel = calibre.utils.ipc.worker:main',

View File

@ -5,10 +5,9 @@ Dynamic language lookup of translations for user-visible strings.
__license__ = 'GPL v3' __license__ = 'GPL v3'
__copyright__ = '2008, Marshall T. Vandegrift <llasram@gmail.com>' __copyright__ = '2008, Marshall T. Vandegrift <llasram@gmail.com>'
import os import cStringIO
from gettext import GNUTranslations from gettext import GNUTranslations
from calibre.utils.localization import get_lc_messages_path from calibre.utils.localization import get_lc_messages_path, ZipFile
__all__ = ['translate'] __all__ = ['translate']
@ -21,10 +20,15 @@ def translate(lang, text):
else: else:
mpath = get_lc_messages_path(lang) mpath = get_lc_messages_path(lang)
if mpath is not None: if mpath is not None:
p = os.path.join(mpath, 'messages.mo') with ZipFile(P('localization/locales.zip',
if os.path.exists(p): allow_user_override=False), 'r') as zf:
trans = GNUTranslations(open(p, 'rb')) try:
_CACHE[lang] = trans buf = cStringIO.StringIO(zf.read(mpath + '/messages.mo'))
except:
pass
else:
trans = GNUTranslations(buf)
_CACHE[lang] = trans
if trans is None: if trans is None:
return getattr(__builtins__, '_', lambda x: x)(text) return getattr(__builtins__, '_', lambda x: x)(text)
return trans.ugettext(text) return trans.ugettext(text)

View File

@ -1,6 +1,6 @@
#!/usr/bin/env python #!/usr/bin/env python
# vim:fileencoding=UTF-8:ts=4:sw=4:sta:et:sts=4:ai # vim:fileencoding=UTF-8:ts=4:sw=4:sta:et:sts=4:ai
from __future__ import with_statement from __future__ import absolute_import
__license__ = 'GPL v3' __license__ = 'GPL v3'
__copyright__ = '2009, Kovid Goyal <kovid@kovidgoyal.net>' __copyright__ = '2009, Kovid Goyal <kovid@kovidgoyal.net>'
@ -8,13 +8,14 @@ __docformat__ = 'restructuredtext en'
import os, locale, re, cStringIO, cPickle import os, locale, re, cStringIO, cPickle
from gettext import GNUTranslations from gettext import GNUTranslations
from zipfile import ZipFile
_available_translations = None _available_translations = None
def available_translations(): def available_translations():
global _available_translations global _available_translations
if _available_translations is None: if _available_translations is None:
stats = P('localization/stats.pickle') stats = P('localization/stats.pickle', allow_user_override=False)
if os.path.exists(stats): if os.path.exists(stats):
stats = cPickle.load(open(stats, 'rb')) stats = cPickle.load(open(stats, 'rb'))
else: else:
@ -49,21 +50,20 @@ def get_lang():
lang = 'en' lang = 'en'
return lang return lang
def messages_path(lang):
return P('localization/locales/%s/LC_MESSAGES'%lang)
def get_lc_messages_path(lang): def get_lc_messages_path(lang):
hlang = None hlang = None
if lang in available_translations(): if zf_exists():
hlang = lang if lang in available_translations():
else: hlang = lang
xlang = lang.split('_')[0] else:
if xlang in available_translations(): xlang = lang.split('_')[0]
hlang = xlang if xlang in available_translations():
if hlang is not None: hlang = xlang
return messages_path(hlang) return hlang
return None
def zf_exists():
return os.path.exists(P('localization/locales.zip',
allow_user_override=False))
def set_translators(): def set_translators():
# To test different translations invoke as # To test different translations invoke as
@ -79,12 +79,17 @@ def set_translators():
mpath = get_lc_messages_path(lang) mpath = get_lc_messages_path(lang)
if mpath is not None: if mpath is not None:
if buf is None: with ZipFile(P('localization/locales.zip',
buf = open(os.path.join(mpath, 'messages.mo'), 'rb') allow_user_override=False), 'r') as zf:
mpath = mpath.replace(os.sep+'nds'+os.sep, os.sep+'de'+os.sep) if buf is None:
isof = os.path.join(mpath, 'iso639.mo') buf = cStringIO.StringIO(zf.read(mpath + '/messages.mo'))
if os.path.exists(isof): if mpath == 'nds':
iso639 = open(isof, 'rb') mpath = 'de'
isof = mpath + '/iso639.mo'
try:
iso639 = cStringIO.StringIO(zf.read(isof))
except:
pass # No iso639 translations for this lang
if buf is not None: if buf is not None:
t = GNUTranslations(buf) t = GNUTranslations(buf)