changeset 4155:7e59a7989b23

Make getText into a context property
author Florian Krupicka <florian.krupicka@googlemail.com>
date Tue, 10 Jun 2008 12:50:31 +0200
parents fbbc33a3d40a
children 40501b3e2176
files MoinMoin/web/contexts.py MoinMoin/wsgiapp.py
diffstat 2 files changed, 39 insertions(+), 8 deletions(-) [+]
line wrap: on
line diff
--- a/MoinMoin/web/contexts.py	Mon Jun 09 17:45:29 2008 +0200
+++ b/MoinMoin/web/contexts.py	Tue Jun 10 12:50:31 2008 +0200
@@ -13,6 +13,7 @@
 from werkzeug.utils import Headers, http_date
 from werkzeug.exceptions import Unauthorized, NotFound
 
+from MoinMoin import i18n
 from MoinMoin.request import RequestBase
 from MoinMoin.web.request import Request
 from MoinMoin.web.utils import check_spider
@@ -203,6 +204,33 @@
     def finish(self):
         pass
 
+    def lang(self):
+        if i18n.languages is None:
+            i18n.i18n_init(self)
+
+        lang = None
+        
+        if i18n.languages and not self.cfg.language_ignore_browser:
+            for l in self.accept_languages:
+                if l in i18n.languages:
+                    lang = l
+                    break
+
+        if lang is None and self.cfg.language_default in i18n.languages:
+            lang = self.cfg.language_default
+        else:
+            lang = 'en'
+        return lang
+    lang = property(lang)
+
+    def getText(self):
+        lang = self.lang
+            
+        def _(text, i18n=i18n, request=self, lang=lang, **kw):
+            return i18n.getText(text, request, lang, **kw)
+        return _
+    getText = property(getText)
+
 class RenderContext(Context):
     """ Context for rendering content
     
@@ -213,6 +241,17 @@
     * page
     * output redirection
     """
+    def __init__(self, parent,  page=None, user=None, lang=None):
+        Context.__init__(self, parent)
+        self.page = page
+        self.user = user
+    
+    def lang(self):
+        if self.user.valid and self.user.language:
+            return self.user.language
+        else:
+            return getattr(self._parent, 'lang')
+    lang = property(lang)
 
 # mangle in logging of function calls
 def _logfunc(func):
--- a/MoinMoin/wsgiapp.py	Mon Jun 09 17:45:29 2008 +0200
+++ b/MoinMoin/wsgiapp.py	Tue Jun 10 12:50:31 2008 +0200
@@ -28,7 +28,6 @@
     request.clock = Clock()
     request.clock.start('total')
     request.clock.start('base__init__')
-
     try:
         request.clock.start('load_multi_cfg')
         request.cfg = multiconfig.getConfig(request.url)
@@ -50,9 +49,6 @@
     request.i18n = i18n
     i18n.i18n_init(request)
 
-    lang = i18n.requestLanguage(request, try_user=False)
-    request.getText = lambda text, i18n=i18n, request=request, lang=lang, **kw: i18n.getText(text, request, lang, **kw)
-    
     user_obj = request.cfg.session_handler.start(request, request.cfg.session_id_handler)
     
     request.user = None
@@ -79,11 +75,7 @@
     request.mode_getpagelinks = 0 # is > 0 as long as we are in a getPageLinks call
     request.parsePageLinks_running = {} # avoid infinite recursion by remembering what we are already running
 
-    request.lang = i18n.requestLanguage(request)
-            # Language for content. Page content should use the wiki default lang,
-            # but generated content like search results should use the user language.
     request.content_lang = request.cfg.language_default
-    request.getText = lambda text, i18n=request.i18n, request=request, lang=request.lang, **kv: i18n.getText(text, request, lang, **kv)
 
     request.reset()