From d554fbeb81b92dcdda93390f775835321528d656 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Wed, 23 Aug 2017 09:46:27 +0530 Subject: [PATCH] Compile the website translations --- setup/translations.py | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/setup/translations.py b/setup/translations.py index a361047ea2..412ad08222 100644 --- a/setup/translations.py +++ b/setup/translations.py @@ -276,6 +276,7 @@ class Translations(POT): # {{{ self.compile_content_server_translations() self.freeze_locales() self.compile_user_manual_translations() + self.compile_website_translations() def compile_group(self, files, handle_stats=None, file_ok=None, action_per_file=None): from calibre.constants import islinux @@ -450,6 +451,44 @@ class Translations(POT): # {{{ def stats(self): return self.j(self.d(self.DEST), 'stats.pickle') + def compile_website_translations(self): + from calibre.utils.zipfile import ZipFile, ZipInfo, ZIP_STORED + from calibre.ptempfile import TemporaryDirectory + self.info('Compiling website translations...') + srcbase = self.j(self.d(self.SRC), 'translations', 'website') + fmap = {} + files = [] + stats = {} + done = [] + + def handle_stats(src, nums): + locale = fmap[src] + trans = nums[0] + total = trans if len(nums) == 1 else (trans + nums[1]) + stats[locale] = int(round(100 * trans / total)) + + with TemporaryDirectory() as tdir, ZipFile(self.j(srcbase, 'locales.zip'), 'w', ZIP_STORED) as zf: + for f in os.listdir(srcbase): + if f.endswith('.po'): + l = f.partition('.')[0] + d = os.path.join(tdir, l + '.mo') + f = os.path.join(srcbase, f) + fmap[f] = l + files.append((f, d)) + self.compile_group(files, handle_stats=handle_stats) + + for locale, translated in stats.iteritems(): + if translated > 30: + with open(os.path.join(tdir, locale + '.mo'), 'rb') as f: + raw = f.read() + zi = ZipInfo(os.path.basename(f.name)) + zi.compress_type = ZIP_STORED + zf.writestr(zi, raw) + done.append(locale) + dest = self.j(self.d(self.stats), 'website-languages.txt') + with open(dest, 'wb') as f: + f.write(' '.join(sorted(done))) + def compile_user_manual_translations(self): self.info('Compiling user manual translations...') srcbase = self.j(self.d(self.SRC), 'translations', 'manual')