-
Notifications
You must be signed in to change notification settings - Fork 12
/
bottle_i18n.py
213 lines (168 loc) · 7.09 KB
/
bottle_i18n.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
"""The MIT License (MIT)
Copyright (c) 2013 iocast
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE."""
import gettext, os, re, functools
from bottle import PluginError, request, template, DictMixin, TEMPLATE_PATH
def i18n_defaults(template, request):
template.defaults['_'] = lambda msgid, options=None: request.app._(msgid) % options if options else request.app._(msgid)
template.defaults['lang'] = lambda: request.app.lang
def i18n_template(*args, **kwargs):
tpl = args[0] if args else None
if tpl:
tpl = os.path.join("{lang!s}/".format(lang=request.app.lang), tpl)
eles = list(args)
eles[0] = tpl
args = tuple(eles)
return template(*args, **kwargs)
def i18n_view(tmpl, **defaults):
def decorator(func):
@functools.wraps(func)
def wrapper(*args, **kwargs):
file = os.path.join("{lang!s}/".format(lang=request.app.lang), tmpl)
result = func(*args, **kwargs)
if isinstance(result, (dict, DictMixin)):
tplvars = defaults.copy()
tplvars.update(result)
return template(file, **tplvars)
elif result is None:
return template(file, defaults)
return result
return wrapper
return decorator
class I18NMiddleware(object):
@property
def header(self):
return self._header
@property
def http_accept_language(self):
return self.header.get('HTTP_ACCEPT_LANGUAGE')
@property
def app(self):
return self._app
def __init__(self, app, i18n, sub_app=True):
self._app = app
self.app.install(i18n)
self._http_language = ""
i18n.middleware = self
if sub_app:
for route in self.app.routes:
if route.config.get('mountpoint'):
route.config.get('mountpoint').get('target').install(i18n)
def __call__(self, e, h):
self._http_language = e.get('HTTP_ACCEPT_LANGUAGE')
self._header = e
locale = e['PATH_INFO'].split('/')[1]
for i18n in [plugin for plugin in self.app.plugins if plugin.name == 'i18n']:
if locale in i18n.locales:
self.app.lang = locale
e['PATH_INFO'] = e['PATH_INFO'][len(locale)+1:]
return self.app(e,h)
Middleware = I18NMiddleware
class I18NPlugin(object):
name = 'i18n'
api = 2
@property
def middleware(self):
return self._middleware
@middleware.setter
def middleware(self, middleware):
self._middleware = middleware
@property
def keyword(self):
return self._keyword
@property
def locales(self):
return self._locales
@property
def local_dir(self):
return self._locale_dir
def __init__(self, domain, locale_dir, lang_code=None, default='en', keyword='i18n'):
self.domain = domain
if locale_dir is None:
raise PluginError('No locale directory found, please assign a right one.')
self._locale_dir = locale_dir
self._locales = self._get_languages(self._locale_dir)
self._default = default
self._lang_code = lang_code
self._cache = {}
self._apps = []
self._keyword = keyword
def _get_languages(self, directory):
return [dir for dir in os.listdir(self._locale_dir) if os.path.isdir(os.path.join(directory, dir))]
def setup(self, app):
self._apps.append(app)
for app in self._apps:
app._ = lambda s: s
if hasattr(app, 'add_hook'):
# attribute hooks was renamed to _hooks in version 0.12.x and add_hook method was introduced instead.
app.add_hook('before_request', self.prepare)
else:
app.hooks.add('before_request', self.prepare)
app.__class__.lang = property(fget=lambda x: self.get_lang(), fset=lambda x, value: self.set_lang(value))
def parse_accept_language(self, accept_language):
if accept_language == None:
return []
languages = accept_language.split(",")
locale_q_pairs = []
for language in languages:
if language.split(";")[0] == language:
# no q => q = 1
locale_q_pairs.append((language.strip(), "1"))
else:
locale = language.split(";")[0].strip()
q = language.split(";")[1].split("=")[1]
locale_q_pairs.append((locale, q))
return locale_q_pairs
def detect_locale(self):
locale_q_pairs = self.parse_accept_language(self.middleware.http_accept_language)
for pair in locale_q_pairs:
for locale in self._locales:
if pair[0].replace('-', '_').lower().startswith(locale.lower()):
return locale
return self._default
def get_lang(self):
return self._lang_code
def set_lang(self, lang_code=None):
self._lang_code = lang_code
if self._lang_code is None:
self._lang_code = self.detect_locale()
self.prepare()
def prepare(self, *args, **kwargs):
if self._lang_code is None:
self._lang_code = self.detect_locale()
if self._lang_code in list(self._cache.keys()):
trans = self._cache[self._lang_code]
if trans:
trans.install()
for app in self._apps:
app._ = trans.gettext
else:
for app in self._apps:
app._ = lambda s: s
return
try:
trans = gettext.translation(self.domain, self._locale_dir, languages=[self._lang_code])
trans.install()
for app in self._apps:
app._ = trans.gettext
self._cache[self._lang_code] = trans
except Exception as e:
for app in self._apps:
app._ = lambda s: s
self._cache[self._lang_code] = None
def apply(self, callback, route):
return callback
Plugin = I18NPlugin