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
|
import os, traceback
import cherrypy
from genshi.core import Stream
from genshi.output import encode, get_serializer
from genshi.template import Context, TemplateLoader
from genshi.filters import Translator
import gettext
languages = {'en': None, 'de': None, 'ja': None}
for l in languages.keys():
languages[l] = gettext.translation('subdap',
os.path.join(os.path.dirname(__file__), 'locale'), [l],
fallback = True)
def do_translate(text):
transl = cherrypy.thread_data.translations.gettext(text)
if type(transl) == unicode:
return transl
return transl.decode('utf-8')
_ = do_translate
def select_language():
if cherrypy.thread_data.lang != None:
lang = cherrypy.thread_data.lang
return languages[lang if languages.has_key(lang) else 'en']
try:
accept = cherrypy.request.headers['Accept-Language']
accept = str(accept)
except Exception:
accept = None
if accept is not None:
for group in accept.split(';'):
for lang in group.split(','):
if lang.find('=') >= 0:
continue
if lang.find('-') >= 0:
lang = lang[:lang.find('-')]
if languages.has_key(lang):
return languages[lang]
return languages['en']
def template_loaded(template):
Translator(do_translate).setup(template)
loader = TemplateLoader(
os.path.join(os.path.dirname(__file__), 'templates'),
auto_reload = True,
callback = template_loaded
)
def render(*args, **kwargs):
if args:
assert len(args) == 1, 'Expected exactly one argument, but got %r' % (args,)
template = loader.load(args[0])
else:
template = cherrypy.thread_data.template
#ctxt = Context(url = cherrypy.url)
ctxt = Context(url = myurl, lang = cherrypy.thread_data.lang, _ = do_translate)
ctxt.push(kwargs)
return template.generate(ctxt)
def exc(exc):
return render('error.html', details = traceback.format_exc())
def expose(filename, method='html', encoding='utf-8', **options):
def decorate(func):
@cherrypy.expose
def wrapper(*args, **kwargs):
cherrypy.thread_data.template = loader.load(filename)
cherrypy.thread_data.func = func
cherrypy.thread_data.lang = None
if cherrypy.request.cookie.has_key('lang'):
cherrypy.thread_data.lang = cherrypy.request.cookie['lang'].value
if cherrypy.request.params.has_key('lang'):
cherrypy.thread_data.lang = cherrypy.request.params['lang']
if cherrypy.thread_data.lang != None:
cherrypy.response.cookie['lang'] = cherrypy.thread_data.lang
cherrypy.response.cookie['lang']['path'] = '/'
cherrypy.response.cookie['lang']['max-age'] = 2419200
cherrypy.response.cookie['lang']['version'] = 1
if kwargs.has_key('lang'):
del kwargs['lang']
cherrypy.thread_data.translations = select_language()
cherrypy.response.headers['Vary'] = 'Accept-Language'
opt = options.copy()
if method == 'html':
opt.setdefault('doctype', 'html')
serializer = get_serializer(method, **opt)
try:
stream = func(*args, **kwargs)
except Exception, e:
stream = exc(e)
if not isinstance(stream, Stream):
return stream
try:
return encode(serializer(stream), method = serializer, encoding = encoding)
except Exception, e:
return encode(serializer(exc(e)), method = serializer, encoding = encoding)
return wrapper
return decorate
def myurl(url):
return '/subdap/' + url
|