changeset 2008:52c474b8c02f

allow translating things early Authentication methods sometimes need to show strings to the user. These can be translated using the browser language but obviously not using user preferences (since no user object is present yet.)
author Johannes Berg <johannes AT sipsolutions DOT net>
date Fri, 20 Apr 2007 15:34:50 +0200
parents 6cc2e0544657
children 1b14cc05a54a
files MoinMoin/i18n/__init__.py MoinMoin/request/__init__.py
diffstat 2 files changed, 10 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/MoinMoin/i18n/__init__.py	Fri Apr 20 15:33:50 2007 +0200
+++ b/MoinMoin/i18n/__init__.py	Fri Apr 20 15:34:50 2007 +0200
@@ -255,7 +255,7 @@
     return translated
 
 
-def requestLanguage(request):
+def requestLanguage(request, try_user=True):
     """ 
     Return the user interface language for this request.
     
@@ -270,13 +270,14 @@
     text is fixed, we are caching the request language locally."
 
     @param request: the request object
+    @param try_user: try getting language from request.user
     @keyword usecache: whether to get the value form the local cache or
                        actually look for it. This will update the cache data.
     @rtype: string
     @return: ISO language code, e.g. 'en'
     """
     # Return the user language preferences for registered users
-    if request.user.valid and request.user.language:
+    if try_user and request.user.valid and request.user.language:
         return request.user.language
 
     # Or try to return one of the user browser accepted languages, if it
--- a/MoinMoin/request/__init__.py	Fri Apr 20 15:33:50 2007 +0200
+++ b/MoinMoin/request/__init__.py	Fri Apr 20 15:34:50 2007 +0200
@@ -149,6 +149,8 @@
         # Pages meta data that we collect in one request
         self.pages = {}
 
+        self._page_headings = {}
+
         self.user_headers = []
         self.cacheable = 0 # may this output get cached by http proxies/caches?
         self.http_caching_disabled = 0 # see disableHttpCaching()
@@ -214,6 +216,11 @@
             self.i18n = i18n
             i18n.i18n_init(self)
 
+            # authentication might require translated forms, so
+            # have a try at guessing the language from the browser
+            lang = i18n.requestLanguage(self, try_user=False)
+            self.getText = lambda text, i18n=self.i18n, request=self, lang=lang, **kv: i18n.getText(text, request, lang, kv.get('formatted', True))
+
             self.user = self.get_user_from_form()
             # setuid handling
             if self.session and 'setuid' in self.session: