Commits

Zhang Huangbin committed 31f73e3

Improve run-time language switch, use app.add_processor() to get language code.

Comments (0)

Files changed (2)

 
 import iredutils
 
+# Load i18n hook.
+app.add_processor(web.loadhook(iredutils.i18n_loadhook))
+
 # init render
 render = render_jinja(
         tmpldir,                           # template dir.

libs/iredutils.py

         return "%d MB" % (bytes / (base * base))
     return "%.1f GB" % (bytes / (base * base * base))
 
+def i18n_loadhook():
+    web.ctx.lang = web.input(lang=None, _method="GET").lang or session.get('lang')
+
 def get_translations(lang='en_US'):
     # Init translation.
     if cfg.allTranslations.has_key(lang):
 
 def ired_gettext(string):
     """Translate a given string to the language of the application."""
-    translation = load_translations(session.get('lang'))
+    lang = web.ctx.lang
+    translation = load_translations(lang)
     if translation is None:
         return unicode(string)
     return translation.ugettext(string)