[Zope3-checkins] CVS: Zope3/src/zope/publisher - browser.py:1.23.4.1
Sidnei da Silva
sidnei at x3ng.com.br
Fri Feb 27 12:34:55 EST 2004
Update of /cvs-repository/Zope3/src/zope/publisher
In directory cvs.zope.org:/tmp/cvs-serv31180
Modified Files:
Tag: runyaga-sip-branch
browser.py
Log Message:
Implement 'quality handlling' on browser language detection, with some good tests.
=== Zope3/src/zope/publisher/browser.py 1.23 => 1.23.4.1 ===
--- Zope3/src/zope/publisher/browser.py:1.23 Mon Feb 16 16:37:19 2004
+++ Zope3/src/zope/publisher/browser.py Fri Feb 27 12:34:24 2004
@@ -836,6 +836,11 @@
super(BrowserResponse, self).reset()
self._base = ''
+def normalize_lang(lang):
+ lang = lang.strip().lower()
+ lang = lang.replace('_', '-')
+ lang = lang.replace(' ', '')
+ return lang
class BrowserLanguages:
@@ -846,12 +851,49 @@
def getPreferredLanguages(self):
'''See interface IUserPreferredLanguages'''
- langs = []
- for lang in self.request.get('HTTP_ACCEPT_LANGUAGE', '').split(','):
- lang = lang.strip()
- if lang:
- langs.append(lang.split(';')[0])
- return langs
+ request = self.request
+ accept_langs = request.get('HTTP_ACCEPT_LANGUAGE', '').split(',')
+
+ # Normalize lang strings
+ accept_langs = map(normalize_lang, accept_langs)
+ # Then filter out empty ones
+ accept_langs = filter(None, accept_langs)
+
+ length = len(accept_langs)
+ accepts = []
+
+ for index, lang in enumerate(accept_langs):
+ l = lang.split(';', 2)
+
+ quality = None
+
+ if len(l) == 2:
+ q = l[1]
+ if q.startswith('q='):
+ q = q.split('=', 2)[1]
+ quality = float(q)
+ else:
+ # If not supplied, quality defaults to 1
+ quality = 1.0
+
+ if quality == 1.0:
+ # ... but we use 1.9 - 0.001 * position to
+ # keep the ordering between all items with
+ # 1.0 quality, which may include items with no quality
+ # defined, and items with quality defined as 1.
+ quality = 1.9 - (0.001 * index)
+
+ accepts.append((quality, l[0]))
+
+ # Filter langs with q=0, which means
+ # unwanted lang according to the spec
+ # See: http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.4
+ accepts = filter(lambda acc: acc[0], accepts)
+
+ accepts.sort()
+ accepts.reverse()
+
+ return [lang for quality, lang in accepts]
class BrowserView:
More information about the Zope3-Checkins
mailing list