forked from i2p/i2p.www
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmulti-domain.patch
115 lines (103 loc) · 4.18 KB
/
multi-domain.patch
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
--- env/lib/python2.7/site-packages/flask_babel/__init__.py 2014-02-14 01:30:08.990285797 +0000
+++ env/lib/python2.7/site-packages/flask_babel/__init__.py 2014-02-14 01:30:14.030285865 +0000
@@ -19,6 +19,7 @@
from datetime import datetime
from flask import _request_ctx_stack
from babel import dates, numbers, support, Locale
+from gettext import NullTranslations
from werkzeug import ImmutableDict
try:
from pytz.gae import pytz
@@ -57,9 +58,11 @@
})
def __init__(self, app=None, default_locale='en', default_timezone='UTC',
- date_formats=None, configure_jinja=True):
+ date_formats=None, configure_jinja=True,
+ default_domain=support.Translations.DEFAULT_DOMAIN):
self._default_locale = default_locale
self._default_timezone = default_timezone
+ self._default_domain = default_domain
self._date_formats = date_formats
self._configure_jinja = configure_jinja
self.app = app
@@ -79,6 +82,7 @@
app.config.setdefault('BABEL_DEFAULT_LOCALE', self._default_locale)
app.config.setdefault('BABEL_DEFAULT_TIMEZONE', self._default_timezone)
+ app.config.setdefault('BABEL_DEFAULT_DOMAIN', self._default_domain)
if self._date_formats is None:
self._date_formats = self.default_date_formats.copy()
@@ -97,6 +101,7 @@
self.locale_selector_func = None
self.timezone_selector_func = None
+ self.domain_selector_func = None
if self._configure_jinja:
app.jinja_env.filters.update(
@@ -144,6 +149,19 @@
self.timezone_selector_func = f
return f
+ def domainselector(self, f):
+ """Registers a callback function for domain selection. The default
+ behaves as if a function was registered that returns `None` all the
+ time. If `None` is returned, the domain falls back to the one from
+ the configuration.
+
+ This has to return the domain as a list of strings (eg: ``['messages']``)
+ """
+ assert self.domain_selector_func is None, \
+ 'a localeselector function is already registered'
+ self.domain_selector_func = f
+ return f
+
def list_translations(self):
"""Returns a list of all the locales translations exist for. The
@@ -180,6 +198,13 @@
"""
return timezone(self.app.config['BABEL_DEFAULT_TIMEZONE'])
+ @property
+ def default_domain(self):
+ """The default domain from the configuration as instance of a
+ `string` object.
+ """
+ return self.app.config['BABEL_DEFAULT_DOMAIN']
+
def get_translations():
"""Returns the correct gettext translations that should be used for
@@ -193,7 +218,10 @@
translations = getattr(ctx, 'babel_translations', None)
if translations is None:
dirname = os.path.join(ctx.app.root_path, 'translations')
- translations = support.Translations.load(dirname, [get_locale()])
+ locale = get_locale()
+ for domain in get_domains():
+ dt = support.Translations.load(dirname, [locale], domain)
+ translations = dt if translations is None or not hasattr(translations, 'merge') else translations.merge(dt)
ctx.babel_translations = translations
return translations
@@ -245,6 +273,29 @@
return tzinfo
+def get_domains():
+ """Returns the domains that should be used for this request as
+ `list` object. This returns `None` if used outside of
+ a request.
+ """
+ ctx = _request_ctx_stack.top
+ if ctx is None:
+ return None
+ domains = getattr(ctx, 'babel_domains', None)
+ if domains is None:
+ babel = ctx.app.extensions['babel']
+ if babel.domain_selector_func is None:
+ domains = [babel.default_domain]
+ else:
+ rv = babel.domain_selector_func()
+ if rv is None:
+ domains = [babel.default_domain]
+ else:
+ domains = rv
+ ctx.babel_domains = domains
+ return domains
+
+
def refresh():
"""Refreshes the cached timezones and locale information. This can
be used to switch a translation between a request and if you want