common/i18n.py
changeset 0 b97547f5f1fa
child 1132 96752791c2b6
equal deleted inserted replaced
-1:000000000000 0:b97547f5f1fa
       
     1 """Some i18n/gettext utilities.
       
     2 
       
     3 :organization: Logilab
       
     4 :copyright: 2001-2008 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
       
     5 :contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr
       
     6 """
       
     7 __docformat__ = "restructuredtext en"
       
     8 
       
     9 import re
       
    10 import os
       
    11 from os.path import join, abspath, basename, splitext, exists
       
    12 from glob import glob
       
    13 
       
    14 from cubicweb.toolsutils import create_dir
       
    15 
       
    16 def extract_from_tal(files, output_file):
       
    17     """extract i18n strings from tal and write them into the given output file
       
    18     using standard python gettext marker (_)
       
    19     """
       
    20     output = open(output_file, 'w')
       
    21     for filepath in files:
       
    22         for match in re.finditer('i18n:(content|replace)="([^"]+)"', open(filepath).read()):
       
    23             print >> output, '_("%s")' % match.group(2)
       
    24     output.close()
       
    25 
       
    26 
       
    27 def add_msg(w, msgid):
       
    28     """write an empty pot msgid definition"""
       
    29     if isinstance(msgid, unicode):
       
    30         msgid = msgid.encode('utf-8')
       
    31     msgid = msgid.replace('"', r'\"').splitlines()
       
    32     if len(msgid) > 1:
       
    33         w('msgid ""\n')
       
    34         for line in msgid:
       
    35             w('"%s"' % line.replace('"', r'\"'))
       
    36     else:
       
    37         w('msgid "%s"\n' % msgid[0])
       
    38     w('msgstr ""\n\n')
       
    39 
       
    40 
       
    41 def execute(cmd):
       
    42     """display the command, execute it and raise an Exception if returned
       
    43     status != 0
       
    44     """
       
    45     print cmd.replace(os.getcwd() + os.sep, '')
       
    46     status = os.system(cmd)
       
    47     if status != 0:
       
    48         raise Exception()
       
    49 
       
    50 
       
    51 def available_catalogs(i18ndir=None):
       
    52     if i18ndir is None:
       
    53         wildcard = '*.po'
       
    54     else:
       
    55         wildcard = join(i18ndir, '*.po')
       
    56     for popath in glob(wildcard):
       
    57         lang = splitext(basename(popath))[0]
       
    58         yield lang, popath
       
    59 
       
    60 
       
    61 def compile_i18n_catalogs(sourcedirs, destdir, langs):
       
    62     """generate .mo files for a set of languages into the `destdir` i18n directory
       
    63     """
       
    64     from logilab.common.fileutils import ensure_fs_mode
       
    65     print 'compiling %s catalogs...' % destdir
       
    66     errors = []
       
    67     for lang in langs:
       
    68         langdir = join(destdir, lang, 'LC_MESSAGES')
       
    69         if not exists(langdir):
       
    70             create_dir(langdir)
       
    71         pofiles = [join(path, '%s.po' % lang) for path in sourcedirs]
       
    72         pofiles = [pof for pof in pofiles if exists(pof)]
       
    73         mergedpo = join(destdir, '%s_merged.po' % lang)
       
    74         try:
       
    75             # merge application messages' catalog with the stdlib's one
       
    76             execute('msgcat --use-first --sort-output --strict %s > %s'
       
    77                     % (' '.join(pofiles), mergedpo))
       
    78             # make sure the .mo file is writeable and compile with *msgfmt*
       
    79             applmo = join(destdir, lang, 'LC_MESSAGES', 'cubicweb.mo')
       
    80             try:
       
    81                 ensure_fs_mode(applmo)
       
    82             except OSError:
       
    83                 pass # suppose not exists
       
    84             execute('msgfmt %s -o %s' % (mergedpo, applmo))
       
    85         except Exception, ex:
       
    86             errors.append('while handling language %s: %s' % (lang, ex))
       
    87         try:
       
    88             # clean everything
       
    89             os.unlink(mergedpo)
       
    90         except Exception:
       
    91             continue
       
    92     return errors
       
    93