author | Sylvain Thénault <sylvain.thenault@logilab.fr> |
Tue, 12 Oct 2010 19:10:08 +0200 | |
changeset 6456 | 150246e77cda |
parent 5424 | 8ecbcbff9777 |
child 10590 | 7629902e7554 |
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:
3279
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:
3279
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:
3279
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:
3279
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:
3279
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:
3279
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:
3279
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:
3279
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:
3279
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:
3279
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:
3279
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:
3279
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:
3279
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:
3279
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:
3279
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:
3279
diff
changeset
|
17 |
# with CubicWeb. If not, see <http://www.gnu.org/licenses/>. |
0 | 18 |
"""Internationalization and localization support. |
19 |
||
20 |
This module provides internationalization (I18N) and localization (L10N) |
|
21 |
support for your Python programs by providing an interface to the GNU gettext |
|
22 |
message catalog library. |
|
23 |
||
24 |
I18N refers to the operation by which a program is made aware of multiple |
|
25 |
languages. L10N refers to the adaptation of your program, once |
|
26 |
internationalized, to the local language and cultural habits. |
|
27 |
||
28 |
""" |
|
29 |
||
30 |
# This module represents the integration of work, contributions, feedback, and |
|
31 |
# suggestions from the following people: |
|
32 |
# |
|
33 |
# Martin von Loewis, who wrote the initial implementation of the underlying |
|
34 |
# C-based libintlmodule (later renamed _gettext), along with a skeletal |
|
35 |
# gettext.py implementation. |
|
36 |
# |
|
37 |
# Peter Funk, who wrote fintl.py, a fairly complete wrapper around intlmodule, |
|
38 |
# which also included a pure-Python implementation to read .mo files if |
|
39 |
# intlmodule wasn't available. |
|
40 |
# |
|
41 |
# James Henstridge, who also wrote a gettext.py module, which has some |
|
42 |
# interesting, but currently unsupported experimental features: the notion of |
|
43 |
# a Catalog class and instances, and the ability to add to a catalog file via |
|
44 |
# a Python API. |
|
45 |
# |
|
46 |
# Barry Warsaw integrated these modules, wrote the .install() API and code, |
|
47 |
# and conformed all C and Python code to Python's coding standards. |
|
48 |
# |
|
49 |
# Francois Pinard and Marc-Andre Lemburg also contributed valuably to this |
|
50 |
# module. |
|
51 |
# |
|
52 |
# J. David Ibanez implemented plural forms. Bruno Haible fixed some bugs. |
|
53 |
# |
|
54 |
# TODO: |
|
55 |
# - Lazy loading of .mo files. Currently the entire catalog is loaded into |
|
56 |
# memory, but that's probably bad for large translated programs. Instead, |
|
57 |
# the lexical sort of original strings in GNU .mo files should be exploited |
|
58 |
# to do binary searches and lazy initializations. Or you might want to use |
|
59 |
# the undocumented double-hash algorithm for .mo files with hash tables, but |
|
60 |
# you'll need to study the GNU gettext code to do this. |
|
61 |
# |
|
62 |
# - Support Solaris .mo file formats. Unfortunately, we've been unable to |
|
63 |
# find this format documented anywhere. |
|
64 |
||
65 |
||
3274
7d53d8491932
gettext] provide a more up-to-date version of gettext.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
66 |
import locale, copy, os, re, struct, sys |
0 | 67 |
from errno import ENOENT |
68 |
||
69 |
||
70 |
__all__ = ['NullTranslations', 'GNUTranslations', 'Catalog', |
|
71 |
'find', 'translation', 'install', 'textdomain', 'bindtextdomain', |
|
72 |
'dgettext', 'dngettext', 'gettext', 'ngettext', |
|
73 |
] |
|
74 |
||
75 |
_default_localedir = os.path.join(sys.prefix, 'share', 'locale') |
|
76 |
||
77 |
||
78 |
def test(condition, true, false): |
|
79 |
""" |
|
80 |
Implements the C expression: |
|
81 |
||
82 |
condition ? true : false |
|
83 |
||
84 |
Required to correctly interpret plural forms. |
|
85 |
""" |
|
86 |
if condition: |
|
87 |
return true |
|
88 |
else: |
|
89 |
return false |
|
90 |
||
91 |
||
92 |
def c2py(plural): |
|
93 |
"""Gets a C expression as used in PO files for plural forms and returns a |
|
94 |
Python lambda function that implements an equivalent expression. |
|
95 |
""" |
|
96 |
# Security check, allow only the "n" identifier |
|
3274
7d53d8491932
gettext] provide a more up-to-date version of gettext.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
97 |
try: |
7d53d8491932
gettext] provide a more up-to-date version of gettext.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
98 |
from cStringIO import StringIO |
7d53d8491932
gettext] provide a more up-to-date version of gettext.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
99 |
except ImportError: |
7d53d8491932
gettext] provide a more up-to-date version of gettext.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
100 |
from StringIO import StringIO |
0 | 101 |
import token, tokenize |
102 |
tokens = tokenize.generate_tokens(StringIO(plural).readline) |
|
103 |
try: |
|
104 |
danger = [x for x in tokens if x[0] == token.NAME and x[1] != 'n'] |
|
105 |
except tokenize.TokenError: |
|
106 |
raise ValueError, \ |
|
107 |
'plural forms expression error, maybe unbalanced parenthesis' |
|
108 |
else: |
|
109 |
if danger: |
|
110 |
raise ValueError, 'plural forms expression could be dangerous' |
|
111 |
||
112 |
# Replace some C operators by their Python equivalents |
|
113 |
plural = plural.replace('&&', ' and ') |
|
114 |
plural = plural.replace('||', ' or ') |
|
115 |
||
116 |
expr = re.compile(r'\!([^=])') |
|
117 |
plural = expr.sub(' not \\1', plural) |
|
118 |
||
119 |
# Regular expression and replacement function used to transform |
|
120 |
# "a?b:c" to "test(a,b,c)". |
|
121 |
expr = re.compile(r'(.*?)\?(.*?):(.*)') |
|
122 |
def repl(x): |
|
123 |
return "test(%s, %s, %s)" % (x.group(1), x.group(2), |
|
124 |
expr.sub(repl, x.group(3))) |
|
125 |
||
126 |
# Code to transform the plural expression, taking care of parentheses |
|
127 |
stack = [''] |
|
128 |
for c in plural: |
|
129 |
if c == '(': |
|
130 |
stack.append('') |
|
131 |
elif c == ')': |
|
132 |
if len(stack) == 1: |
|
133 |
# Actually, we never reach this code, because unbalanced |
|
134 |
# parentheses get caught in the security check at the |
|
135 |
# beginning. |
|
136 |
raise ValueError, 'unbalanced parenthesis in plural form' |
|
137 |
s = expr.sub(repl, stack.pop()) |
|
138 |
stack[-1] += '(%s)' % s |
|
139 |
else: |
|
140 |
stack[-1] += c |
|
141 |
plural = expr.sub(repl, stack.pop()) |
|
142 |
||
143 |
return eval('lambda n: int(%s)' % plural) |
|
144 |
||
145 |
||
146 |
||
147 |
def _expand_lang(locale): |
|
148 |
from locale import normalize |
|
149 |
locale = normalize(locale) |
|
150 |
COMPONENT_CODESET = 1 << 0 |
|
151 |
COMPONENT_TERRITORY = 1 << 1 |
|
152 |
COMPONENT_MODIFIER = 1 << 2 |
|
153 |
# split up the locale into its base components |
|
154 |
mask = 0 |
|
155 |
pos = locale.find('@') |
|
156 |
if pos >= 0: |
|
157 |
modifier = locale[pos:] |
|
158 |
locale = locale[:pos] |
|
159 |
mask |= COMPONENT_MODIFIER |
|
160 |
else: |
|
161 |
modifier = '' |
|
162 |
pos = locale.find('.') |
|
163 |
if pos >= 0: |
|
164 |
codeset = locale[pos:] |
|
165 |
locale = locale[:pos] |
|
166 |
mask |= COMPONENT_CODESET |
|
167 |
else: |
|
168 |
codeset = '' |
|
169 |
pos = locale.find('_') |
|
170 |
if pos >= 0: |
|
171 |
territory = locale[pos:] |
|
172 |
locale = locale[:pos] |
|
173 |
mask |= COMPONENT_TERRITORY |
|
174 |
else: |
|
175 |
territory = '' |
|
176 |
language = locale |
|
177 |
ret = [] |
|
178 |
for i in range(mask+1): |
|
179 |
if not (i & ~mask): # if all components for this combo exist ... |
|
180 |
val = language |
|
181 |
if i & COMPONENT_TERRITORY: val += territory |
|
182 |
if i & COMPONENT_CODESET: val += codeset |
|
183 |
if i & COMPONENT_MODIFIER: val += modifier |
|
184 |
ret.append(val) |
|
185 |
ret.reverse() |
|
186 |
return ret |
|
187 |
||
188 |
||
189 |
||
190 |
class NullTranslations: |
|
191 |
def __init__(self, fp=None): |
|
192 |
self._info = {} |
|
193 |
self._charset = None |
|
3274
7d53d8491932
gettext] provide a more up-to-date version of gettext.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
194 |
self._output_charset = None |
0 | 195 |
self._fallback = None |
196 |
if fp is not None: |
|
197 |
self._parse(fp) |
|
198 |
||
199 |
def _parse(self, fp): |
|
200 |
pass |
|
201 |
||
202 |
def add_fallback(self, fallback): |
|
203 |
if self._fallback: |
|
204 |
self._fallback.add_fallback(fallback) |
|
205 |
else: |
|
206 |
self._fallback = fallback |
|
207 |
||
208 |
def gettext(self, message): |
|
209 |
if self._fallback: |
|
210 |
return self._fallback.gettext(message) |
|
211 |
return message |
|
212 |
||
3274
7d53d8491932
gettext] provide a more up-to-date version of gettext.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
213 |
def pgettext(self, context, message): |
7d53d8491932
gettext] provide a more up-to-date version of gettext.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
214 |
if self._fallback: |
7d53d8491932
gettext] provide a more up-to-date version of gettext.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
215 |
return self._fallback.pgettext(context, message) |
7d53d8491932
gettext] provide a more up-to-date version of gettext.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
216 |
return message |
7d53d8491932
gettext] provide a more up-to-date version of gettext.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
217 |
|
7d53d8491932
gettext] provide a more up-to-date version of gettext.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
218 |
def lgettext(self, message): |
7d53d8491932
gettext] provide a more up-to-date version of gettext.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
219 |
if self._fallback: |
7d53d8491932
gettext] provide a more up-to-date version of gettext.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
220 |
return self._fallback.lgettext(message) |
7d53d8491932
gettext] provide a more up-to-date version of gettext.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
221 |
return message |
7d53d8491932
gettext] provide a more up-to-date version of gettext.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
222 |
|
7d53d8491932
gettext] provide a more up-to-date version of gettext.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
223 |
def lpgettext(self, context, message): |
7d53d8491932
gettext] provide a more up-to-date version of gettext.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
224 |
if self._fallback: |
7d53d8491932
gettext] provide a more up-to-date version of gettext.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
225 |
return self._fallback.lpgettext(context, message) |
7d53d8491932
gettext] provide a more up-to-date version of gettext.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
226 |
return message |
7d53d8491932
gettext] provide a more up-to-date version of gettext.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
227 |
|
0 | 228 |
def ngettext(self, msgid1, msgid2, n): |
229 |
if self._fallback: |
|
230 |
return self._fallback.ngettext(msgid1, msgid2, n) |
|
231 |
if n == 1: |
|
232 |
return msgid1 |
|
233 |
else: |
|
234 |
return msgid2 |
|
235 |
||
3274
7d53d8491932
gettext] provide a more up-to-date version of gettext.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
236 |
def npgettext(self, context, msgid1, msgid2, n): |
7d53d8491932
gettext] provide a more up-to-date version of gettext.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
237 |
if self._fallback: |
7d53d8491932
gettext] provide a more up-to-date version of gettext.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
238 |
return self._fallback.npgettext(context, msgid1, msgid2, n) |
7d53d8491932
gettext] provide a more up-to-date version of gettext.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
239 |
if n == 1: |
7d53d8491932
gettext] provide a more up-to-date version of gettext.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
240 |
return msgid1 |
7d53d8491932
gettext] provide a more up-to-date version of gettext.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
241 |
else: |
7d53d8491932
gettext] provide a more up-to-date version of gettext.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
242 |
return msgid2 |
7d53d8491932
gettext] provide a more up-to-date version of gettext.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
243 |
|
7d53d8491932
gettext] provide a more up-to-date version of gettext.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
244 |
def lngettext(self, msgid1, msgid2, n): |
7d53d8491932
gettext] provide a more up-to-date version of gettext.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
245 |
if self._fallback: |
7d53d8491932
gettext] provide a more up-to-date version of gettext.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
246 |
return self._fallback.lngettext(msgid1, msgid2, n) |
7d53d8491932
gettext] provide a more up-to-date version of gettext.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
247 |
if n == 1: |
7d53d8491932
gettext] provide a more up-to-date version of gettext.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
248 |
return msgid1 |
7d53d8491932
gettext] provide a more up-to-date version of gettext.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
249 |
else: |
7d53d8491932
gettext] provide a more up-to-date version of gettext.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
250 |
return msgid2 |
7d53d8491932
gettext] provide a more up-to-date version of gettext.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
251 |
|
7d53d8491932
gettext] provide a more up-to-date version of gettext.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
252 |
def lnpgettext(self, context, msgid1, msgid2, n): |
7d53d8491932
gettext] provide a more up-to-date version of gettext.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
253 |
if self._fallback: |
7d53d8491932
gettext] provide a more up-to-date version of gettext.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
254 |
return self._fallback.lnpgettext(context, msgid1, msgid2, n) |
7d53d8491932
gettext] provide a more up-to-date version of gettext.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
255 |
if n == 1: |
7d53d8491932
gettext] provide a more up-to-date version of gettext.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
256 |
return msgid1 |
7d53d8491932
gettext] provide a more up-to-date version of gettext.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
257 |
else: |
7d53d8491932
gettext] provide a more up-to-date version of gettext.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
258 |
return msgid2 |
7d53d8491932
gettext] provide a more up-to-date version of gettext.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
259 |
|
0 | 260 |
def ugettext(self, message): |
261 |
if self._fallback: |
|
262 |
return self._fallback.ugettext(message) |
|
263 |
return unicode(message) |
|
264 |
||
3274
7d53d8491932
gettext] provide a more up-to-date version of gettext.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
265 |
def upgettext(self, context, message): |
7d53d8491932
gettext] provide a more up-to-date version of gettext.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
266 |
if self._fallback: |
7d53d8491932
gettext] provide a more up-to-date version of gettext.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
267 |
return self._fallback.upgettext(context, message) |
7d53d8491932
gettext] provide a more up-to-date version of gettext.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
268 |
return unicode(message) |
7d53d8491932
gettext] provide a more up-to-date version of gettext.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
269 |
|
0 | 270 |
def ungettext(self, msgid1, msgid2, n): |
271 |
if self._fallback: |
|
272 |
return self._fallback.ungettext(msgid1, msgid2, n) |
|
273 |
if n == 1: |
|
274 |
return unicode(msgid1) |
|
275 |
else: |
|
276 |
return unicode(msgid2) |
|
277 |
||
3274
7d53d8491932
gettext] provide a more up-to-date version of gettext.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
278 |
def unpgettext(self, context, msgid1, msgid2, n): |
7d53d8491932
gettext] provide a more up-to-date version of gettext.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
279 |
if self._fallback: |
7d53d8491932
gettext] provide a more up-to-date version of gettext.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
280 |
return self._fallback.unpgettext(context, msgid1, msgid2, n) |
7d53d8491932
gettext] provide a more up-to-date version of gettext.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
281 |
if n == 1: |
7d53d8491932
gettext] provide a more up-to-date version of gettext.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
282 |
return unicode(msgid1) |
7d53d8491932
gettext] provide a more up-to-date version of gettext.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
283 |
else: |
7d53d8491932
gettext] provide a more up-to-date version of gettext.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
284 |
return unicode(msgid2) |
7d53d8491932
gettext] provide a more up-to-date version of gettext.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
285 |
|
0 | 286 |
def info(self): |
287 |
return self._info |
|
288 |
||
289 |
def charset(self): |
|
290 |
return self._charset |
|
291 |
||
3274
7d53d8491932
gettext] provide a more up-to-date version of gettext.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
292 |
def output_charset(self): |
7d53d8491932
gettext] provide a more up-to-date version of gettext.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
293 |
return self._output_charset |
7d53d8491932
gettext] provide a more up-to-date version of gettext.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
294 |
|
7d53d8491932
gettext] provide a more up-to-date version of gettext.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
295 |
def set_output_charset(self, charset): |
7d53d8491932
gettext] provide a more up-to-date version of gettext.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
296 |
self._output_charset = charset |
7d53d8491932
gettext] provide a more up-to-date version of gettext.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
297 |
|
7d53d8491932
gettext] provide a more up-to-date version of gettext.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
298 |
def install(self, unicode=False, names=None): |
0 | 299 |
import __builtin__ |
300 |
__builtin__.__dict__['_'] = unicode and self.ugettext or self.gettext |
|
3274
7d53d8491932
gettext] provide a more up-to-date version of gettext.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
301 |
if hasattr(names, "__contains__"): |
7d53d8491932
gettext] provide a more up-to-date version of gettext.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
302 |
if "gettext" in names: |
7d53d8491932
gettext] provide a more up-to-date version of gettext.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
303 |
__builtin__.__dict__['gettext'] = __builtin__.__dict__['_'] |
7d53d8491932
gettext] provide a more up-to-date version of gettext.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
304 |
if "pgettext" in names: |
7d53d8491932
gettext] provide a more up-to-date version of gettext.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
305 |
__builtin__.__dict__['pgettext'] = (unicode and self.upgettext |
7d53d8491932
gettext] provide a more up-to-date version of gettext.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
306 |
or self.pgettext) |
7d53d8491932
gettext] provide a more up-to-date version of gettext.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
307 |
if "ngettext" in names: |
7d53d8491932
gettext] provide a more up-to-date version of gettext.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
308 |
__builtin__.__dict__['ngettext'] = (unicode and self.ungettext |
7d53d8491932
gettext] provide a more up-to-date version of gettext.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
309 |
or self.ngettext) |
7d53d8491932
gettext] provide a more up-to-date version of gettext.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
310 |
if "npgettext" in names: |
7d53d8491932
gettext] provide a more up-to-date version of gettext.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
311 |
__builtin__.__dict__['npgettext'] = \ |
7d53d8491932
gettext] provide a more up-to-date version of gettext.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
312 |
(unicode and self.unpgettext or self.npgettext) |
7d53d8491932
gettext] provide a more up-to-date version of gettext.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
313 |
if "lgettext" in names: |
7d53d8491932
gettext] provide a more up-to-date version of gettext.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
314 |
__builtin__.__dict__['lgettext'] = self.lgettext |
7d53d8491932
gettext] provide a more up-to-date version of gettext.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
315 |
if "lpgettext" in names: |
7d53d8491932
gettext] provide a more up-to-date version of gettext.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
316 |
__builtin__.__dict__['lpgettext'] = self.lpgettext |
7d53d8491932
gettext] provide a more up-to-date version of gettext.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
317 |
if "lngettext" in names: |
7d53d8491932
gettext] provide a more up-to-date version of gettext.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
318 |
__builtin__.__dict__['lngettext'] = self.lngettext |
7d53d8491932
gettext] provide a more up-to-date version of gettext.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
319 |
if "lnpgettext" in names: |
7d53d8491932
gettext] provide a more up-to-date version of gettext.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
320 |
__builtin__.__dict__['lnpgettext'] = self.lnpgettext |
0 | 321 |
|
322 |
||
323 |
class GNUTranslations(NullTranslations): |
|
324 |
# Magic number of .mo files |
|
325 |
LE_MAGIC = 0x950412deL |
|
326 |
BE_MAGIC = 0xde120495L |
|
327 |
||
3274
7d53d8491932
gettext] provide a more up-to-date version of gettext.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
328 |
# The encoding of a msgctxt and a msgid in a .mo file is |
7d53d8491932
gettext] provide a more up-to-date version of gettext.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
329 |
# msgctxt + "\x04" + msgid (gettext version >= 0.15) |
7d53d8491932
gettext] provide a more up-to-date version of gettext.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
330 |
CONTEXT_ENCODING = "%s\x04%s" |
7d53d8491932
gettext] provide a more up-to-date version of gettext.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
331 |
|
0 | 332 |
def _parse(self, fp): |
333 |
"""Override this method to support alternative .mo formats.""" |
|
334 |
unpack = struct.unpack |
|
335 |
filename = getattr(fp, 'name', '') |
|
336 |
# Parse the .mo file header, which consists of 5 little endian 32 |
|
337 |
# bit words. |
|
338 |
self._catalog = catalog = {} |
|
339 |
self.plural = lambda n: int(n != 1) # germanic plural by default |
|
340 |
buf = fp.read() |
|
341 |
buflen = len(buf) |
|
342 |
# Are we big endian or little endian? |
|
343 |
magic = unpack('<I', buf[:4])[0] |
|
344 |
if magic == self.LE_MAGIC: |
|
345 |
version, msgcount, masteridx, transidx = unpack('<4I', buf[4:20]) |
|
346 |
ii = '<II' |
|
347 |
elif magic == self.BE_MAGIC: |
|
348 |
version, msgcount, masteridx, transidx = unpack('>4I', buf[4:20]) |
|
349 |
ii = '>II' |
|
350 |
else: |
|
351 |
raise IOError(0, 'Bad magic number', filename) |
|
352 |
# Now put all messages from the .mo file buffer into the catalog |
|
353 |
# dictionary. |
|
354 |
for i in xrange(0, msgcount): |
|
355 |
mlen, moff = unpack(ii, buf[masteridx:masteridx+8]) |
|
356 |
mend = moff + mlen |
|
357 |
tlen, toff = unpack(ii, buf[transidx:transidx+8]) |
|
358 |
tend = toff + tlen |
|
359 |
if mend < buflen and tend < buflen: |
|
360 |
msg = buf[moff:mend] |
|
361 |
tmsg = buf[toff:tend] |
|
362 |
else: |
|
363 |
raise IOError(0, 'File is corrupt', filename) |
|
364 |
# See if we're looking at GNU .mo conventions for metadata |
|
365 |
if mlen == 0: |
|
366 |
# Catalog description |
|
367 |
lastk = k = None |
|
368 |
for item in tmsg.splitlines(): |
|
369 |
item = item.strip() |
|
3274
7d53d8491932
gettext] provide a more up-to-date version of gettext.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
370 |
if not item: |
0 | 371 |
continue |
3274
7d53d8491932
gettext] provide a more up-to-date version of gettext.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
372 |
if ':' in item: |
7d53d8491932
gettext] provide a more up-to-date version of gettext.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
373 |
k, v = item.split(':', 1) |
7d53d8491932
gettext] provide a more up-to-date version of gettext.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
374 |
k = k.strip().lower() |
7d53d8491932
gettext] provide a more up-to-date version of gettext.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
375 |
v = v.strip() |
7d53d8491932
gettext] provide a more up-to-date version of gettext.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
376 |
self._info[k] = v |
7d53d8491932
gettext] provide a more up-to-date version of gettext.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
377 |
lastk = k |
7d53d8491932
gettext] provide a more up-to-date version of gettext.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
378 |
elif lastk: |
7d53d8491932
gettext] provide a more up-to-date version of gettext.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
379 |
self._info[lastk] += '\n' + item |
0 | 380 |
if k == 'content-type': |
381 |
self._charset = v.split('charset=')[1] |
|
382 |
elif k == 'plural-forms': |
|
383 |
v = v.split(';') |
|
384 |
plural = v[1].split('plural=')[1] |
|
385 |
self.plural = c2py(plural) |
|
386 |
# Note: we unconditionally convert both msgids and msgstrs to |
|
387 |
# Unicode using the character encoding specified in the charset |
|
388 |
# parameter of the Content-Type header. The gettext documentation |
|
389 |
# strongly encourages msgids to be us-ascii, but some appliations |
|
390 |
# require alternative encodings (e.g. Zope's ZCML and ZPT). For |
|
391 |
# traditional gettext applications, the msgid conversion will |
|
392 |
# cause no problems since us-ascii should always be a subset of |
|
393 |
# the charset encoding. We may want to fall back to 8-bit msgids |
|
394 |
# if the Unicode conversion fails. |
|
3274
7d53d8491932
gettext] provide a more up-to-date version of gettext.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
395 |
if '\x00' in msg: |
0 | 396 |
# Plural forms |
397 |
msgid1, msgid2 = msg.split('\x00') |
|
398 |
tmsg = tmsg.split('\x00') |
|
399 |
if self._charset: |
|
400 |
msgid1 = unicode(msgid1, self._charset) |
|
401 |
tmsg = [unicode(x, self._charset) for x in tmsg] |
|
402 |
for i in range(len(tmsg)): |
|
403 |
catalog[(msgid1, i)] = tmsg[i] |
|
404 |
else: |
|
405 |
if self._charset: |
|
406 |
msg = unicode(msg, self._charset) |
|
407 |
tmsg = unicode(tmsg, self._charset) |
|
408 |
catalog[msg] = tmsg |
|
409 |
# advance to next entry in the seek tables |
|
410 |
masteridx += 8 |
|
411 |
transidx += 8 |
|
412 |
||
413 |
def gettext(self, message): |
|
414 |
missing = object() |
|
415 |
tmsg = self._catalog.get(message, missing) |
|
416 |
if tmsg is missing: |
|
417 |
if self._fallback: |
|
418 |
return self._fallback.gettext(message) |
|
419 |
return message |
|
420 |
# Encode the Unicode tmsg back to an 8-bit string, if possible |
|
3274
7d53d8491932
gettext] provide a more up-to-date version of gettext.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
421 |
if self._output_charset: |
7d53d8491932
gettext] provide a more up-to-date version of gettext.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
422 |
return tmsg.encode(self._output_charset) |
7d53d8491932
gettext] provide a more up-to-date version of gettext.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
423 |
elif self._charset: |
7d53d8491932
gettext] provide a more up-to-date version of gettext.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
424 |
return tmsg.encode(self._charset) |
7d53d8491932
gettext] provide a more up-to-date version of gettext.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
425 |
return tmsg |
7d53d8491932
gettext] provide a more up-to-date version of gettext.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
426 |
|
7d53d8491932
gettext] provide a more up-to-date version of gettext.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
427 |
def pgettext(self, context, message): |
7d53d8491932
gettext] provide a more up-to-date version of gettext.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
428 |
ctxt_msg_id = self.CONTEXT_ENCODING % (context, message) |
7d53d8491932
gettext] provide a more up-to-date version of gettext.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
429 |
missing = object() |
7d53d8491932
gettext] provide a more up-to-date version of gettext.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
430 |
tmsg = self._catalog.get(ctxt_msg_id, missing) |
7d53d8491932
gettext] provide a more up-to-date version of gettext.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
431 |
if tmsg is missing: |
7d53d8491932
gettext] provide a more up-to-date version of gettext.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
432 |
if self._fallback: |
7d53d8491932
gettext] provide a more up-to-date version of gettext.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
433 |
return self._fallback.pgettext(context, message) |
7d53d8491932
gettext] provide a more up-to-date version of gettext.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
434 |
return message |
7d53d8491932
gettext] provide a more up-to-date version of gettext.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
435 |
# Encode the Unicode tmsg back to an 8-bit string, if possible |
7d53d8491932
gettext] provide a more up-to-date version of gettext.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
436 |
if self._output_charset: |
7d53d8491932
gettext] provide a more up-to-date version of gettext.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
437 |
return tmsg.encode(self._output_charset) |
7d53d8491932
gettext] provide a more up-to-date version of gettext.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
438 |
elif self._charset: |
0 | 439 |
return tmsg.encode(self._charset) |
440 |
return tmsg |
|
3279
6a2cde3f886e
patch gettext for compat w/ catalog generated by cw < 3.5
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3274
diff
changeset
|
441 |
|
3274
7d53d8491932
gettext] provide a more up-to-date version of gettext.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
442 |
def lgettext(self, message): |
7d53d8491932
gettext] provide a more up-to-date version of gettext.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
443 |
missing = object() |
7d53d8491932
gettext] provide a more up-to-date version of gettext.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
444 |
tmsg = self._catalog.get(message, missing) |
7d53d8491932
gettext] provide a more up-to-date version of gettext.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
445 |
if tmsg is missing: |
7d53d8491932
gettext] provide a more up-to-date version of gettext.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
446 |
if self._fallback: |
7d53d8491932
gettext] provide a more up-to-date version of gettext.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
447 |
return self._fallback.lgettext(message) |
7d53d8491932
gettext] provide a more up-to-date version of gettext.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
448 |
return message |
7d53d8491932
gettext] provide a more up-to-date version of gettext.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
449 |
if self._output_charset: |
7d53d8491932
gettext] provide a more up-to-date version of gettext.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
450 |
return tmsg.encode(self._output_charset) |
7d53d8491932
gettext] provide a more up-to-date version of gettext.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
451 |
return tmsg.encode(locale.getpreferredencoding()) |
7d53d8491932
gettext] provide a more up-to-date version of gettext.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
452 |
|
7d53d8491932
gettext] provide a more up-to-date version of gettext.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
453 |
def lpgettext(self, context, message): |
7d53d8491932
gettext] provide a more up-to-date version of gettext.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
454 |
ctxt_msg_id = self.CONTEXT_ENCODING % (context, message) |
7d53d8491932
gettext] provide a more up-to-date version of gettext.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
455 |
missing = object() |
7d53d8491932
gettext] provide a more up-to-date version of gettext.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
456 |
tmsg = self._catalog.get(ctxt_msg_id, missing) |
7d53d8491932
gettext] provide a more up-to-date version of gettext.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
457 |
if tmsg is missing: |
7d53d8491932
gettext] provide a more up-to-date version of gettext.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
458 |
if self._fallback: |
7d53d8491932
gettext] provide a more up-to-date version of gettext.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
459 |
return self._fallback.lpgettext(context, message) |
7d53d8491932
gettext] provide a more up-to-date version of gettext.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
460 |
return message |
7d53d8491932
gettext] provide a more up-to-date version of gettext.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
461 |
if self._output_charset: |
7d53d8491932
gettext] provide a more up-to-date version of gettext.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
462 |
return tmsg.encode(self._output_charset) |
7d53d8491932
gettext] provide a more up-to-date version of gettext.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
463 |
return tmsg.encode(locale.getpreferredencoding()) |
0 | 464 |
|
465 |
def ngettext(self, msgid1, msgid2, n): |
|
466 |
try: |
|
467 |
tmsg = self._catalog[(msgid1, self.plural(n))] |
|
3274
7d53d8491932
gettext] provide a more up-to-date version of gettext.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
468 |
if self._output_charset: |
7d53d8491932
gettext] provide a more up-to-date version of gettext.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
469 |
return tmsg.encode(self._output_charset) |
7d53d8491932
gettext] provide a more up-to-date version of gettext.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
470 |
elif self._charset: |
0 | 471 |
return tmsg.encode(self._charset) |
472 |
return tmsg |
|
473 |
except KeyError: |
|
474 |
if self._fallback: |
|
475 |
return self._fallback.ngettext(msgid1, msgid2, n) |
|
476 |
if n == 1: |
|
477 |
return msgid1 |
|
478 |
else: |
|
479 |
return msgid2 |
|
480 |
||
3274
7d53d8491932
gettext] provide a more up-to-date version of gettext.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
481 |
def npgettext(self, context, msgid1, msgid2, n): |
7d53d8491932
gettext] provide a more up-to-date version of gettext.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
482 |
ctxt_msg_id = self.CONTEXT_ENCODING % (context, msgid1) |
7d53d8491932
gettext] provide a more up-to-date version of gettext.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
483 |
try: |
7d53d8491932
gettext] provide a more up-to-date version of gettext.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
484 |
tmsg = self._catalog[(ctxt_msg_id, self.plural(n))] |
7d53d8491932
gettext] provide a more up-to-date version of gettext.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
485 |
if self._output_charset: |
7d53d8491932
gettext] provide a more up-to-date version of gettext.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
486 |
return tmsg.encode(self._output_charset) |
7d53d8491932
gettext] provide a more up-to-date version of gettext.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
487 |
elif self._charset: |
7d53d8491932
gettext] provide a more up-to-date version of gettext.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
488 |
return tmsg.encode(self._charset) |
7d53d8491932
gettext] provide a more up-to-date version of gettext.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
489 |
return tmsg |
7d53d8491932
gettext] provide a more up-to-date version of gettext.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
490 |
except KeyError: |
7d53d8491932
gettext] provide a more up-to-date version of gettext.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
491 |
if self._fallback: |
7d53d8491932
gettext] provide a more up-to-date version of gettext.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
492 |
return self._fallback.npgettext(context, msgid1, msgid2, n) |
7d53d8491932
gettext] provide a more up-to-date version of gettext.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
493 |
if n == 1: |
7d53d8491932
gettext] provide a more up-to-date version of gettext.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
494 |
return msgid1 |
7d53d8491932
gettext] provide a more up-to-date version of gettext.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
495 |
else: |
7d53d8491932
gettext] provide a more up-to-date version of gettext.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
496 |
return msgid2 |
7d53d8491932
gettext] provide a more up-to-date version of gettext.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
497 |
|
7d53d8491932
gettext] provide a more up-to-date version of gettext.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
498 |
def lngettext(self, msgid1, msgid2, n): |
7d53d8491932
gettext] provide a more up-to-date version of gettext.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
499 |
try: |
7d53d8491932
gettext] provide a more up-to-date version of gettext.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
500 |
tmsg = self._catalog[(msgid1, self.plural(n))] |
7d53d8491932
gettext] provide a more up-to-date version of gettext.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
501 |
if self._output_charset: |
7d53d8491932
gettext] provide a more up-to-date version of gettext.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
502 |
return tmsg.encode(self._output_charset) |
7d53d8491932
gettext] provide a more up-to-date version of gettext.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
503 |
return tmsg.encode(locale.getpreferredencoding()) |
7d53d8491932
gettext] provide a more up-to-date version of gettext.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
504 |
except KeyError: |
7d53d8491932
gettext] provide a more up-to-date version of gettext.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
505 |
if self._fallback: |
7d53d8491932
gettext] provide a more up-to-date version of gettext.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
506 |
return self._fallback.lngettext(msgid1, msgid2, n) |
7d53d8491932
gettext] provide a more up-to-date version of gettext.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
507 |
if n == 1: |
7d53d8491932
gettext] provide a more up-to-date version of gettext.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
508 |
return msgid1 |
7d53d8491932
gettext] provide a more up-to-date version of gettext.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
509 |
else: |
7d53d8491932
gettext] provide a more up-to-date version of gettext.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
510 |
return msgid2 |
7d53d8491932
gettext] provide a more up-to-date version of gettext.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
511 |
|
7d53d8491932
gettext] provide a more up-to-date version of gettext.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
512 |
def lnpgettext(self, context, msgid1, msgid2, n): |
7d53d8491932
gettext] provide a more up-to-date version of gettext.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
513 |
ctxt_msg_id = self.CONTEXT_ENCODING % (context, msgid1) |
7d53d8491932
gettext] provide a more up-to-date version of gettext.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
514 |
try: |
7d53d8491932
gettext] provide a more up-to-date version of gettext.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
515 |
tmsg = self._catalog[(ctxt_msg_id, self.plural(n))] |
7d53d8491932
gettext] provide a more up-to-date version of gettext.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
516 |
if self._output_charset: |
7d53d8491932
gettext] provide a more up-to-date version of gettext.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
517 |
return tmsg.encode(self._output_charset) |
7d53d8491932
gettext] provide a more up-to-date version of gettext.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
518 |
return tmsg.encode(locale.getpreferredencoding()) |
7d53d8491932
gettext] provide a more up-to-date version of gettext.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
519 |
except KeyError: |
7d53d8491932
gettext] provide a more up-to-date version of gettext.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
520 |
if self._fallback: |
7d53d8491932
gettext] provide a more up-to-date version of gettext.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
521 |
return self._fallback.lnpgettext(context, msgid1, msgid2, n) |
7d53d8491932
gettext] provide a more up-to-date version of gettext.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
522 |
if n == 1: |
7d53d8491932
gettext] provide a more up-to-date version of gettext.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
523 |
return msgid1 |
7d53d8491932
gettext] provide a more up-to-date version of gettext.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
524 |
else: |
7d53d8491932
gettext] provide a more up-to-date version of gettext.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
525 |
return msgid2 |
7d53d8491932
gettext] provide a more up-to-date version of gettext.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
526 |
|
0 | 527 |
def ugettext(self, message): |
528 |
missing = object() |
|
529 |
tmsg = self._catalog.get(message, missing) |
|
530 |
if tmsg is missing: |
|
531 |
if self._fallback: |
|
532 |
return self._fallback.ugettext(message) |
|
533 |
return unicode(message) |
|
534 |
return tmsg |
|
535 |
||
3274
7d53d8491932
gettext] provide a more up-to-date version of gettext.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
536 |
def upgettext(self, context, message): |
7d53d8491932
gettext] provide a more up-to-date version of gettext.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
537 |
ctxt_message_id = self.CONTEXT_ENCODING % (context, message) |
7d53d8491932
gettext] provide a more up-to-date version of gettext.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
538 |
missing = object() |
7d53d8491932
gettext] provide a more up-to-date version of gettext.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
539 |
tmsg = self._catalog.get(ctxt_message_id, missing) |
7d53d8491932
gettext] provide a more up-to-date version of gettext.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
540 |
if tmsg is missing: |
3279
6a2cde3f886e
patch gettext for compat w/ catalog generated by cw < 3.5
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3274
diff
changeset
|
541 |
# XXX logilab patch for compat w/ catalog generated by cw < 3.5 |
6a2cde3f886e
patch gettext for compat w/ catalog generated by cw < 3.5
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3274
diff
changeset
|
542 |
return self.ugettext(message) |
3274
7d53d8491932
gettext] provide a more up-to-date version of gettext.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
543 |
if self._fallback: |
7d53d8491932
gettext] provide a more up-to-date version of gettext.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
544 |
return self._fallback.upgettext(context, message) |
7d53d8491932
gettext] provide a more up-to-date version of gettext.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
545 |
return unicode(message) |
7d53d8491932
gettext] provide a more up-to-date version of gettext.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
546 |
return tmsg |
7d53d8491932
gettext] provide a more up-to-date version of gettext.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
547 |
|
0 | 548 |
def ungettext(self, msgid1, msgid2, n): |
549 |
try: |
|
550 |
tmsg = self._catalog[(msgid1, self.plural(n))] |
|
551 |
except KeyError: |
|
552 |
if self._fallback: |
|
553 |
return self._fallback.ungettext(msgid1, msgid2, n) |
|
554 |
if n == 1: |
|
555 |
tmsg = unicode(msgid1) |
|
556 |
else: |
|
557 |
tmsg = unicode(msgid2) |
|
558 |
return tmsg |
|
559 |
||
3274
7d53d8491932
gettext] provide a more up-to-date version of gettext.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
560 |
def unpgettext(self, context, msgid1, msgid2, n): |
7d53d8491932
gettext] provide a more up-to-date version of gettext.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
561 |
ctxt_message_id = self.CONTEXT_ENCODING % (context, msgid1) |
7d53d8491932
gettext] provide a more up-to-date version of gettext.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
562 |
try: |
7d53d8491932
gettext] provide a more up-to-date version of gettext.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
563 |
tmsg = self._catalog[(ctxt_message_id, self.plural(n))] |
7d53d8491932
gettext] provide a more up-to-date version of gettext.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
564 |
except KeyError: |
7d53d8491932
gettext] provide a more up-to-date version of gettext.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
565 |
if self._fallback: |
7d53d8491932
gettext] provide a more up-to-date version of gettext.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
566 |
return self._fallback.unpgettext(context, msgid1, msgid2, n) |
7d53d8491932
gettext] provide a more up-to-date version of gettext.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
567 |
if n == 1: |
7d53d8491932
gettext] provide a more up-to-date version of gettext.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
568 |
tmsg = unicode(msgid1) |
7d53d8491932
gettext] provide a more up-to-date version of gettext.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
569 |
else: |
7d53d8491932
gettext] provide a more up-to-date version of gettext.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
570 |
tmsg = unicode(msgid2) |
7d53d8491932
gettext] provide a more up-to-date version of gettext.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
571 |
return tmsg |
7d53d8491932
gettext] provide a more up-to-date version of gettext.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
572 |
|
0 | 573 |
|
574 |
# Locate a .mo file using the gettext strategy |
|
575 |
def find(domain, localedir=None, languages=None, all=0): |
|
576 |
# Get some reasonable defaults for arguments that were not supplied |
|
577 |
if localedir is None: |
|
578 |
localedir = _default_localedir |
|
579 |
if languages is None: |
|
580 |
languages = [] |
|
581 |
for envar in ('LANGUAGE', 'LC_ALL', 'LC_MESSAGES', 'LANG'): |
|
582 |
val = os.environ.get(envar) |
|
583 |
if val: |
|
584 |
languages = val.split(':') |
|
585 |
break |
|
586 |
if 'C' not in languages: |
|
587 |
languages.append('C') |
|
588 |
# now normalize and expand the languages |
|
589 |
nelangs = [] |
|
590 |
for lang in languages: |
|
591 |
for nelang in _expand_lang(lang): |
|
592 |
if nelang not in nelangs: |
|
593 |
nelangs.append(nelang) |
|
594 |
# select a language |
|
595 |
if all: |
|
596 |
result = [] |
|
597 |
else: |
|
598 |
result = None |
|
599 |
for lang in nelangs: |
|
600 |
if lang == 'C': |
|
601 |
break |
|
602 |
mofile = os.path.join(localedir, lang, 'LC_MESSAGES', '%s.mo' % domain) |
|
603 |
if os.path.exists(mofile): |
|
604 |
if all: |
|
605 |
result.append(mofile) |
|
606 |
else: |
|
607 |
return mofile |
|
608 |
return result |
|
609 |
||
610 |
||
611 |
||
612 |
# a mapping between absolute .mo file path and Translation object |
|
613 |
_translations = {} |
|
614 |
||
615 |
def translation(domain, localedir=None, languages=None, |
|
3274
7d53d8491932
gettext] provide a more up-to-date version of gettext.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
616 |
class_=None, fallback=False, codeset=None): |
0 | 617 |
if class_ is None: |
618 |
class_ = GNUTranslations |
|
619 |
mofiles = find(domain, localedir, languages, all=1) |
|
620 |
if not mofiles: |
|
621 |
if fallback: |
|
622 |
return NullTranslations() |
|
623 |
raise IOError(ENOENT, 'No translation file found for domain', domain) |
|
624 |
# TBD: do we need to worry about the file pointer getting collected? |
|
625 |
# Avoid opening, reading, and parsing the .mo file after it's been done |
|
626 |
# once. |
|
627 |
result = None |
|
628 |
for mofile in mofiles: |
|
629 |
key = os.path.abspath(mofile) |
|
630 |
t = _translations.get(key) |
|
631 |
if t is None: |
|
632 |
t = _translations.setdefault(key, class_(open(mofile, 'rb'))) |
|
3274
7d53d8491932
gettext] provide a more up-to-date version of gettext.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
633 |
# Copy the translation object to allow setting fallbacks and |
7d53d8491932
gettext] provide a more up-to-date version of gettext.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
634 |
# output charset. All other instance data is shared with the |
7d53d8491932
gettext] provide a more up-to-date version of gettext.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
635 |
# cached object. |
0 | 636 |
t = copy.copy(t) |
3274
7d53d8491932
gettext] provide a more up-to-date version of gettext.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
637 |
if codeset: |
7d53d8491932
gettext] provide a more up-to-date version of gettext.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
638 |
t.set_output_charset(codeset) |
0 | 639 |
if result is None: |
640 |
result = t |
|
641 |
else: |
|
642 |
result.add_fallback(t) |
|
643 |
return result |
|
644 |
||
645 |
||
3274
7d53d8491932
gettext] provide a more up-to-date version of gettext.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
646 |
def install(domain, localedir=None, unicode=False, codeset=None, names=None): |
7d53d8491932
gettext] provide a more up-to-date version of gettext.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
647 |
t = translation(domain, localedir, fallback=True, codeset=codeset) |
7d53d8491932
gettext] provide a more up-to-date version of gettext.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
648 |
t.install(unicode, names) |
0 | 649 |
|
650 |
||
651 |
||
652 |
# a mapping b/w domains and locale directories |
|
653 |
_localedirs = {} |
|
3274
7d53d8491932
gettext] provide a more up-to-date version of gettext.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
654 |
# a mapping b/w domains and codesets |
7d53d8491932
gettext] provide a more up-to-date version of gettext.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
655 |
_localecodesets = {} |
0 | 656 |
# current global domain, `messages' used for compatibility w/ GNU gettext |
657 |
_current_domain = 'messages' |
|
658 |
||
659 |
||
660 |
def textdomain(domain=None): |
|
661 |
global _current_domain |
|
662 |
if domain is not None: |
|
663 |
_current_domain = domain |
|
664 |
return _current_domain |
|
665 |
||
666 |
||
667 |
def bindtextdomain(domain, localedir=None): |
|
3274
7d53d8491932
gettext] provide a more up-to-date version of gettext.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
668 |
global _localedirs |
0 | 669 |
if localedir is not None: |
670 |
_localedirs[domain] = localedir |
|
671 |
return _localedirs.get(domain, _default_localedir) |
|
672 |
||
673 |
||
3274
7d53d8491932
gettext] provide a more up-to-date version of gettext.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
674 |
def bind_textdomain_codeset(domain, codeset=None): |
7d53d8491932
gettext] provide a more up-to-date version of gettext.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
675 |
global _localecodesets |
7d53d8491932
gettext] provide a more up-to-date version of gettext.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
676 |
if codeset is not None: |
7d53d8491932
gettext] provide a more up-to-date version of gettext.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
677 |
_localecodesets[domain] = codeset |
7d53d8491932
gettext] provide a more up-to-date version of gettext.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
678 |
return _localecodesets.get(domain) |
7d53d8491932
gettext] provide a more up-to-date version of gettext.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
679 |
|
7d53d8491932
gettext] provide a more up-to-date version of gettext.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
680 |
|
0 | 681 |
def dgettext(domain, message): |
682 |
try: |
|
3274
7d53d8491932
gettext] provide a more up-to-date version of gettext.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
683 |
t = translation(domain, _localedirs.get(domain, None), |
7d53d8491932
gettext] provide a more up-to-date version of gettext.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
684 |
codeset=_localecodesets.get(domain)) |
0 | 685 |
except IOError: |
686 |
return message |
|
687 |
return t.gettext(message) |
|
688 |
||
3274
7d53d8491932
gettext] provide a more up-to-date version of gettext.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
689 |
def dpgettext(domain, context, message): |
7d53d8491932
gettext] provide a more up-to-date version of gettext.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
690 |
try: |
7d53d8491932
gettext] provide a more up-to-date version of gettext.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
691 |
t = translation(domain, _localedirs.get(domain, None), |
7d53d8491932
gettext] provide a more up-to-date version of gettext.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
692 |
codeset=_localecodesets.get(domain)) |
7d53d8491932
gettext] provide a more up-to-date version of gettext.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
693 |
except IOError: |
7d53d8491932
gettext] provide a more up-to-date version of gettext.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
694 |
return message |
7d53d8491932
gettext] provide a more up-to-date version of gettext.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
695 |
return t.pgettext(context, message) |
7d53d8491932
gettext] provide a more up-to-date version of gettext.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
696 |
|
7d53d8491932
gettext] provide a more up-to-date version of gettext.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
697 |
def ldgettext(domain, message): |
7d53d8491932
gettext] provide a more up-to-date version of gettext.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
698 |
try: |
7d53d8491932
gettext] provide a more up-to-date version of gettext.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
699 |
t = translation(domain, _localedirs.get(domain, None), |
7d53d8491932
gettext] provide a more up-to-date version of gettext.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
700 |
codeset=_localecodesets.get(domain)) |
7d53d8491932
gettext] provide a more up-to-date version of gettext.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
701 |
except IOError: |
7d53d8491932
gettext] provide a more up-to-date version of gettext.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
702 |
return message |
7d53d8491932
gettext] provide a more up-to-date version of gettext.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
703 |
return t.lgettext(message) |
7d53d8491932
gettext] provide a more up-to-date version of gettext.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
704 |
|
7d53d8491932
gettext] provide a more up-to-date version of gettext.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
705 |
def ldpgettext(domain, context, message): |
7d53d8491932
gettext] provide a more up-to-date version of gettext.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
706 |
try: |
7d53d8491932
gettext] provide a more up-to-date version of gettext.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
707 |
t = translation(domain, _localedirs.get(domain, None), |
7d53d8491932
gettext] provide a more up-to-date version of gettext.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
708 |
codeset=_localecodesets.get(domain)) |
7d53d8491932
gettext] provide a more up-to-date version of gettext.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
709 |
except IOError: |
7d53d8491932
gettext] provide a more up-to-date version of gettext.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
710 |
return message |
7d53d8491932
gettext] provide a more up-to-date version of gettext.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
711 |
return t.lpgettext(context, message) |
0 | 712 |
|
713 |
def dngettext(domain, msgid1, msgid2, n): |
|
714 |
try: |
|
3274
7d53d8491932
gettext] provide a more up-to-date version of gettext.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
715 |
t = translation(domain, _localedirs.get(domain, None), |
7d53d8491932
gettext] provide a more up-to-date version of gettext.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
716 |
codeset=_localecodesets.get(domain)) |
0 | 717 |
except IOError: |
718 |
if n == 1: |
|
719 |
return msgid1 |
|
720 |
else: |
|
721 |
return msgid2 |
|
722 |
return t.ngettext(msgid1, msgid2, n) |
|
723 |
||
3274
7d53d8491932
gettext] provide a more up-to-date version of gettext.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
724 |
def dnpgettext(domain, context, msgid1, msgid2, n): |
7d53d8491932
gettext] provide a more up-to-date version of gettext.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
725 |
try: |
7d53d8491932
gettext] provide a more up-to-date version of gettext.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
726 |
t = translation(domain, _localedirs.get(domain, None), |
7d53d8491932
gettext] provide a more up-to-date version of gettext.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
727 |
codeset=_localecodesets.get(domain)) |
7d53d8491932
gettext] provide a more up-to-date version of gettext.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
728 |
except IOError: |
7d53d8491932
gettext] provide a more up-to-date version of gettext.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
729 |
if n == 1: |
7d53d8491932
gettext] provide a more up-to-date version of gettext.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
730 |
return msgid1 |
7d53d8491932
gettext] provide a more up-to-date version of gettext.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
731 |
else: |
7d53d8491932
gettext] provide a more up-to-date version of gettext.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
732 |
return msgid2 |
7d53d8491932
gettext] provide a more up-to-date version of gettext.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
733 |
return t.npgettext(context, msgid1, msgid2, n) |
7d53d8491932
gettext] provide a more up-to-date version of gettext.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
734 |
|
7d53d8491932
gettext] provide a more up-to-date version of gettext.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
735 |
def ldngettext(domain, msgid1, msgid2, n): |
7d53d8491932
gettext] provide a more up-to-date version of gettext.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
736 |
try: |
7d53d8491932
gettext] provide a more up-to-date version of gettext.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
737 |
t = translation(domain, _localedirs.get(domain, None), |
7d53d8491932
gettext] provide a more up-to-date version of gettext.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
738 |
codeset=_localecodesets.get(domain)) |
7d53d8491932
gettext] provide a more up-to-date version of gettext.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
739 |
except IOError: |
7d53d8491932
gettext] provide a more up-to-date version of gettext.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
740 |
if n == 1: |
7d53d8491932
gettext] provide a more up-to-date version of gettext.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
741 |
return msgid1 |
7d53d8491932
gettext] provide a more up-to-date version of gettext.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
742 |
else: |
7d53d8491932
gettext] provide a more up-to-date version of gettext.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
743 |
return msgid2 |
7d53d8491932
gettext] provide a more up-to-date version of gettext.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
744 |
return t.lngettext(msgid1, msgid2, n) |
7d53d8491932
gettext] provide a more up-to-date version of gettext.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
745 |
|
7d53d8491932
gettext] provide a more up-to-date version of gettext.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
746 |
def ldnpgettext(domain, context, msgid1, msgid2, n): |
7d53d8491932
gettext] provide a more up-to-date version of gettext.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
747 |
try: |
7d53d8491932
gettext] provide a more up-to-date version of gettext.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
748 |
t = translation(domain, _localedirs.get(domain, None), |
7d53d8491932
gettext] provide a more up-to-date version of gettext.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
749 |
codeset=_localecodesets.get(domain)) |
7d53d8491932
gettext] provide a more up-to-date version of gettext.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
750 |
except IOError: |
7d53d8491932
gettext] provide a more up-to-date version of gettext.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
751 |
if n == 1: |
7d53d8491932
gettext] provide a more up-to-date version of gettext.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
752 |
return msgid1 |
7d53d8491932
gettext] provide a more up-to-date version of gettext.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
753 |
else: |
7d53d8491932
gettext] provide a more up-to-date version of gettext.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
754 |
return msgid2 |
7d53d8491932
gettext] provide a more up-to-date version of gettext.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
755 |
return t.lnpgettext(context, msgid1, msgid2, n) |
0 | 756 |
|
757 |
def gettext(message): |
|
758 |
return dgettext(_current_domain, message) |
|
759 |
||
3274
7d53d8491932
gettext] provide a more up-to-date version of gettext.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
760 |
def pgettext(context, message): |
7d53d8491932
gettext] provide a more up-to-date version of gettext.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
761 |
return dpgettext(_current_domain, context, message) |
7d53d8491932
gettext] provide a more up-to-date version of gettext.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
762 |
|
7d53d8491932
gettext] provide a more up-to-date version of gettext.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
763 |
def lgettext(message): |
7d53d8491932
gettext] provide a more up-to-date version of gettext.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
764 |
return ldgettext(_current_domain, message) |
7d53d8491932
gettext] provide a more up-to-date version of gettext.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
765 |
|
7d53d8491932
gettext] provide a more up-to-date version of gettext.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
766 |
def lpgettext(context, message): |
7d53d8491932
gettext] provide a more up-to-date version of gettext.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
767 |
return ldpgettext(_current_domain, context, message) |
0 | 768 |
|
769 |
def ngettext(msgid1, msgid2, n): |
|
770 |
return dngettext(_current_domain, msgid1, msgid2, n) |
|
771 |
||
3274
7d53d8491932
gettext] provide a more up-to-date version of gettext.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
772 |
def npgettext(context, msgid1, msgid2, n): |
7d53d8491932
gettext] provide a more up-to-date version of gettext.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
773 |
return dnpgettext(_current_domain, context, msgid1, msgid2, n) |
7d53d8491932
gettext] provide a more up-to-date version of gettext.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
774 |
|
7d53d8491932
gettext] provide a more up-to-date version of gettext.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
775 |
def lngettext(msgid1, msgid2, n): |
7d53d8491932
gettext] provide a more up-to-date version of gettext.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
776 |
return ldngettext(_current_domain, msgid1, msgid2, n) |
7d53d8491932
gettext] provide a more up-to-date version of gettext.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
777 |
|
7d53d8491932
gettext] provide a more up-to-date version of gettext.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
778 |
def lnpgettext(context, msgid1, msgid2, n): |
7d53d8491932
gettext] provide a more up-to-date version of gettext.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1977
diff
changeset
|
779 |
return ldnpgettext(_current_domain, context, msgid1, msgid2, n) |
0 | 780 |
|
781 |
# dcgettext() has been deemed unnecessary and is not implemented. |
|
782 |
||
783 |
# James Henstridge's Catalog constructor from GNOME gettext. Documented usage |
|
784 |
# was: |
|
785 |
# |
|
786 |
# import gettext |
|
787 |
# cat = gettext.Catalog(PACKAGE, localedir=LOCALEDIR) |
|
788 |
# _ = cat.gettext |
|
789 |
# print _('Hello World') |
|
790 |
||
791 |
# The resulting catalog object currently don't support access through a |
|
792 |
# dictionary API, which was supported (but apparently unused) in GNOME |
|
793 |
# gettext. |
|
794 |
||
795 |
Catalog = translation |