changeset 5987:e06ce67bcaab

accept-language case-sensitivity bug fix (plus cleanup of duplicate code)
author Thomas Waldmann <tw AT waldmann-edv DOT de>
date Tue, 28 May 2013 11:22:45 +0200
parents c293897aad01
children 488a3cd30701
files MoinMoin/i18n/__init__.py MoinMoin/wsgiapp.py docs/CHANGES
diffstat 3 files changed, 18 insertions(+), 43 deletions(-) [+]
line wrap: on
line diff
--- a/MoinMoin/i18n/__init__.py	Thu May 02 23:09:33 2013 +0200
+++ b/MoinMoin/i18n/__init__.py	Tue May 28 11:22:45 2013 +0200
@@ -10,7 +10,8 @@
         languages -- dict of languages that MoinMoin knows metadata about
 
     Public functions:
-        requestLanguage(request, usecache=1) -- return the request language
+        requestLanguage(request) -- return the request language
+        userLanguage(request) -- return the language from user profile
         wikiLanguages() -- return the available wiki user languages
         browserLanguages() -- return the browser accepted languages
         getDirection(lang) -- return the lang direction either 'ltr' or 'rtl'
@@ -329,31 +330,28 @@
     return translated
 
 
-def requestLanguage(request, try_user=True):
+def userLanguage(request):
     """
-    Return the user interface language for this request.
+    Return the language from a valid user's profile (or None)
 
-    The user interface language is taken from the user preferences for
-    registered users, or request environment, or the default language of
-    the wiki, or English.
+    This should be called once per request, then you should get the value from
+    request object lang attribute.
+    """
+    if request.user and request.user.valid and request.user.language:
+        return request.user.language
+
+
+def requestLanguage(request):
+    """
+    Return the language from request environment (or a default / fallback).
 
     This should be called once per request, then you should get the value from
     request object lang attribute.
 
-    Unclear what this means: "Until the code for get
-    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 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
     # is available on this wiki...
     lang = get_browser_language(request)
--- a/MoinMoin/wsgiapp.py	Thu May 02 23:09:33 2013 +0200
+++ b/MoinMoin/wsgiapp.py	Tue May 28 11:22:45 2013 +0200
@@ -231,38 +231,13 @@
     """ Determine language for the request in absence of any user info. """
     if i18n.languages is None:
         i18n.i18n_init(context)
-
-    lang = None
-    if i18n.languages:
-        cfg = context.cfg
-        if not cfg.language_ignore_browser:
-            for l, w in context.request.accept_languages:
-                logging.debug("client accepts language %r, weight %r" % (l, w))
-                if l in i18n.languages:
-                    logging.debug("moin supports language %r" % l)
-                    lang = l
-                    break
-            else:
-                logging.debug("moin does not support any language client accepts")
-        if not lang:
-            if cfg.language_default in i18n.languages:
-                lang = cfg.language_default
-                logging.debug("fall back to cfg.language_default (%r)" % lang)
-    if not lang:
-        lang = 'en'
-        logging.debug("emergency fallback to 'en'")
+    lang = i18n.requestLanguage(context)
     logging.debug("setup_i18n_preauth returns %r" % lang)
     return lang
 
 def setup_i18n_postauth(context):
     """ Determine language for the request after user-id is established. """
-    user = context.user
-    if user and user.valid and user.language:
-        logging.debug("valid user that has configured some specific language to use in his user profile")
-        lang = user.language
-    else:
-        logging.debug("either no valid user or no specific language configured in user profile, using lang setup by setup_i18n_preauth")
-        lang = context.lang
+    lang = i18n.userLanguage(context) or context.lang
     logging.debug("setup_i18n_postauth returns %r" % lang)
     return lang
 
--- a/docs/CHANGES	Thu May 02 23:09:33 2013 +0200
+++ b/docs/CHANGES	Tue May 28 11:22:45 2013 +0200
@@ -45,6 +45,8 @@
   * AttachFile do=get and do=view: send 404 status if file does not exist
   * link rel="Alternate" links: rather use page.url than request.href, so
     url_prefix_action gets used for the (print and raw) action URLs
+  * fixed wrong detection of UI language from accept-language header for not
+    logged-in users if the language identifier there was (partially) uppercase.
 
 
 Version 1.9.7: