author | Adrien Di Mascio <Adrien.DiMascio@logilab.fr> |
Tue, 22 Sep 2009 12:11:12 +0200 | |
changeset 3369 | 7b88d12b4ee2 |
parent 3293 | 69c0ba095536 |
parent 3316 | c4c07aab1c39 |
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 |
|
3156
cd56d5c379cb
very dubious but observed : os.system works on linux, subprocess.call on windows, no other way around ?
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
3118
diff
changeset
|
12 |
import sys |
1132 | 13 |
from os.path import join, basename, splitext, exists |
0 | 14 |
from glob import glob |
15 |
||
16 |
from cubicweb.toolsutils import create_dir |
|
17 |
||
18 |
def extract_from_tal(files, output_file): |
|
19 |
"""extract i18n strings from tal and write them into the given output file |
|
20 |
using standard python gettext marker (_) |
|
21 |
""" |
|
22 |
output = open(output_file, 'w') |
|
23 |
for filepath in files: |
|
24 |
for match in re.finditer('i18n:(content|replace)="([^"]+)"', open(filepath).read()): |
|
25 |
print >> output, '_("%s")' % match.group(2) |
|
26 |
output.close() |
|
27 |
||
28 |
||
3275
5247789df541
[gettext] provide GNU contexts to avoid translations ambiguities
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3202
diff
changeset
|
29 |
def add_msg(w, msgid, msgctx=None): |
0 | 30 |
"""write an empty pot msgid definition""" |
31 |
if isinstance(msgid, unicode): |
|
32 |
msgid = msgid.encode('utf-8') |
|
3275
5247789df541
[gettext] provide GNU contexts to avoid translations ambiguities
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3202
diff
changeset
|
33 |
if msgctx: |
5247789df541
[gettext] provide GNU contexts to avoid translations ambiguities
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3202
diff
changeset
|
34 |
if isinstance(msgctx, unicode): |
5247789df541
[gettext] provide GNU contexts to avoid translations ambiguities
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3202
diff
changeset
|
35 |
msgctx = msgctx.encode('utf-8') |
5247789df541
[gettext] provide GNU contexts to avoid translations ambiguities
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3202
diff
changeset
|
36 |
w('msgctxt "%s"\n' % msgctx) |
0 | 37 |
msgid = msgid.replace('"', r'\"').splitlines() |
38 |
if len(msgid) > 1: |
|
39 |
w('msgid ""\n') |
|
40 |
for line in msgid: |
|
41 |
w('"%s"' % line.replace('"', r'\"')) |
|
42 |
else: |
|
43 |
w('msgid "%s"\n' % msgid[0]) |
|
44 |
w('msgstr ""\n\n') |
|
45 |
||
46 |
||
47 |
def execute(cmd): |
|
48 |
"""display the command, execute it and raise an Exception if returned |
|
49 |
status != 0 |
|
50 |
""" |
|
3230 | 51 |
from subprocess import call |
0 | 52 |
print cmd.replace(os.getcwd() + os.sep, '') |
3202 | 53 |
status = call(cmd, shell=True) |
0 | 54 |
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
|
55 |
raise Exception('status = %s' % status) |
0 | 56 |
|
57 |
||
58 |
def available_catalogs(i18ndir=None): |
|
59 |
if i18ndir is None: |
|
60 |
wildcard = '*.po' |
|
61 |
else: |
|
62 |
wildcard = join(i18ndir, '*.po') |
|
63 |
for popath in glob(wildcard): |
|
64 |
lang = splitext(basename(popath))[0] |
|
65 |
yield lang, popath |
|
66 |
||
67 |
||
68 |
def compile_i18n_catalogs(sourcedirs, destdir, langs): |
|
69 |
"""generate .mo files for a set of languages into the `destdir` i18n directory |
|
70 |
""" |
|
71 |
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
|
72 |
print '-> compiling %s catalogs...' % destdir |
0 | 73 |
errors = [] |
74 |
for lang in langs: |
|
75 |
langdir = join(destdir, lang, 'LC_MESSAGES') |
|
76 |
if not exists(langdir): |
|
77 |
create_dir(langdir) |
|
78 |
pofiles = [join(path, '%s.po' % lang) for path in sourcedirs] |
|
79 |
pofiles = [pof for pof in pofiles if exists(pof)] |
|
80 |
mergedpo = join(destdir, '%s_merged.po' % lang) |
|
81 |
try: |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2395
diff
changeset
|
82 |
# merge instance/cubes messages catalogs with the stdlib's one |
3118 | 83 |
execute('msgcat --use-first --sort-output --strict -o "%s" %s' |
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
|
84 |
% (mergedpo, ' '.join('"%s"' % f for f in pofiles))) |
3118 | 85 |
# make sure the .mo file is writeable and compiles with *msgfmt* |
0 | 86 |
applmo = join(destdir, lang, 'LC_MESSAGES', 'cubicweb.mo') |
87 |
try: |
|
88 |
ensure_fs_mode(applmo) |
|
89 |
except OSError: |
|
90 |
pass # suppose not exists |
|
3118 | 91 |
execute('msgfmt "%s" -o "%s"' % (mergedpo, applmo)) |
0 | 92 |
except Exception, ex: |
93 |
errors.append('while handling language %s: %s' % (lang, ex)) |
|
94 |
try: |
|
95 |
# clean everything |
|
96 |
os.unlink(mergedpo) |
|
97 |
except Exception: |
|
98 |
continue |
|
99 |
return errors |