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