From 1f6882c69af3918d81ded8ac1c61a79d040b236b Mon Sep 17 00:00:00 2001 From: equinox Date: Tue, 27 Sep 2011 08:39:35 +0200 Subject: language switch --- static/lang_de.png | Bin 0 -> 545 bytes static/lang_en.png | Bin 0 -> 599 bytes static/layout.css | 4 +++- templates/login.html | 2 ++ tmpl.py | 16 ++++++++++++++-- 5 files changed, 19 insertions(+), 3 deletions(-) create mode 100644 static/lang_de.png create mode 100644 static/lang_en.png diff --git a/static/lang_de.png b/static/lang_de.png new file mode 100644 index 0000000..ac4a977 Binary files /dev/null and b/static/lang_de.png differ diff --git a/static/lang_en.png b/static/lang_en.png new file mode 100644 index 0000000..ff701e1 Binary files /dev/null and b/static/lang_en.png differ diff --git a/static/layout.css b/static/layout.css index d54d79e..a1c59cf 100644 --- a/static/layout.css +++ b/static/layout.css @@ -136,5 +136,7 @@ body#error pre { color:#ffb100; } - +a.lang img { + margin:2px; +} diff --git a/templates/login.html b/templates/login.html index 09b130c..3ca7299 100644 --- a/templates/login.html +++ b/templates/login.html @@ -17,5 +17,7 @@ ${errors.password} + Deutsch + English diff --git a/tmpl.py b/tmpl.py index b729a2f..b761ec2 100644 --- a/tmpl.py +++ b/tmpl.py @@ -22,7 +22,9 @@ def select_language(): lang = cherrypy.thread_data.lang return languages[lang if languages.has_key(lang) else 'en'] accept = cherrypy.request.headers['Accept-Language'] - if isinstance(accept, str): + try: accept = str(accept) + except: accept = None + if accept is not None: for group in accept.split(';'): for lang in group.split(','): if lang.find('=') >= 0: @@ -65,10 +67,20 @@ def expose(filename, method='html', encoding='utf-8', **options): 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'): - cherrypy.thread_data.lang = kwargs['lang'] del kwargs['lang'] + cherrypy.thread_data.translations = select_language() + cherrypy.response.headers['Vary'] = 'Accept-Language' opt = options.copy() if method == 'html': -- cgit v1.2.1