author | Nicolas Chauvat <nicolas.chauvat@logilab.fr> |
Wed, 28 Nov 2012 11:44:15 +0100 | |
branch | stable |
changeset 8604 | 7bacc4f21edc |
parent 8225 | baf8c16cae95 |
child 8620 | 61c4bdd70dd8 |
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 |
# |
5424
8ecbcbff9777
replace logilab-common by CubicWeb in disclaimer
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5421
diff
changeset
|
11 |
# CubicWeb is distributed in the hope that it will be useful, but WITHOUT |
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
|
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/>. |
6356
e89f71a41e20
[c-c i18n] namespace/line wrap cleanup + dynamically compute available language + make it works in installed mode
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5424
diff
changeset
|
18 |
"""Some i18n/gettext utilities.""" |
0 | 19 |
|
20 |
__docformat__ = "restructuredtext en" |
|
21 |
||
22 |
import re |
|
23 |
import os |
|
1132 | 24 |
from os.path import join, basename, splitext, exists |
0 | 25 |
from glob import glob |
26 |
||
27 |
from cubicweb.toolsutils import create_dir |
|
28 |
||
29 |
def extract_from_tal(files, output_file): |
|
30 |
"""extract i18n strings from tal and write them into the given output file |
|
31 |
using standard python gettext marker (_) |
|
32 |
""" |
|
33 |
output = open(output_file, 'w') |
|
34 |
for filepath in files: |
|
35 |
for match in re.finditer('i18n:(content|replace)="([^"]+)"', open(filepath).read()): |
|
7896
4c954e1e73ef
[lint] remove uses of "print >> sys.stderr" (closes #1908571)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
7155
diff
changeset
|
36 |
output.write('_("%s")' % match.group(2)) |
0 | 37 |
output.close() |
38 |
||
39 |
||
3275
5247789df541
[gettext] provide GNU contexts to avoid translations ambiguities
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3202
diff
changeset
|
40 |
def add_msg(w, msgid, msgctx=None): |
0 | 41 |
"""write an empty pot msgid definition""" |
42 |
if isinstance(msgid, unicode): |
|
43 |
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
|
44 |
if msgctx: |
5247789df541
[gettext] provide GNU contexts to avoid translations ambiguities
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3202
diff
changeset
|
45 |
if isinstance(msgctx, unicode): |
5247789df541
[gettext] provide GNU contexts to avoid translations ambiguities
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3202
diff
changeset
|
46 |
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
|
47 |
w('msgctxt "%s"\n' % msgctx) |
0 | 48 |
msgid = msgid.replace('"', r'\"').splitlines() |
49 |
if len(msgid) > 1: |
|
50 |
w('msgid ""\n') |
|
51 |
for line in msgid: |
|
52 |
w('"%s"' % line.replace('"', r'\"')) |
|
53 |
else: |
|
54 |
w('msgid "%s"\n' % msgid[0]) |
|
55 |
w('msgstr ""\n\n') |
|
56 |
||
57 |
||
58 |
def execute(cmd): |
|
59 |
"""display the command, execute it and raise an Exception if returned |
|
60 |
status != 0 |
|
61 |
""" |
|
3230 | 62 |
from subprocess import call |
8225
baf8c16cae95
[i18n] fix pb with non-ascii chars in cwd. closes #2149063
Florent Cayré <florent.cayre@gmail.com>
parents:
7896
diff
changeset
|
63 |
# use getcwdu as cmd may be unicode and cwd may contains non-ascii |
baf8c16cae95
[i18n] fix pb with non-ascii chars in cwd. closes #2149063
Florent Cayré <florent.cayre@gmail.com>
parents:
7896
diff
changeset
|
64 |
# characters |
baf8c16cae95
[i18n] fix pb with non-ascii chars in cwd. closes #2149063
Florent Cayré <florent.cayre@gmail.com>
parents:
7896
diff
changeset
|
65 |
print cmd.replace(os.getcwdu() + os.sep, '') |
3202 | 66 |
status = call(cmd, shell=True) |
0 | 67 |
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
|
68 |
raise Exception('status = %s' % status) |
0 | 69 |
|
70 |
||
71 |
def available_catalogs(i18ndir=None): |
|
72 |
if i18ndir is None: |
|
73 |
wildcard = '*.po' |
|
74 |
else: |
|
75 |
wildcard = join(i18ndir, '*.po') |
|
76 |
for popath in glob(wildcard): |
|
77 |
lang = splitext(basename(popath))[0] |
|
78 |
yield lang, popath |
|
79 |
||
80 |
||
81 |
def compile_i18n_catalogs(sourcedirs, destdir, langs): |
|
82 |
"""generate .mo files for a set of languages into the `destdir` i18n directory |
|
83 |
""" |
|
84 |
from logilab.common.fileutils import ensure_fs_mode |
|
7155
4bab50b02927
[cwctl] refactor ui messages aiming for consistency and simplicity
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
6356
diff
changeset
|
85 |
print '-> compiling message catalogs to %s' % destdir |
0 | 86 |
errors = [] |
87 |
for lang in langs: |
|
88 |
langdir = join(destdir, lang, 'LC_MESSAGES') |
|
89 |
if not exists(langdir): |
|
90 |
create_dir(langdir) |
|
91 |
pofiles = [join(path, '%s.po' % lang) for path in sourcedirs] |
|
92 |
pofiles = [pof for pof in pofiles if exists(pof)] |
|
93 |
mergedpo = join(destdir, '%s_merged.po' % lang) |
|
94 |
try: |
|
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2395
diff
changeset
|
95 |
# merge instance/cubes messages catalogs with the stdlib's one |
3118 | 96 |
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
|
97 |
% (mergedpo, ' '.join('"%s"' % f for f in pofiles))) |
3118 | 98 |
# make sure the .mo file is writeable and compiles with *msgfmt* |
0 | 99 |
applmo = join(destdir, lang, 'LC_MESSAGES', 'cubicweb.mo') |
100 |
try: |
|
101 |
ensure_fs_mode(applmo) |
|
102 |
except OSError: |
|
103 |
pass # suppose not exists |
|
3118 | 104 |
execute('msgfmt "%s" -o "%s"' % (mergedpo, applmo)) |
0 | 105 |
except Exception, ex: |
106 |
errors.append('while handling language %s: %s' % (lang, ex)) |
|
107 |
try: |
|
108 |
# clean everything |
|
109 |
os.unlink(mergedpo) |
|
110 |
except Exception: |
|
111 |
continue |
|
112 |
return errors |