Allow running the translations builder on OSes other than linux where msgfmt is not available

This commit is contained in:
Kovid Goyal 2016-06-26 11:51:39 +05:30
parent 7e68c19381
commit 51a6d79039
2 changed files with 22 additions and 6 deletions

View File

@ -6,7 +6,7 @@ __license__ = 'GPL v3'
__copyright__ = '2009, Kovid Goyal <kovid@kovidgoyal.net>' __copyright__ = '2009, Kovid Goyal <kovid@kovidgoyal.net>'
__docformat__ = 'restructuredtext en' __docformat__ = 'restructuredtext en'
import os, tempfile, shutil, subprocess, glob, re, time, textwrap, cPickle, shlex, json, errno, hashlib import os, tempfile, shutil, subprocess, glob, re, time, textwrap, cPickle, shlex, json, errno, hashlib, sys
from collections import defaultdict from collections import defaultdict
from locale import normalize as normalize_locale from locale import normalize as normalize_locale
from functools import partial from functools import partial
@ -255,6 +255,7 @@ class Translations(POT): # {{{
self.compile_user_manual_translations() self.compile_user_manual_translations()
def compile_group(self, files, handle_stats=None, file_ok=None, action_per_file=None): def compile_group(self, files, handle_stats=None, file_ok=None, action_per_file=None):
from calibre.constants import islinux
jobs, ok_files = [], [] jobs, ok_files = [], []
hashmap = {} hashmap = {}
@ -279,7 +280,11 @@ class Translations(POT): # {{{
else: else:
if file_ok is None or file_ok(data, src): if file_ok is None or file_ok(data, src):
self.info('\t' + os.path.relpath(src, self.j(self.d(self.SRC), 'translations'))) self.info('\t' + os.path.relpath(src, self.j(self.d(self.SRC), 'translations')))
jobs.append(['msgfmt', '--statistics', '-o', dest, src]) if islinux:
msgfmt = ['msgfmt']
else:
msgfmt = [sys.executable, self.j(self.SRC, 'calibre', 'translations', 'msgfmt.py')]
jobs.append(msgfmt + ['--statistics', '-o', dest, src])
ok_files.append((src, dest)) ok_files.append((src, dest))
hashmap[src] = current_hash hashmap[src] = current_hash
if action_per_file is not None: if action_per_file is not None:

View File

@ -1,4 +1,4 @@
#! /usr/bin/env python2 #!/usr/bin/env python2
# Written by Martin v. Loewis <loewis@informatik.hu-berlin.de> # Written by Martin v. Loewis <loewis@informatik.hu-berlin.de>
"""Generate binary message catalog from textual translation description. """Generate binary message catalog from textual translation description.
@ -33,7 +33,7 @@ import array
__version__ = "1.1" __version__ = "1.1"
MESSAGES = {} MESSAGES = {}
STATS = {'translated': 0, 'untranslated': 0}
def usage(code, msg=''): def usage(code, msg=''):
print >> sys.stderr, __doc__ print >> sys.stderr, __doc__
@ -45,8 +45,13 @@ def usage(code, msg=''):
def add(id, str, fuzzy): def add(id, str, fuzzy):
"Add a non-fuzzy translation to the dictionary." "Add a non-fuzzy translation to the dictionary."
global MESSAGES global MESSAGES
if not id:
return
if not fuzzy and str: if not fuzzy and str:
MESSAGES[id] = str MESSAGES[id] = str
STATS['translated'] += 1
else:
STATS['untranslated'] += 1
def generate(): def generate():
@ -193,12 +198,13 @@ def make(filename, outfile):
def main(): def main():
try: try:
opts, args = getopt.getopt(sys.argv[1:], 'hVo:', opts, args = getopt.getopt(sys.argv[1:], 'hVso:',
['help', 'version', 'output-file=']) ['help', 'version', 'statistics', 'output-file='])
except getopt.error, msg: except getopt.error, msg:
usage(1, msg) usage(1, msg)
outfile = None outfile = None
output_stats = False
# parse options # parse options
for opt, arg in opts: for opt, arg in opts:
if opt in ('-h', '--help'): if opt in ('-h', '--help'):
@ -208,6 +214,8 @@ def main():
sys.exit(0) sys.exit(0)
elif opt in ('-o', '--output-file'): elif opt in ('-o', '--output-file'):
outfile = arg outfile = arg
elif opt in ('-s', '--statistics'):
output_stats = True
# do it # do it
if not args: if not args:
print >> sys.stderr, 'No input file given' print >> sys.stderr, 'No input file given'
@ -215,7 +223,10 @@ def main():
return return
for filename in args: for filename in args:
STATS['translated'] = STATS['untranslated'] = 0
make(filename, outfile) make(filename, outfile)
if output_stats:
print STATS['translated'], 'translated messages,', STATS['untranslated'], 'untranslated messages.'
if __name__ == '__main__': if __name__ == '__main__':