Show More
@@ -1,61 +1,78 b'' | |||
|
1 | 1 | # Copyright (C) 2016-2020 RhodeCode GmbH |
|
2 | 2 | # |
|
3 | 3 | # This program is free software: you can redistribute it and/or modify |
|
4 | 4 | # it under the terms of the GNU Affero General Public License, version 3 |
|
5 | 5 | # (only), as published by the Free Software Foundation. |
|
6 | 6 | # |
|
7 | 7 | # This program is distributed in the hope that it will be useful, |
|
8 | 8 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
9 | 9 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
10 | 10 | # GNU General Public License for more details. |
|
11 | 11 | # |
|
12 | 12 | # You should have received a copy of the GNU Affero General Public License |
|
13 | 13 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
14 | 14 | # |
|
15 | 15 | # This program is dual-licensed. If you wish to learn more about the |
|
16 | 16 | # RhodeCode Enterprise Edition, including its added features, Support services, |
|
17 | 17 | # and proprietary license terms, please see https://rhodecode.com/licenses/ |
|
18 | ||
|
19 | from pyramid.i18n import TranslationStringFactory, TranslationString | |
|
18 | from translationstring import TranslationString | |
|
20 | 19 | |
|
21 | 20 | # Create a translation string factory for the 'rhodecode' domain. |
|
22 | from pyramid.threadlocal import get_current_request | |
|
21 | # This is extracted from pyramid.i18n and uses its code to be gevent safe | |
|
22 | ||
|
23 | def TranslationStringFactory(factory_domain): # noqa | |
|
24 | ||
|
25 | def create(msgid, mapping=None, default=None, context=None): | |
|
26 | if isinstance(msgid, TranslationString): | |
|
27 | domain = msgid.domain or factory_domain | |
|
28 | else: | |
|
29 | domain = factory_domain | |
|
30 | ||
|
31 | return TranslationString(msgid, domain=domain, default=default, | |
|
32 | mapping=mapping, context=context) | |
|
33 | return create | |
|
34 | ||
|
23 | 35 | |
|
24 | 36 | _ = TranslationStringFactory('rhodecode') |
|
25 | 37 | |
|
26 | 38 | |
|
27 | 39 | class _LazyString(object): |
|
28 | 40 | def __init__(self, *args, **kw): |
|
29 | 41 | self.args = args |
|
30 | 42 | self.kw = kw |
|
43 | self._request = kw.pop('request', None) | |
|
44 | ||
|
45 | def _get_request(self): | |
|
46 | from pyramid.threadlocal import get_current_request | |
|
47 | return self._request or get_current_request() | |
|
31 | 48 | |
|
32 | 49 | def eval(self): |
|
33 |
req = |
|
|
50 | req = self._get_request() | |
|
34 | 51 | translator = _ |
|
35 | 52 | if req: |
|
36 | 53 | translator = req.translate |
|
37 | 54 | return translator(*self.args, **self.kw) |
|
38 | 55 | |
|
39 | 56 | def __str__(self): |
|
40 | 57 | return self.eval() |
|
41 | 58 | |
|
42 | 59 | def __repr__(self): |
|
43 | 60 | return self.__str__() |
|
44 | 61 | |
|
45 | 62 | def __mod__(self, other): |
|
46 | 63 | return self.eval() % other |
|
47 | 64 | |
|
48 | 65 | def format(self, *args): |
|
49 | 66 | return self.eval().format(*args) |
|
50 | 67 | |
|
51 | 68 | |
|
52 | 69 | def lazy_ugettext(*args, **kw): |
|
53 | 70 | """ Lazily evaluated version of _() """ |
|
54 | 71 | return _LazyString(*args, **kw) |
|
55 | 72 | |
|
56 | 73 | |
|
57 | 74 | def _pluralize(msgid1, msgid2, n, mapping=None): |
|
58 | 75 | if n == 1: |
|
59 | 76 | return _(msgid1, mapping=mapping) |
|
60 | 77 | else: |
|
61 | 78 | return _(msgid2, mapping=mapping) |
General Comments 0
You need to be logged in to leave comments.
Login now