common/i18n.py
branchstable
changeset 3315 59220b704562
parent 3275 5247789df541
child 3293 69c0ba095536
child 3316 c4c07aab1c39
equal deleted inserted replaced
3298:caef98aa4a98 3315:59220b704562
    23         for match in re.finditer('i18n:(content|replace)="([^"]+)"', open(filepath).read()):
    23         for match in re.finditer('i18n:(content|replace)="([^"]+)"', open(filepath).read()):
    24             print >> output, '_("%s")' % match.group(2)
    24             print >> output, '_("%s")' % match.group(2)
    25     output.close()
    25     output.close()
    26 
    26 
    27 
    27 
    28 def add_msg(w, msgid):
    28 def add_msg(w, msgid, msgctx=None):
    29     """write an empty pot msgid definition"""
    29     """write an empty pot msgid definition"""
    30     if isinstance(msgid, unicode):
    30     if isinstance(msgid, unicode):
    31         msgid = msgid.encode('utf-8')
    31         msgid = msgid.encode('utf-8')
       
    32     if msgctx:
       
    33         if isinstance(msgctx, unicode):
       
    34             msgctx = msgctx.encode('utf-8')
       
    35         w('msgctxt "%s"\n' % msgctx)
    32     msgid = msgid.replace('"', r'\"').splitlines()
    36     msgid = msgid.replace('"', r'\"').splitlines()
    33     if len(msgid) > 1:
    37     if len(msgid) > 1:
    34         w('msgid ""\n')
    38         w('msgid ""\n')
    35         for line in msgid:
    39         for line in msgid:
    36             w('"%s"' % line.replace('"', r'\"'))
    40             w('"%s"' % line.replace('"', r'\"'))
    42 def execute(cmd):
    46 def execute(cmd):
    43     """display the command, execute it and raise an Exception if returned
    47     """display the command, execute it and raise an Exception if returned
    44     status != 0
    48     status != 0
    45     """
    49     """
    46     print cmd.replace(os.getcwd() + os.sep, '')
    50     print cmd.replace(os.getcwd() + os.sep, '')
    47     status = os.system(cmd)
    51     from subprocess import call
       
    52     status = call(cmd, shell=True)
    48     if status != 0:
    53     if status != 0:
    49         raise Exception()
    54         raise Exception('status = %s' % status)
    50 
    55 
    51 
    56 
    52 def available_catalogs(i18ndir=None):
    57 def available_catalogs(i18ndir=None):
    53     if i18ndir is None:
    58     if i18ndir is None:
    54         wildcard = '*.po'
    59         wildcard = '*.po'
    72         pofiles = [join(path, '%s.po' % lang) for path in sourcedirs]
    77         pofiles = [join(path, '%s.po' % lang) for path in sourcedirs]
    73         pofiles = [pof for pof in pofiles if exists(pof)]
    78         pofiles = [pof for pof in pofiles if exists(pof)]
    74         mergedpo = join(destdir, '%s_merged.po' % lang)
    79         mergedpo = join(destdir, '%s_merged.po' % lang)
    75         try:
    80         try:
    76             # merge instance/cubes messages catalogs with the stdlib's one
    81             # merge instance/cubes messages catalogs with the stdlib's one
    77             execute('msgcat --use-first --sort-output --strict %s > %s'
    82             execute('msgcat --use-first --sort-output --strict -o "%s" %s'
    78                     % (' '.join(pofiles), mergedpo))
    83                     % (mergedpo, ' '.join('"%s"' % f for f in pofiles)))
    79             # make sure the .mo file is writeable and compile with *msgfmt*
    84             # make sure the .mo file is writeable and compiles with *msgfmt*
    80             applmo = join(destdir, lang, 'LC_MESSAGES', 'cubicweb.mo')
    85             applmo = join(destdir, lang, 'LC_MESSAGES', 'cubicweb.mo')
    81             try:
    86             try:
    82                 ensure_fs_mode(applmo)
    87                 ensure_fs_mode(applmo)
    83             except OSError:
    88             except OSError:
    84                 pass # suppose not exists
    89                 pass # suppose not exists
    85             execute('msgfmt %s -o %s' % (mergedpo, applmo))
    90             execute('msgfmt "%s" -o "%s"' % (mergedpo, applmo))
    86         except Exception, ex:
    91         except Exception, ex:
    87             errors.append('while handling language %s: %s' % (lang, ex))
    92             errors.append('while handling language %s: %s' % (lang, ex))
    88         try:
    93         try:
    89             # clean everything
    94             # clean everything
    90             os.unlink(mergedpo)
    95             os.unlink(mergedpo)