/
translation.py
405 lines (329 loc) · 13.8 KB
/
translation.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
# -*- coding: utf-8 -*-
#
# Copyright (C) 2007-2009 Edgewall Software
# All rights reserved.
#
# This software is licensed as described in the file COPYING, which
# you should have received as part of this distribution. The terms
# are also available at http://trac.edgewall.org/wiki/TracLicense.
#
# This software consists of voluntary contributions made by many
# individuals. For the exact contribution history, see the revision
# history and logs, available at http://trac.edgewall.org/log/.
"""Utilities for text translation with gettext."""
import pkg_resources
import re
from genshi.builder import tag
from trac.util.concurrency import ThreadLocal, threading
from trac.util.text import cleandoc
__all__ = ['gettext', 'ngettext', 'gettext_noop', 'ngettext_noop',
'tgettext', 'tgettext_noop', 'tngettext', 'tngettext_noop']
def safefmt(string, kwargs):
if kwargs:
try:
return string % kwargs
except KeyError:
pass
return string
def gettext_noop(string, **kwargs):
return safefmt(string, kwargs)
def dgettext_noop(domain, string, **kwargs):
return gettext_noop(string, **kwargs)
N_ = _noop = lambda string: string
cleandoc_ = cleandoc
def ngettext_noop(singular, plural, num, **kwargs):
string = singular if num == 1 else plural
kwargs.setdefault('num', num)
return safefmt(string, kwargs)
def dngettext_noop(domain, singular, plural, num, **kwargs):
return ngettext_noop(singular, plural, num, **kwargs)
_param_re = re.compile(r"%\((\w+)\)(?:s|[\d]*d|\d*.?\d*[fg])")
def _tag_kwargs(trans, kwargs):
trans_elts = _param_re.split(trans)
for i in xrange(1, len(trans_elts), 2):
trans_elts[i] = kwargs.get(trans_elts[i], '???')
return tag(*trans_elts)
def tgettext_noop(string, **kwargs):
return _tag_kwargs(string, kwargs) if kwargs else string
def dtgettext_noop(domain, string, **kwargs):
return tgettext_noop(string, **kwargs)
def tngettext_noop(singular, plural, num, **kwargs):
string = singular if num == 1 else plural
kwargs.setdefault('num', num)
return _tag_kwargs(string, kwargs)
def dtngettext_noop(domain, singular, plural, num, **kwargs):
return tngettext_noop(singular, plural, num, **kwargs)
def add_domain(domain, env_path, locale_dir):
pass
def domain_functions(domain, *symbols):
if symbols and not isinstance(symbols[0], basestring):
symbols = symbols[0]
_functions = {
'gettext': gettext_noop,
'_': gettext_noop,
'N_': _noop,
'ngettext': ngettext_noop,
'tgettext': tgettext_noop,
'tag_': tgettext_noop,
'tngettext': tngettext_noop,
'tagn_': tngettext_noop,
'add_domain': lambda env_path, locale_dir: None,
}
return [_functions[s] for s in symbols]
from gettext import NullTranslations
class NullTranslationsBabel(NullTranslations):
"""NullTranslations doesn't have the domain related methods."""
def dugettext(self, domain, string):
return self.ugettext(string)
def dungettext(self, domain, singular, plural, num):
return self.ungettext(singular, plural, num)
has_babel = False
try:
from babel import Locale
from babel.support import LazyProxy, Translations
class TranslationsProxy(object):
"""Delegate Translations calls to the currently active Translations.
If there's none, wrap those calls in LazyProxy objects.
Activation is controlled by `activate` and `deactivate` methods.
However, if retrieving the locale information is costly, it's also
possible to enable activation on demand only, by providing a callable
to `make_activable`.
"""
def __init__(self):
self._current = ThreadLocal(args=None, translations=None)
self._null_translations = NullTranslationsBabel()
self._plugin_domains = {}
self._plugin_domains_lock = threading.RLock()
self._activate_failed = False
# Public API
def add_domain(self, domain, env_path, locales_dir):
with self._plugin_domains_lock:
domains = self._plugin_domains.setdefault(env_path, {})
domains[domain] = locales_dir
def make_activable(self, get_locale, env_path=None):
self._current.args = (get_locale, env_path)
def activate(self, locale, env_path=None):
try:
locale_dir = pkg_resources.resource_filename('trac', 'locale')
except Exception:
self._activate_failed = True
return
t = Translations.load(locale_dir, locale or 'en_US')
if not isinstance(t, Translations):
t = self._null_translations
else:
self._add(t, Translations.load(locale_dir, locale or 'en_US',
'tracini'))
if env_path:
with self._plugin_domains_lock:
domains = self._plugin_domains.get(env_path, {})
domains = domains.items()
for domain, dirname in domains:
self._add(t, Translations.load(dirname, locale,
domain))
self._current.translations = t
self._activate_failed = False
def deactivate(self):
self._current.args = None
t, self._current.translations = self._current.translations, None
return t
def reactivate(self, t):
if t:
self._current.translations = t
@property
def active(self):
return self._current.translations or self._null_translations
@property
def isactive(self):
if self._current.args is not None:
get_locale, env_path = self._current.args
self._current.args = None
self.activate(get_locale(), env_path)
# FIXME: The following always returns True: either a translation is
# active, or activation has failed.
return self._current.translations is not None \
or self._activate_failed
# Internal methods
def _add(self, t, translations):
if isinstance(translations, Translations):
t.add(translations)
# Delegated methods
def __getattr__(self, name):
return getattr(self.active, name)
def gettext(self, string, **kwargs):
def _gettext():
return safefmt(self.active.ugettext(string), kwargs)
if not self.isactive:
return LazyProxy(_gettext)
return _gettext()
def dgettext(self, domain, string, **kwargs):
def _dgettext():
return safefmt(self.active.dugettext(domain, string), kwargs)
if not self.isactive:
return LazyProxy(_dgettext)
return _dgettext()
def ngettext(self, singular, plural, num, **kwargs):
kwargs = kwargs.copy()
kwargs.setdefault('num', num)
def _ngettext():
trans = self.active.ungettext(singular, plural, num)
return safefmt(trans, kwargs)
if not self.isactive:
return LazyProxy(_ngettext)
return _ngettext()
def dngettext(self, domain, singular, plural, num, **kwargs):
kwargs = kwargs.copy()
kwargs.setdefault('num', num)
def _dngettext():
trans = self.active.dungettext(domain, singular, plural, num)
return safefmt(trans, kwargs)
if not self.isactive:
return LazyProxy(_dngettext)
return _dngettext()
def tgettext(self, string, **kwargs):
def _tgettext():
trans = self.active.ugettext(string)
return _tag_kwargs(trans, kwargs) if kwargs else trans
if not self.isactive:
return LazyProxy(_tgettext)
return _tgettext()
def dtgettext(self, domain, string, **kwargs):
def _dtgettext():
trans = self.active.dugettext(domain, string)
return _tag_kwargs(trans, kwargs) if kwargs else trans
if not self.isactive:
return LazyProxy(_dtgettext)
return _dtgettext()
def tngettext(self, singular, plural, num, **kwargs):
kwargs = kwargs.copy()
kwargs.setdefault('num', num)
def _tngettext():
trans = self.active.ungettext(singular, plural, num)
return _tag_kwargs(trans, kwargs)
if not self.isactive:
return LazyProxy(_tngettext)
return _tngettext()
def dtngettext(self, domain, singular, plural, num, **kwargs):
kwargs = kwargs.copy()
def _dtngettext():
trans = self.active.dungettext(domain, singular, plural, num)
if '%(num)' in trans:
kwargs.update(num=num)
return _tag_kwargs(trans, kwargs) if kwargs else trans
if not self.isactive:
return LazyProxy(_dtngettext)
return _dtngettext()
translations = TranslationsProxy()
def domain_functions(domain, *symbols):
"""Prepare partial instantiations of domain translation functions.
:param domain: domain used for partial instantiation
:param symbols: remaining parameters are the name of commonly used
translation function which will be bound to the domain
Note: the symbols can also be given as an iterable in the 2nd argument.
"""
if symbols and not isinstance(symbols[0], basestring):
symbols = symbols[0]
_functions = {
'gettext': translations.dgettext,
'_': translations.dgettext,
'ngettext': translations.dngettext,
'tgettext': translations.dtgettext,
'tag_': translations.dtgettext,
'tngettext': translations.dtngettext,
'tagn_': translations.dtngettext,
'add_domain': translations.add_domain,
}
def wrapdomain(symbol):
if symbol == 'N_':
return _noop
return lambda *args, **kw: _functions[symbol](domain, *args, **kw)
return [wrapdomain(s) for s in symbols]
gettext = translations.gettext
_ = gettext
dgettext = translations.dgettext
ngettext = translations.ngettext
dngettext = translations.dngettext
tgettext = translations.tgettext
tag_ = tgettext
dtgettext = translations.dtgettext
tngettext = translations.tngettext
tagn_ = tngettext
dtngettext = translations.dtngettext
def deactivate():
"""Deactivate translations.
:return: the current Translations, if any
"""
return translations.deactivate()
def reactivate(t):
"""Reactivate previously deactivated translations.
:param t: the Translations, as returned by `deactivate`
"""
return translations.reactivate(t)
def make_activable(get_locale, env_path=None):
"""Defer activation of translations.
:param get_locale: a callable returning a Babel Locale object
:param env_path: the environment to use for looking up catalogs
"""
translations.make_activable(get_locale, env_path)
def activate(locale, env_path=None):
translations.activate(locale, env_path)
def add_domain(domain, env_path, locale_dir):
translations.add_domain(domain, env_path, locale_dir)
def get_translations():
return translations
def get_available_locales():
"""Return a list of locale identifiers of the locales for which
translations are available.
"""
try:
locales = [dirname for dirname
in pkg_resources.resource_listdir('trac', 'locale')
if '.' not in dirname
and pkg_resources.resource_exists(
'trac', 'locale/%s/LC_MESSAGES/messages.mo' % dirname)]
return locales
except Exception:
return []
def get_negotiated_locale(preferred_locales):
def normalize(locale_ids):
return [id.replace('-', '_') for id in locale_ids if id]
available_locales = get_available_locales()
if 'en_US' not in available_locales:
available_locales.append('en_US')
locale = Locale.negotiate(normalize(preferred_locales),
normalize(available_locales))
if locale and str(locale) not in available_locales:
# The list of get_available_locales() must include locale
# identifier from str(locale), but zh_* don't be included after
# Babel 1.0. Avoid expanding zh_* to zh_Hans_CN and zh_Hant_TW
# to clear "script" property of Locale instance. See #11258.
locale._data # load localedata before clear script property
locale.script = None
assert str(locale) in available_locales
return locale
has_babel = True
except ImportError: # fall back on 0.11 behavior, i18n functions are no-ops
Locale = None
gettext = _ = gettext_noop
dgettext = dgettext_noop
ngettext = ngettext_noop
dngettext = dngettext_noop
tgettext = tag_ = tgettext_noop
dtgettext = dtgettext_noop
tngettext = tagn_ = tngettext_noop
dtngettext = dtngettext_noop
translations = NullTranslationsBabel()
def activate(locale, env_path=None):
pass
def deactivate():
pass
def reactivate(t):
pass
def make_activable(get_locale, env_path=None):
pass
def get_translations():
return translations
def get_available_locales():
return []
def get_negotiated_locale(preferred=None, default=None):
return None