author | Aurélien Campéas |
Mon, 07 Sep 2009 17:46:28 +0200 | |
branch | 3.5 |
changeset 3117 | 32686ae66c75 |
parent 2476 | 1294a6bdf3bf |
child 3118 | 9e7a155bc4e5 |
permissions | -rw-r--r-- |
0 | 1 |
"""Some i18n/gettext utilities. |
2 |
||
3 |
:organization: Logilab |
|
1977
606923dff11b
big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1802
diff
changeset
|
4 |
:copyright: 2001-2009 LOGILAB S.A. (Paris, FRANCE), license is LGPL v2. |
0 | 5 |
:contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr |
1977
606923dff11b
big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1802
diff
changeset
|
6 |
:license: GNU Lesser General Public License, v2.1 - http://www.gnu.org/licenses |
0 | 7 |
""" |
8 |
__docformat__ = "restructuredtext en" |
|
9 |
||
10 |
import re |
|
11 |
import os |
|
1132 | 12 |
from os.path import join, basename, splitext, exists |
0 | 13 |
from glob import glob |
14 |
||
15 |
from cubicweb.toolsutils import create_dir |
|
16 |
||
17 |
def extract_from_tal(files, output_file): |
|
18 |
"""extract i18n strings from tal and write them into the given output file |
|
19 |
using standard python gettext marker (_) |
|
20 |
""" |
|
21 |
output = open(output_file, 'w') |
|
22 |
for filepath in files: |
|
23 |
for match in re.finditer('i18n:(content|replace)="([^"]+)"', open(filepath).read()): |
|
24 |
print >> output, '_("%s")' % match.group(2) |
|
25 |
output.close() |
|
26 |
||
27 |
||
28 |
def add_msg(w, msgid): |
|
29 |
"""write an empty pot msgid definition""" |
|
30 |
if isinstance(msgid, unicode): |
|
31 |
msgid = msgid.encode('utf-8') |
|
32 |
msgid = msgid.replace('"', r'\"').splitlines() |
|
33 |
if len(msgid) > 1: |
|
34 |
w('msgid ""\n') |
|
35 |
for line in msgid: |
|
36 |
w('"%s"' % line.replace('"', r'\"')) |
|
37 |
else: |
|
38 |
w('msgid "%s"\n' % msgid[0]) |
|
39 |
w('msgstr ""\n\n') |
|
40 |
||
41 |
||
42 |
def execute(cmd): |
|
43 |
"""display the command, execute it and raise an Exception if returned |
|
44 |
status != 0 |
|
45 |
""" |
|
46 |
print cmd.replace(os.getcwd() + os.sep, '') |
|
3117
32686ae66c75
mostly adapt the i18n subsystem to limitation wrt redirection handinling in windows, using the -o argument of the utilities
Aurélien Campéas
parents:
2476
diff
changeset
|
47 |
from subprocess import call |
32686ae66c75
mostly adapt the i18n subsystem to limitation wrt redirection handinling in windows, using the -o argument of the utilities
Aurélien Campéas
parents:
2476
diff
changeset
|
48 |
status = call(cmd) |
0 | 49 |
if status != 0: |
3117
32686ae66c75
mostly adapt the i18n subsystem to limitation wrt redirection handinling in windows, using the -o argument of the utilities
Aurélien Campéas
parents:
2476
diff
changeset
|
50 |
raise Exception('status = %s' % status) |
0 | 51 |
|
52 |
||
53 |
def available_catalogs(i18ndir=None): |
|
54 |
if i18ndir is None: |
|
55 |
wildcard = '*.po' |
|
56 |
else: |
|
57 |
wildcard = join(i18ndir, '*.po') |
|
58 |
for popath in glob(wildcard): |
|
59 |
lang = splitext(basename(popath))[0] |
|
60 |
yield lang, popath |
|
61 |
||
62 |
||
63 |
def compile_i18n_catalogs(sourcedirs, destdir, langs): |
|
64 |
"""generate .mo files for a set of languages into the `destdir` i18n directory |
|
65 |
""" |
|
66 |
from logilab.common.fileutils import ensure_fs_mode |
|
2395
e3093fc12a00
[cw-ctl] improve dialog messages
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
1977
diff
changeset
|
67 |
print '-> compiling %s catalogs...' % destdir |
0 | 68 |
errors = [] |
69 |
for lang in langs: |
|
70 |
langdir = join(destdir, lang, 'LC_MESSAGES') |
|
71 |
if not exists(langdir): |
|
72 |
create_dir(langdir) |
|
73 |
pofiles = [join(path, '%s.po' % lang) for path in sourcedirs] |
|
74 |
pofiles = [pof for pof in pofiles if exists(pof)] |
|
75 |
mergedpo = join(destdir, '%s_merged.po' % lang) |
|
76 |
try: |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2395
diff
changeset
|
77 |
# merge instance/cubes messages catalogs with the stdlib's one |
3117
32686ae66c75
mostly adapt the i18n subsystem to limitation wrt redirection handinling in windows, using the -o argument of the utilities
Aurélien Campéas
parents:
2476
diff
changeset
|
78 |
execute('msgcat --use-first --sort-output --strict -o %s %s' |
32686ae66c75
mostly adapt the i18n subsystem to limitation wrt redirection handinling in windows, using the -o argument of the utilities
Aurélien Campéas
parents:
2476
diff
changeset
|
79 |
% (mergedpo, ' '.join('"%s"' % f for f in pofiles))) |
0 | 80 |
# make sure the .mo file is writeable and compile with *msgfmt* |
81 |
applmo = join(destdir, lang, 'LC_MESSAGES', 'cubicweb.mo') |
|
82 |
try: |
|
83 |
ensure_fs_mode(applmo) |
|
84 |
except OSError: |
|
85 |
pass # suppose not exists |
|
86 |
execute('msgfmt %s -o %s' % (mergedpo, applmo)) |
|
87 |
except Exception, ex: |
|
88 |
errors.append('while handling language %s: %s' % (lang, ex)) |
|
89 |
try: |
|
90 |
# clean everything |
|
91 |
os.unlink(mergedpo) |
|
92 |
except Exception: |
|
93 |
continue |
|
94 |
return errors |