i18n.py
branchstable
changeset 8743 27a83746aebd
parent 8695 358d8bed9626
child 10589 7c23b7de2b8d
equal deleted inserted replaced
8742:bd374bd906f3 8743:27a83746aebd
    52             w('"%s"' % line.replace('"', r'\"'))
    52             w('"%s"' % line.replace('"', r'\"'))
    53     else:
    53     else:
    54         w('msgid "%s"\n' % msgid[0])
    54         w('msgid "%s"\n' % msgid[0])
    55     w('msgstr ""\n\n')
    55     w('msgstr ""\n\n')
    56 
    56 
    57 
    57 def execute2(args):
    58 def execute(cmd):
    58     # XXX replace this with check_output in Python 2.7
    59     """display the command, execute it and raise an Exception if returned
    59     from subprocess import Popen, PIPE, CalledProcessError
    60     status != 0
    60     p = Popen(args, stdout=PIPE, stderr=PIPE)
    61     """
    61     out, err = p.communicate()
    62     from subprocess import call
    62     if p.returncode != 0:
    63     # use getcwdu as cmd may be unicode and cwd may contains non-ascii
    63         exc = CalledProcessError(p.returncode, args[0])
    64     # characters
    64         exc.cmd = args
    65     print cmd.replace(os.getcwdu() + os.sep, '')
    65         exc.data = (out, err)
    66     status = call(cmd, shell=True)
    66         raise exc
    67     if status != 0:
       
    68         raise Exception('status = %s' % status)
       
    69 
       
    70 
    67 
    71 def available_catalogs(i18ndir=None):
    68 def available_catalogs(i18ndir=None):
    72     if i18ndir is None:
    69     if i18ndir is None:
    73         wildcard = '*.po'
    70         wildcard = '*.po'
    74     else:
    71     else:
    79 
    76 
    80 
    77 
    81 def compile_i18n_catalogs(sourcedirs, destdir, langs):
    78 def compile_i18n_catalogs(sourcedirs, destdir, langs):
    82     """generate .mo files for a set of languages into the `destdir` i18n directory
    79     """generate .mo files for a set of languages into the `destdir` i18n directory
    83     """
    80     """
       
    81     from subprocess import CalledProcessError
    84     from logilab.common.fileutils import ensure_fs_mode
    82     from logilab.common.fileutils import ensure_fs_mode
    85     print '-> compiling message catalogs to %s' % destdir
    83     print '-> compiling message catalogs to %s' % destdir
    86     errors = []
    84     errors = []
    87     for lang in langs:
    85     for lang in langs:
    88         langdir = join(destdir, lang, 'LC_MESSAGES')
    86         langdir = join(destdir, lang, 'LC_MESSAGES')
    91         pofiles = [join(path, '%s.po' % lang) for path in sourcedirs]
    89         pofiles = [join(path, '%s.po' % lang) for path in sourcedirs]
    92         pofiles = [pof for pof in pofiles if exists(pof)]
    90         pofiles = [pof for pof in pofiles if exists(pof)]
    93         mergedpo = join(destdir, '%s_merged.po' % lang)
    91         mergedpo = join(destdir, '%s_merged.po' % lang)
    94         try:
    92         try:
    95             # merge instance/cubes messages catalogs with the stdlib's one
    93             # merge instance/cubes messages catalogs with the stdlib's one
    96             execute('msgcat --use-first --sort-output --strict -o "%s" %s'
    94             cmd = ['msgcat', '--use-first', '--sort-output', '--strict',
    97                     % (mergedpo, ' '.join('"%s"' % f for f in pofiles)))
    95                    '-o', mergedpo] + pofiles
       
    96             execute2(cmd)
    98             # make sure the .mo file is writeable and compiles with *msgfmt*
    97             # make sure the .mo file is writeable and compiles with *msgfmt*
    99             applmo = join(destdir, lang, 'LC_MESSAGES', 'cubicweb.mo')
    98             applmo = join(destdir, lang, 'LC_MESSAGES', 'cubicweb.mo')
   100             try:
    99             try:
   101                 ensure_fs_mode(applmo)
   100                 ensure_fs_mode(applmo)
   102             except OSError:
   101             except OSError:
   103                 pass # suppose not exists
   102                 pass # suppose not exists
   104             execute('msgfmt "%s" -o "%s"' % (mergedpo, applmo))
   103             execute2(['msgfmt', mergedpo, '-o', applmo])
   105         except Exception, ex:
   104         except CalledProcessError as exc:
   106             errors.append('while handling language %s: %s' % (lang, ex))
   105             errors.append(u'while handling language %s:\ncmd:\n%s\nstdout:\n%s\nstderr:\n%s\n' %
       
   106                           (lang, exc.cmd, repr(exc.data[0]), repr(exc.data[1])))
       
   107         except Exception as exc:
       
   108             errors.append(u'while handling language %s: %s' % (lang, exc))
   107         try:
   109         try:
   108             # clean everything
   110             # clean everything
   109             os.unlink(mergedpo)
   111             os.unlink(mergedpo)
   110         except Exception:
   112         except Exception:
   111             continue
   113             continue