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