summaryrefslogtreecommitdiff
path: root/tmpl.py
diff options
context:
space:
mode:
Diffstat (limited to 'tmpl.py')
-rw-r--r--tmpl.py44
1 files changed, 42 insertions, 2 deletions
diff --git a/tmpl.py b/tmpl.py
index 9748f2a..b729a2f 100644
--- a/tmpl.py
+++ b/tmpl.py
@@ -4,10 +4,43 @@ 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}
+for l in languages.keys():
+ languages[l] = gettext.translation('subdap',
+ os.path.join(os.path.dirname(__file__), 'locale'), [l],
+ fallback = True)
+ print l, '->', languages[l]
+
+def do_translate(text):
+ return cherrypy.thread_data.translations.gettext(text)
+def select_language():
+ if cherrypy.thread_data.lang != None:
+ 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):
+ 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
+ auto_reload = True,
+ callback = template_loaded
)
def render(*args, **kwargs):
@@ -16,8 +49,9 @@ def render(*args, **kwargs):
template = loader.load(args[0])
else:
template = cherrypy.thread_data.template
+
#ctxt = Context(url = cherrypy.url)
- ctxt = Context(url = myurl)
+ ctxt = Context(url = myurl, lang = cherrypy.thread_data.lang)
ctxt.push(kwargs)
return template.generate(ctxt)
@@ -30,6 +64,12 @@ def expose(filename, method='html', encoding='utf-8', **options):
def wrapper(*args, **kwargs):
cherrypy.thread_data.template = loader.load(filename)
cherrypy.thread_data.func = func
+ cherrypy.thread_data.lang = None
+ if kwargs.has_key('lang'):
+ cherrypy.thread_data.lang = kwargs['lang']
+ del kwargs['lang']
+ cherrypy.thread_data.translations = select_language()
+
opt = options.copy()
if method == 'html':
opt.setdefault('doctype', 'html')