author | Sylvain Thénault <sylvain.thenault@logilab.fr> |
Wed, 28 Apr 2010 10:06:01 +0200 | |
branch | stable |
changeset 5421 | 8167de96c523 |
parent 4719 | aaed3f813ef8 |
child 5424 | 8ecbcbff9777 |
permissions | -rw-r--r-- |
5421
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4719
diff
changeset
|
1 |
# copyright 2003-2010 LOGILAB S.A. (Paris, FRANCE), all rights reserved. |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4719
diff
changeset
|
2 |
# contact http://www.logilab.fr/ -- mailto:contact@logilab.fr |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4719
diff
changeset
|
3 |
# |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4719
diff
changeset
|
4 |
# This file is part of CubicWeb. |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4719
diff
changeset
|
5 |
# |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4719
diff
changeset
|
6 |
# CubicWeb is free software: you can redistribute it and/or modify it under the |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4719
diff
changeset
|
7 |
# terms of the GNU Lesser General Public License as published by the Free |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4719
diff
changeset
|
8 |
# Software Foundation, either version 2.1 of the License, or (at your option) |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4719
diff
changeset
|
9 |
# any later version. |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4719
diff
changeset
|
10 |
# |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4719
diff
changeset
|
11 |
# logilab-common is distributed in the hope that it will be useful, but WITHOUT |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4719
diff
changeset
|
12 |
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4719
diff
changeset
|
13 |
# FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4719
diff
changeset
|
14 |
# details. |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4719
diff
changeset
|
15 |
# |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4719
diff
changeset
|
16 |
# You should have received a copy of the GNU Lesser General Public License along |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4719
diff
changeset
|
17 |
# with CubicWeb. If not, see <http://www.gnu.org/licenses/>. |
0 | 18 |
"""Some i18n/gettext utilities. |
19 |
||
20 |
""" |
|
21 |
__docformat__ = "restructuredtext en" |
|
22 |
||
23 |
import re |
|
24 |
import os |
|
1132 | 25 |
from os.path import join, basename, splitext, exists |
0 | 26 |
from glob import glob |
27 |
||
28 |
from cubicweb.toolsutils import create_dir |
|
29 |
||
30 |
def extract_from_tal(files, output_file): |
|
31 |
"""extract i18n strings from tal and write them into the given output file |
|
32 |
using standard python gettext marker (_) |
|
33 |
""" |
|
34 |
output = open(output_file, 'w') |
|
35 |
for filepath in files: |
|
36 |
for match in re.finditer('i18n:(content|replace)="([^"]+)"', open(filepath).read()): |
|
37 |
print >> output, '_("%s")' % match.group(2) |
|
38 |
output.close() |
|
39 |
||
40 |
||
3275
5247789df541
[gettext] provide GNU contexts to avoid translations ambiguities
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3202
diff
changeset
|
41 |
def add_msg(w, msgid, msgctx=None): |
0 | 42 |
"""write an empty pot msgid definition""" |
43 |
if isinstance(msgid, unicode): |
|
44 |
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
|
45 |
if msgctx: |
5247789df541
[gettext] provide GNU contexts to avoid translations ambiguities
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3202
diff
changeset
|
46 |
if isinstance(msgctx, unicode): |
5247789df541
[gettext] provide GNU contexts to avoid translations ambiguities
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3202
diff
changeset
|
47 |
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
|
48 |
w('msgctxt "%s"\n' % msgctx) |
0 | 49 |
msgid = msgid.replace('"', r'\"').splitlines() |
50 |
if len(msgid) > 1: |
|
51 |
w('msgid ""\n') |
|
52 |
for line in msgid: |
|
53 |
w('"%s"' % line.replace('"', r'\"')) |
|
54 |
else: |
|
55 |
w('msgid "%s"\n' % msgid[0]) |
|
56 |
w('msgstr ""\n\n') |
|
57 |
||
58 |
||
59 |
def execute(cmd): |
|
60 |
"""display the command, execute it and raise an Exception if returned |
|
61 |
status != 0 |
|
62 |
""" |
|
3230 | 63 |
from subprocess import call |
0 | 64 |
print cmd.replace(os.getcwd() + os.sep, '') |
3202 | 65 |
status = call(cmd, shell=True) |
0 | 66 |
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
|
67 |
raise Exception('status = %s' % status) |
0 | 68 |
|
69 |
||
70 |
def available_catalogs(i18ndir=None): |
|
71 |
if i18ndir is None: |
|
72 |
wildcard = '*.po' |
|
73 |
else: |
|
74 |
wildcard = join(i18ndir, '*.po') |
|
75 |
for popath in glob(wildcard): |
|
76 |
lang = splitext(basename(popath))[0] |
|
77 |
yield lang, popath |
|
78 |
||
79 |
||
80 |
def compile_i18n_catalogs(sourcedirs, destdir, langs): |
|
81 |
"""generate .mo files for a set of languages into the `destdir` i18n directory |
|
82 |
""" |
|
83 |
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
|
84 |
print '-> compiling %s catalogs...' % destdir |
0 | 85 |
errors = [] |
86 |
for lang in langs: |
|
87 |
langdir = join(destdir, lang, 'LC_MESSAGES') |
|
88 |
if not exists(langdir): |
|
89 |
create_dir(langdir) |
|
90 |
pofiles = [join(path, '%s.po' % lang) for path in sourcedirs] |
|
91 |
pofiles = [pof for pof in pofiles if exists(pof)] |
|
92 |
mergedpo = join(destdir, '%s_merged.po' % lang) |
|
93 |
try: |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2395
diff
changeset
|
94 |
# merge instance/cubes messages catalogs with the stdlib's one |
3118 | 95 |
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
|
96 |
% (mergedpo, ' '.join('"%s"' % f for f in pofiles))) |
3118 | 97 |
# make sure the .mo file is writeable and compiles with *msgfmt* |
0 | 98 |
applmo = join(destdir, lang, 'LC_MESSAGES', 'cubicweb.mo') |
99 |
try: |
|
100 |
ensure_fs_mode(applmo) |
|
101 |
except OSError: |
|
102 |
pass # suppose not exists |
|
3118 | 103 |
execute('msgfmt "%s" -o "%s"' % (mergedpo, applmo)) |
0 | 104 |
except Exception, ex: |
105 |
errors.append('while handling language %s: %s' % (lang, ex)) |
|
106 |
try: |
|
107 |
# clean everything |
|
108 |
os.unlink(mergedpo) |
|
109 |
except Exception: |
|
110 |
continue |
|
111 |
return errors |